dennismonsewicz Posted November 21, 2008 Share Posted November 21, 2008 I have the following code: echo $e_display; echo '<p>Select your state</p>'; echo '<form action="tmotier.php?action=make" method="post">'; echo '<select name="state" onChange="this.form.submit();">'; echo '<option value=""></option>'; $test_qry = mysql_query("SELECT * FROM tbl_name")or die(mysql_error()); while($row = mysql_fetch_object($test_qry)) { echo '<option value="' . $row->state_abbr . '">' . $row->state_abbr . '</option>'; } echo '</select>'; echo '<input type="submit" value="Submit" />'; echo '</form>'; the $e_display var is being set like so: if(empty($_POST['state'])) { $e_display = '<p style="color: red">You must select a state to continue!</p>'; } else { $e_display = ''; } Well when you load the page the error message is still displayed even if you have not submitted anything.. any ideas? Quote Link to comment Share on other sites More sharing options...
flyhoney Posted November 21, 2008 Share Posted November 21, 2008 Im not completely sure, but since state is a string and not an array it might be better to do this: <?php if ($_POST['state']) { $e_display = '<p style="color: red">You must select a state to continue!</p>'; } else { $e_display = ''; } ?> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted November 21, 2008 Share Posted November 21, 2008 All of your form validation and processing code should be enclosed in a conditional statement that checks if the form was submitted. This is commonly done by giving your submit button a name="...." parameter and then checking that the corresponding $_POST['....'] variable is set. echo '<input type="submit" name="submit" value="Submit" />'; if(isset($_POST['submit'])){ // your form was submitted // do your form validation and processing here } 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.