Jump to content

[SOLVED] error checking help


dennismonsewicz

Recommended Posts

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?

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.