chrisrice Posted February 28, 2013 Share Posted February 28, 2013 Hello, I know very little about php and I used a php form generator for a contact page and I'm not sure how to go about making the fields required. Attached is the htm and php files. Your help is greatly appreciated! contact.php contact.htm Quote Link to comment https://forums.phpfreaks.com/topic/275067-php-required-fields/ Share on other sites More sharing options...
timothyarden Posted March 1, 2013 Share Posted March 1, 2013 Hi Chris, Here on the PHP Freaks it is much better to copy and paste the code here betwen [ code ] and [ / code ] without any spaces. Could you please do that and then I will read over your code for you. Thanks Timothy Quote Link to comment https://forums.phpfreaks.com/topic/275067-php-required-fields/#findComment-1415738 Share on other sites More sharing options...
P5system Posted March 1, 2013 Share Posted March 1, 2013 You can use javascript validation. There are lots of free codes are available Quote Link to comment https://forums.phpfreaks.com/topic/275067-php-required-fields/#findComment-1415756 Share on other sites More sharing options...
DavidAM Posted March 1, 2013 Share Posted March 1, 2013 You can use javascript validation. There are lots of free codes are availableJavascript validation is only good for informing the user quickly and without a round-trip to the server. Users have the option of turning off Javascript, and some browsers may not support it at all. So you can NOT depend on Javascript validation to force any kind of restrictions or ... uhh ... validation, at all. Quote Link to comment https://forums.phpfreaks.com/topic/275067-php-required-fields/#findComment-1415757 Share on other sites More sharing options...
chrisrice Posted March 1, 2013 Author Share Posted March 1, 2013 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"> <title>MSM Offer</title> </head> <body style="color: rgb(0, 0, 0); background-color: rgb(8, 8, ;" alink="#000099" link="#000099" vlink="#990099"> <div style="text-align: center;"><img style="width: 534px; height: 272px;" alt="" src="http://offer.mirrorshow.com/offerpage3.jpg"><br> <br> <form method="post" action="contact.php">Fields marked (*) are required <p>NN<span style="color: rgb(246, 246, 246);"><span style="font-weight: bold;">Name</span></span>ame:<br> <input name="Name" type="text"> </p> <p>Co <span style="color: rgb(246, 246, 246);"><span style="font-weight: bold;">Company</span></span>pany:<br> <input name="Company" type="text"> </p> <p>Telep<span style="color: rgb(246, 246, 246);"><span style="font-weight: bold;">Telephone</span></span>hone:<br> <input name="Telephone" type="text"> </p> <p>Em<span style="color: rgb(246, 246, 246);"><span style="font-weight: bold;">Email</span></span>ail:<br> <input name="Email" type="text"> </p> <p><input name="submit" value="Submit" type="submit"></p> </form> <p></p> <a href="http://mirrorshowmanagement.com"><img style="border: 0px solid ; width: 447px; height: 125px;" alt="" src="http://offer.mirrorshow.com/logo.jpg"></a></div> </body> </html> <?php // Website Contact Form Generator // http://www.tele-pro.co.uk/scripts/contact_form/ // This script is free to use as long as you // retain the credit link // get posted data into local variables $EmailFrom = "offerpage@mirrorshow.com"; $EmailTo = "chris.rice@mirrorshow.com"; $Subject = "Offer Submission"; $Name = Trim(stripslashes($_POST['Name'])); $Company = Trim(stripslashes($_POST['Company'])); $Telephone = Trim(stripslashes($_POST['Telephone'])); $Email = Trim(stripslashes($_POST['Email'])); // validation $validationOK=true; if (!$validationOK) { print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; exit; } // prepare email body text $Body = ""; $Body .= "Name: "; $Body .= $Name; $Body .= "\n"; $Body .= "Company: "; $Body .= $Company; $Body .= "\n"; $Body .= "Telephone: "; $Body .= $Telephone; $Body .= "\n"; $Body .= "Email: "; $Body .= $Email; $Body .= "\n"; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); // redirect to success page if ($success){ print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">"; } else{ print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/275067-php-required-fields/#findComment-1415797 Share on other sites More sharing options...
jcbones Posted March 1, 2013 Share Posted March 1, 2013 To make a required input, you need to check if that input is empty. <?php // get posted data into local variables $EmailFrom = "offerpage@mirrorshow.com"; $EmailTo = "chris.rice@mirrorshow.com"; $Subject = "Offer Submission"; $Name = Trim(stripslashes($_POST['Name'])); $Company = Trim(stripslashes($_POST['Company'])); $Telephone = Trim(stripslashes($_POST['Telephone'])); $Email = Trim(stripslashes($_POST['Email'])); if(empty($Name)) { $message = 'You are required to input a name.'; } Quote Link to comment https://forums.phpfreaks.com/topic/275067-php-required-fields/#findComment-1415818 Share on other sites More sharing options...
chrisrice Posted March 1, 2013 Author Share Posted March 1, 2013 To make a required input, you need to check if that input is empty. <?php // get posted data into local variables $EmailFrom = "offerpage@mirrorshow.com"; $EmailTo = "chris.rice@mirrorshow.com"; $Subject = "Offer Submission"; $Name = Trim(stripslashes($_POST['Name'])); $Company = Trim(stripslashes($_POST['Company'])); $Telephone = Trim(stripslashes($_POST['Telephone'])); $Email = Trim(stripslashes($_POST['Email'])); if(empty($Name)) { $message = 'You are required to input a name.'; } I entered that portion into the code and it still let me submit with the name field blank. Quote Link to comment https://forums.phpfreaks.com/topic/275067-php-required-fields/#findComment-1415852 Share on other sites More sharing options...
jazzman1 Posted March 1, 2013 Share Posted March 1, 2013 (edited) I entered that portion into the code and it still let me submit with the name field blank. Just return false. if(empty($Name)) { $message = 'You are required to input a name.'; return false; } How is treated an integer by 0 in the form , as true or false? Edited March 1, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/275067-php-required-fields/#findComment-1415853 Share on other sites More sharing options...
Christian F. Posted March 1, 2013 Share Posted March 1, 2013 This is what I got when I visited the link to the generator: You are viewing an archived website. Would you like to visit the new updated website?Now, when it comes to code it is very much a perishable goods, so whenever you see a message like that the best advice is to simply move on. Don't use the code on such a site, as it is bound to be out of date and thus insecure and/or buggy. That said, the steps you can take to make this code serviceable, and to add validation is first and foremost to remove all of those stripslashes. Then, as jcbones stated, you'll need to check if the input values are empty. Or, if they otherwise does not confirm to the pattern of expected legal input. In other words, check that an e-mail address actually looks like and e-mail address, that a number is actually a number and so forth. A lot of this is done with filter_var and the various "ctype_* ()" functions such as ctype_alnum. If validation fails for one of the elements: Add an error message, I recommend using arrays for this, and then set the validation flag to false. Of course, this assumes you've set it to true by default. Another way to check for failed validation, is to check whether or not the error array is empty. Then you don't need the validation flag. Quote Link to comment https://forums.phpfreaks.com/topic/275067-php-required-fields/#findComment-1415855 Share on other sites More sharing options...
chrisrice Posted March 1, 2013 Author Share Posted March 1, 2013 Would you be able to provide me the recommended code? Quote Link to comment https://forums.phpfreaks.com/topic/275067-php-required-fields/#findComment-1415867 Share on other sites More sharing options...
timothyarden Posted March 2, 2013 Share Posted March 2, 2013 Generally on PHP Freaks we don't provide code, thats generally a paid service. You can list a job on the PHP Freaks Freelancing Forum. Quote Link to comment https://forums.phpfreaks.com/topic/275067-php-required-fields/#findComment-1415952 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.