SEVIZ Posted June 16, 2009 Share Posted June 16, 2009 I have a dropdown with choices. Inside I have titles for sections with a value of "none". Is there a way via php that if value = none it errors on submit? Most importantly is there a way to do this WITHOUT javascript? Thanks Quote Link to comment Share on other sites More sharing options...
garethhall Posted June 16, 2009 Share Posted June 16, 2009 Sure there is as long as the submit page is the same as the page with the form elements. Like this <?php if(isset($_POST['confirm'])){ if($_POST['mycolor'] <> "none"){ echo $_POST['mycolor']; }else{ echo "Please select a color"; } } ?> <html> <body> <form name="frm" methord="post"> <select name="mycolor"> <option value="none">Select a Color</option> <option value="red">Red</option> <option value="blue">Blue</option> <option value="green">Green</option> </select> <input type="submit" name="confirm" value="Send"/> </form> </body </html> Quote Link to comment Share on other sites More sharing options...
haku Posted June 16, 2009 Share Posted June 16, 2009 if(isset($_POST['submit'])) // this is if you have a submit button with the 'name' attribute of 'submit' { if($_POST['whatever'] == 'none') { $errors = TRUE; } if(!$errors) { //do the rest of your form processing } } The first 'if' statement checks if the form has been submitted. Then $errors is set to false, since there aren't any. After that, a check is made to see if the dropdown's value is equal to none. If it is, you have errors, and as such, $errors is set to true. Since $errors is true, the rest of the processing will not be done. Quote Link to comment Share on other sites More sharing options...
SEVIZ Posted June 16, 2009 Author Share Posted June 16, 2009 Currently my Submit button loads to a second page. Is that a problem for these helpful examples? Quote Link to comment Share on other sites More sharing options...
haku Posted June 16, 2009 Share Posted June 16, 2009 Yes. <?php if($_POST['whatever'] == 'none') { session_start(); $_SESSION['errors'] = 'Whatever was empty'; header('Location: page/with/the/form.php'); } ?> Then on the page with the form: <?php session_start(); if(isset($_SESSION['errors'])) { echo $_SESSION['errors']; unset($_SESSION['errors']); } // output form ?> Note: you will actually have to use session_start() at the top of your document before any output, and you will probably want to add some html to the errors and stuff, but the basic logic is there. Quote Link to comment 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.