Skor Posted October 28, 2007 Share Posted October 28, 2007 I've got one that has me scratching my head, not sure exactly how to do this. I've got a drop down menu that I would like to consider mandatory. If it's not selected, I'd like to display an error on the page close to the drop-down, if it is selected, the form should post to another page. I've tinkered with a little javascript which I couldn't figure out. I've used a switch to display different dropdowns and that works fine, I've just included one as an example. Here's what the form looks like: <FORM NAME="CORDS" METHOD="POST" ACTION="checkmeout.php"> <INPUT TYPE="HIDDEN" NAME="product" VALUE="<?php echo $item3; ?>"> <INPUT TYPE="HIDDEN" NAME="price" VALUE="<?php echo $price; ?>"> <INPUT TYPE="HIDDEN" NAME="pagetype" VALUE="neck"> switch ($neck) { case "A": echo " <input type=\"hidden\" name=\"product[]\" value=\"$item3\"> <select name=\"product[]\"><option value=\"\">Select your cord or chain:</option> <option value=\"18-inch silver chain\">18-inch Sterling silver box chain</option> <option value=\"16-inch black cord\">16-inch black rubber cord</option> <option value=\"18-inch black cord\">18-inch black rubber cord</option></select>"; break; } ?> <br><br>Include giftbox <input type="checkbox" name="giftbox" value="giftbox"> <INPUT TYPE="SUBMIT" Value="Add to Cart"> </FORM> Quote Link to comment https://forums.phpfreaks.com/topic/75070-form-validation-and-error-message/ Share on other sites More sharing options...
schme16 Posted October 28, 2007 Share Posted October 28, 2007 I find using foreach($_POST as $post) { if($post==NULL){print 'You have left fields blank!';} } usually does the trick, however it will check all of the posted variable to check its contents isn't null (there is a way to make it specific but unless you need it don't bother... if you need it specific then just ask and I'll give you the longer piece for that) Quote Link to comment https://forums.phpfreaks.com/topic/75070-form-validation-and-error-message/#findComment-379709 Share on other sites More sharing options...
Skor Posted October 28, 2007 Author Share Posted October 28, 2007 Yes, I do need to make it specific to the drop down. The check box is an option and can be null. Thanks much. Quote Link to comment https://forums.phpfreaks.com/topic/75070-form-validation-and-error-message/#findComment-379787 Share on other sites More sharing options...
wildteen88 Posted October 28, 2007 Share Posted October 28, 2007 If you only want to validate the pull down, You'll want to use something like the following: if(!isset($_POST['product'][1]) || empty($_POST['product'][1])) { echo 'You must select a product from the pulldown menu'; } Quote Link to comment https://forums.phpfreaks.com/topic/75070-form-validation-and-error-message/#findComment-379798 Share on other sites More sharing options...
Skor Posted October 29, 2007 Author Share Posted October 29, 2007 Where do you put the "if" statement -- at the top of the form, bottom, outside of the form tags. Just curious. Tinkering with your suggestion and haven't gotten it to work yet.... Quote Link to comment https://forums.phpfreaks.com/topic/75070-form-validation-and-error-message/#findComment-380126 Share on other sites More sharing options...
darkfreaks Posted October 29, 2007 Share Posted October 29, 2007 in the php tags at the top Quote Link to comment https://forums.phpfreaks.com/topic/75070-form-validation-and-error-message/#findComment-380138 Share on other sites More sharing options...
wildteen88 Posted October 29, 2007 Share Posted October 29, 2007 Where do you put the "if" statement -- at the top of the form, bottom, outside of the form tags. Just curious. Tinkering with your suggestion and haven't gotten it to work yet.... That code will go into the script which processes the form when it is submitted. Looking at your forms action tag the script which processing the form is checkmeout.php Quote Link to comment https://forums.phpfreaks.com/topic/75070-form-validation-and-error-message/#findComment-380578 Share on other sites More sharing options...
Skor Posted October 29, 2007 Author Share Posted October 29, 2007 I was hoping to put the error on the form itself, near the drop-down...is that possible to do. If not, I can certainly put it on the post page, which I'll try this evening. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/75070-form-validation-and-error-message/#findComment-380645 Share on other sites More sharing options...
wildteen88 Posted October 30, 2007 Share Posted October 30, 2007 Yes you can do this however you you'll need to change the forms action, so it submits to itself (the page the form is displayed on) rather than getting submitted to another page. Example: <?php $error = null; // form validation: if(isset($_POST['name']) && empty($_POST['name'])) { $error[] = 'Please fill in the name field'; } if(isset($_POST['age']) && empty($_POST['age'])) { $error[] = 'Please fill in the age field'; } // display errors if there is any: if(is_array($error)) { echo 'Please correct the following:<br />'; echo '<ul><li>' . implode('</li><li>', $error) . '</li></ul>'; } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> Name: <input type="text" name="name" /><br /> Age: <input type="text" name="age" /><br /> <input type="submit" name="submit" value="Submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/75070-form-validation-and-error-message/#findComment-381353 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.