required form field help please -...

User 55304 Photo


Registered User
72 posts

Hi Please can someone help me and my wife with a problem regarding required form fields. Ok, we are creating a php form for booking holiday lets and we have no php knowledge at all. We have been working on a form which is going relatively well but we need to know how to make all the fiels within the form all required so no information can be left out. Is there a line or two of code we can add to the form script to make all our form fields required. For your information we do own CC Form Builder but in this case we cannot use the software due to a customers preference, I personally disagree with her ideas but its the fact that the customer is always right especially in this case.

Any help please
Regards
chris
User 37670 Photo


Registered User
2,138 posts

There are different ways to accomplish this, depending on your needs and programming skills. Here is a php method, but you might want to google form validation using javascript as an alternative.

You can check the length of the characters in the form field. An empty field would return an error, therefore making sure at least something was typed in.

//check $_POST['name'] and strip any slashes:
if (strlen($_POST['name']) >0) {
$name = stripslashes($_POST['name']);
} else { //if no name was entered...
$firstname= NULL;
echo '<p><font color="red"><b>You forgot to enter your first name! Please use the back button on your browser, make sure you enter your name before clicking the submit button.</b></font></p>';
}

This will produce the error message on the browser instead of processing the form. you can continue with your regular php code for the form after you have put validation for every form field that you need. The 'stripslashes' will remove any slashes the user might have put into the field, which could cause the php to missread things.

There are many variations to this code. You can use php to check for numbers and formatting for phone numbers, special characters and such. A quick google on 'php form validation' should produce lots of examples and tutorial sites.
Notice I use the php5 $_POST['name'] rather than the older $name, which worked in php4. the $_POST['name'] is more secure and should work even if your server's php is older.

Hope this puts you on the right track, and don't forget to search for javascript solutions.
NOTE: the javascript solutions won't/might not work if the user has java turned off on their machine
E-Learning Specialist
www.mainsites.ca is my website, and yes, some of it is crappy.
User 55304 Photo


Registered User
72 posts

Thank you Cliff, for taking the time to write that info out for me,
I shall give it a go and hopefully achieve some good results.

Kind regards

Have something to add? We’d love to hear it!
You must have an account to participate. Please Sign In Here, then join the conversation.