kerrya Posted September 28, 2009 Share Posted September 28, 2009 What I want to do is make sure that the name and the email address are completed before the form is submitted. This is my code for the form. How do i do it? <!-- <form> --> <table border="1"> <tr><td>Your Name </td><td><input type="text" name="Name" /></td></tr> <tr><td>Email Address</td><td><input type="text" name="Email" /></td></tr> <tr><td colspan="2"> <input type="submit" value="Submit for Quote" /></td></tr> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/175751-solved-required-field-in-form/ Share on other sites More sharing options...
Alex Posted September 28, 2009 Share Posted September 28, 2009 If you want to make sure before it's submitted you'll have to use JavaScript. Either way you will need to do PHP validation (Because people can turn off JavaScript). Here's an example of how to do it: if(!isset($_POST['Name']) || !isset($_POST['Email'])) { echo "Required fields were left blank"; } else { //Process as usual. } isset() Use this to check if the variables are set and aren't NULL. Quote Link to comment https://forums.phpfreaks.com/topic/175751-solved-required-field-in-form/#findComment-926157 Share on other sites More sharing options...
kerrya Posted September 28, 2009 Author Share Posted September 28, 2009 Ok, thanks. I am new to this so where would i put it in my code? Quote Link to comment https://forums.phpfreaks.com/topic/175751-solved-required-field-in-form/#findComment-926158 Share on other sites More sharing options...
Alex Posted September 28, 2009 Share Posted September 28, 2009 Well, in the code you posted you didn't even have an action set for the form (the action attribute for the form tells the form where to submit the data too.) Additionally you need to specify the method, which in your case would be post. Use this: <form action="somefile.php" method="post"> <table border="1"> <tr><td>Your Name </td><td><input type="text" name="Name" /></td></tr> <tr><td>Email Address</td><td><input type="text" name="Email" /></td></tr> <tr><td colspan="2"> <input type="submit" value="Submit for Quote" /></td></tr> </table> </form> Then inside "somefile.php" (Use a different filename if you want, just make sure to edit that in the form then). somefile.php: if(!isset($_POST['Name']) || !isset($_POST['Email'])) { echo "Required fields were left blank"; } else { echo "Name: {$_POST['Name']} <br />\n Email: {$_POST['Email']}"; } Quote Link to comment https://forums.phpfreaks.com/topic/175751-solved-required-field-in-form/#findComment-926160 Share on other sites More sharing options...
kerrya Posted September 28, 2009 Author Share Posted September 28, 2009 I am getting a little confused now because i tried something different and now have this My form page <form method="post" action="xxxxxx/Scripts/ptrquoteemail.php" enctype="multipart/form-data"> <div> <input type="hidden" name="redirect" value="http://xxxxxx/quotethankyou.php" /></div> <!-- <form> --> <table border="1"> <tr><td>Your Name </td><td><input type="text" name="Name" /></td></tr> <tr><td>Email Address</td><td><input type="text" name="Email" /></td></tr> <tr><td colspan="2"> <input type="submit" value="Submit for Quote" /></td></tr> </table> On the ptrquoteemail page I have <?php require '/home/xxxxxxxxxxxxx/Includes/connect.php'; /* Assign all variables passed from the form */ $client = mysql_real_escape_string($_POST['Name']); $email = mysql_real_escape_string($_POST['Email']); /* Sending email with information to us */ $to = "[email protected]"; $subject = "Quote"; $random_hash = md5(date('r', time())); $headers = 'From: kerry.nz'; $headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; ob_start(); ?> --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit <!-- Email Content --> <? include '/home/xxxxxxxxx/Includes/ptrquote.inc'; ?> <!-- End of Email Content --> --PHP-alt-<?php echo $random_hash; ?>-- <?php $html = ob_get_contents(); ob_end_clean(); $body = $html; mail($to, $subject, $body, $headers); /* Once Complete return to............*/ header("Location: https://xxxxxx/quotethankyou.php"); ?> The ptrquote.inc page has <table width="100%" cellspacing="0"> <tr bgcolor="#EDEEEA"> </table> <table width="100%" height="100%" cellpadding="50"> <tr> <td>This quote was submitted by <? echo $client ?> at <? echo date('r', time()); ?><br/><br/> Name: <? echo $client;?><br/><br/> Email: <? echo $email;?><br/><br/> I know all this might sound silly but I am at a lost where to go with ensuring the name and email are entered. Quote Link to comment https://forums.phpfreaks.com/topic/175751-solved-required-field-in-form/#findComment-926163 Share on other sites More sharing options...
Alex Posted September 28, 2009 Share Posted September 28, 2009 Firstly, for future reference please post all code either inside of [ code ] or [ php ] (without the spaces), just makes it easier to read. Now, here's how you'd apply what I just posted: <?php require '/home/xxxxxxxxxxxxx/Includes/connect.php'; if(!isset($_POST['Name']) || !isset($_POST['Email'])) { echo 'Required fields missing'; exit; } /* Assign all variables passed from the form */ $client = mysql_real_escape_string($_POST['Name']); $email = mysql_real_escape_string($_POST['Email']); /* Sending email with information to us */ $to = "[email protected]"; $subject = "Quote"; $random_hash = md5(date('r', time())); $headers = 'From: kerry.nz'; $headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; ob_start(); ?> Usually using exit like that is a sign of bad logic and planning, but in less complicated situations like this it doesn't really matter. Quote Link to comment https://forums.phpfreaks.com/topic/175751-solved-required-field-in-form/#findComment-926168 Share on other sites More sharing options...
kerrya Posted September 28, 2009 Author Share Posted September 28, 2009 Apologies for the formatting, i am new to all this. I copied your text and put that in my ptrquoteemail.php like shown and when I submit I get my thank you page and still get blank fields Quote Link to comment https://forums.phpfreaks.com/topic/175751-solved-required-field-in-form/#findComment-926169 Share on other sites More sharing options...
Alex Posted September 28, 2009 Share Posted September 28, 2009 My Mistake :-\ Use a empty() to make sure it has a value (And not that it was just submitted like you'd do with isset()). <?php require '/home/xxxxxxxxxxxxx/Includes/connect.php'; if(empty($_POST['Name']) || empty($_POST['Email'])) { echo 'Required fields missing'; exit; } /* Assign all variables passed from the form */ $client = mysql_real_escape_string($_POST['Name']); $email = mysql_real_escape_string($_POST['Email']); /* Sending email with information to us */ $to = "[email protected]"; $subject = "Quote"; $random_hash = md5(date('r', time())); $headers = 'From: kerry.nz'; $headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; ob_start(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/175751-solved-required-field-in-form/#findComment-926170 Share on other sites More sharing options...
kerrya Posted September 28, 2009 Author Share Posted September 28, 2009 Thanks - so hard sometime with code to get it just right. Ok, so now i get that message is there any way I can direct them back to the page they came from? Is it as simple as putting an href="xxxxx.php"? Quote Link to comment https://forums.phpfreaks.com/topic/175751-solved-required-field-in-form/#findComment-926171 Share on other sites More sharing options...
Alex Posted September 28, 2009 Share Posted September 28, 2009 Using header() one of the headers you can send is essentially a redirect: header('Location: http://phpfreaks.com'); Would redirect to phpfreaks.com, just put there were you want to redirect. Quote Link to comment https://forums.phpfreaks.com/topic/175751-solved-required-field-in-form/#findComment-926172 Share on other sites More sharing options...
kerrya Posted September 28, 2009 Author Share Posted September 28, 2009 Superb! Thanks so much you are a champ Quote Link to comment https://forums.phpfreaks.com/topic/175751-solved-required-field-in-form/#findComment-926178 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.