betsy_ls Posted September 12, 2009 Share Posted September 12, 2009 I am trying to edit a form so that certain fields are required to submit. I am very, very new to PHP though and am not sure how to do this. The form already makes sure there is a valid email, but I also want the first name, last name and organization to be required. Here is my PHP code. <?php if(isset($_POST['submitform']) && $_POST['submitform']==1) { if(isset($_POST['email']) && eregi("^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$", $_POST['email'])) { $to = "[email protected]"; $subject = "GTT FORM SUBMISSION"; $headers = "From: ".$_POST['email']; $date = date("r"); $message = "Form submission sent from 'Subscribe' at ".$date."\n\n"; $message .= "Salutation : ".$_POST['salutation']."\n"; $message .= "First Name : ".$_POST['firstname']."\n"; $message .= "Last Name : ".$_POST['lastname']."\n"; $message .= "Company : ".$_POST['organization']."\n"; $message .= "Title : ".$_POST['title']."\n\n"; $message .= "Country : ".$_POST['country']."\n\n"; $message .= "Phone : ".$_POST['phone']."\n"; $message .= "Email : ".$_POST['email']."\n\n"; if(isset($_POST['addtonetwork']) && $_POST['addtonetwork']=='yes') { $message .= "YES - Add to network.\n\n"; } else { $message .= "NO - Do not add to network.\n\n"; } if(mail($to, $subject, $message, $headers)) { include("includes/thankyou.php"); } else { global $error; $error = "Mail could not be sent!"; include("includes/form_subscribe.php"); } } else { global $error; $error = "You must submit a valid E-Mail."; include("includes/form_subscribe.php"); } } else { include("includes/form_subscribe.php"); } ?> Please let me know if you need any other code. I have been reading tutorials about this all day, but I do not know enough PHP to be able to apply it to the code I am editing. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/174020-solved-required-fields-in-a-form/ Share on other sites More sharing options...
obay Posted September 12, 2009 Share Posted September 12, 2009 many ways to do this. this is how i would do it: <?php $valid_email = false; $valid_first = false; $valid_last = false; $valid_org = false; $errors = array(); if(isset($_POST['submitform']) && $_POST['submitform']==1) { if(isset($_POST['email']) && eregi("^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$", $_POST['email'])) { $valid_email = true; } else { array_push($errors, "You must submit a valid E-Mail."); } if ($_POST['firstname'])!="") { $valid_first = true; } else { array_push($errors, "You must enter your first name."); } if ($_POST['lastname'])!="") { $valid_last = true; } else { array_push($errors, "You must enter your last name."); } if ($_POST['organization'])!="") { $valid_org = true; } else { array_push($errors, "You must enter your organization."); } if ($valid_email && $valid_first && $valid_last && $valid_org) { $to = "[email protected]"; $subject = "GTT FORM SUBMISSION"; $headers = "From: ".$_POST['email']; $date = date("r"); $message = "Form submission sent from 'Subscribe' at ".$date."\n\n"; $message .= "Salutation : ".$_POST['salutation']."\n"; $message .= "First Name : ".$_POST['firstname']."\n"; $message .= "Last Name : ".$_POST['lastname']."\n"; $message .= "Company : ".$_POST['organization']."\n"; $message .= "Title : ".$_POST['title']."\n\n"; $message .= "Country : ".$_POST['country']."\n\n"; $message .= "Phone : ".$_POST['phone']."\n"; $message .= "Email : ".$_POST['email']."\n\n"; if(isset($_POST['addtonetwork']) && $_POST['addtonetwork']=='yes') { $message .= "YES - Add to network.\n\n"; } else { $message .= "NO - Do not add to network.\n\n"; } if(mail($to, $subject, $message, $headers)) { include("includes/thankyou.php"); } else { global $error; $error = "Mail could not be sent!"; include("includes/form_subscribe.php"); } } else { global $error; $error = implode("<br />", $errors); include("includes/form_subscribe.php"); } } else { include("includes/form_subscribe.php"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/174020-solved-required-fields-in-a-form/#findComment-917321 Share on other sites More sharing options...
betsy_ls Posted September 12, 2009 Author Share Posted September 12, 2009 When I use that code I get this error... Parse error: syntax error, unexpected T_IS_NOT_EQUAL in /data/17/1/152/115/1804278/user/1953870/htdocs/subscribe.php on line 79 This is line 79... if ($_POST['firstname'])!="") { Quote Link to comment https://forums.phpfreaks.com/topic/174020-solved-required-fields-in-a-form/#findComment-917326 Share on other sites More sharing options...
obay Posted September 12, 2009 Share Posted September 12, 2009 my bad. remove the ) before the ! Quote Link to comment https://forums.phpfreaks.com/topic/174020-solved-required-fields-in-a-form/#findComment-917330 Share on other sites More sharing options...
betsy_ls Posted September 12, 2009 Author Share Posted September 12, 2009 Thanks! That worked. Now, I was wondering how you keep the information they have already entered into the form when the error message comes up... so they don't have to type everything in again. Quote Link to comment https://forums.phpfreaks.com/topic/174020-solved-required-fields-in-a-form/#findComment-917332 Share on other sites More sharing options...
obay Posted September 12, 2009 Share Posted September 12, 2009 put the $_POST[] value as the value in the text boxes for example <input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" /> <input type="text" name="lastname" value="<?php echo $_POST['lastname']; ?>" /> etc... Quote Link to comment https://forums.phpfreaks.com/topic/174020-solved-required-fields-in-a-form/#findComment-917362 Share on other sites More sharing options...
pneudralics Posted September 12, 2009 Share Posted September 12, 2009 Thanks! That worked. Now, I was wondering how you keep the information they have already entered into the form when the error message comes up... so they don't have to type everything in again. Something like this: <form> <input type="text" name="input name goes here" value"<?php echo $_POST['input name goes here']; ?>" /> </form Obay beat me to it. Quote Link to comment https://forums.phpfreaks.com/topic/174020-solved-required-fields-in-a-form/#findComment-917363 Share on other sites More sharing options...
obay Posted September 12, 2009 Share Posted September 12, 2009 lol Quote Link to comment https://forums.phpfreaks.com/topic/174020-solved-required-fields-in-a-form/#findComment-917366 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.