Jump to content

Basic ? about forms... how to...


artdoesart

Recommended Posts

Thanks for looking...

 

i am making a form with basic information there is some "req" fields so there will be like a javascript or a php function in place to do this. i am leaning towards the javascript pop-up deal, however my ? is this.

 

i want to create a form but there are many questions... some of the fields are required, but when someone submits the form but doesnt do all the required fields and they get that pop-up saying "you need to fill out this" i do not want the form to erase so they have to start all over again...

 

any help on this matter would be nice thanks.

Link to comment
https://forums.phpfreaks.com/topic/96782-basic-about-forms-how-to/
Share on other sites

I would suggest using PHP. Not all users have JS enabled and you will be sure that the PHP method will work for all users.

 

Your basic format would be like:

<?php

// test to see that the form is submitted
if (isset($_POST['submit']) && $_POST['submit'] == "submit")
{

// grab all the posted values here
$name = $_POST['name'];
$address= $_POST['address'];
$phone= $_POST['phone'];

// test each value to make sure it's not empty
// or that it's formatted correctly according to what you want.
// you have a couple options here.
// you can have multiple error variables that are specific to each input
// or 1 general error array or variable.
$errors .= (empty($name)) ? "<br />What's your name?" : "";
$errors .= (empty($address)) ? "<br />What's your address?" : "";
$errors .= (empty($phone)) ? "<br />What's your phone?" : "";

if (!$errors) // if the variable $errors doesn't have anything
{
	// process the form and maybe redirect to a thank you page
}
// if you don't process the form, we have errors, so we want to reshow the form
}
?>
<p>All fields are required.</p>
<?php
// this only happens if the form posted AND they messed up
// otherwise the form just posts as normal
if ($errors)
{
echo "<p>There are some errors: $errors</p>";
}
?>

<form action="page.php" method="post">
<input type="text" name="name" value="<?php echo $name; ?>" />
<input type="text" name="email" value="<?php echo $email; ?>" />
<input type="text" name="phone" value="<?php echo $phone; ?>" />

<input type="submit" name="submit" value="submit"
</form>
<?php
// the echo statements inside the value attribute
// are so that the original value is reposted if the form hit errors.
// the first time the page loads, those variable won't have any
// values in them so it won't matter because they will echo nothing
?>

 

There are a few more steps, especially with grabbing the data from the $_POST array, to make sure it's escaped for sending you an email or for updating the database. Search for how to make your $_POST data safe for inserting into a database. If the data isn't escaped and you hit an error, you might have slashes start appearing in your data once it's reposted because of errors. You don't want that.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.