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
https://forums.phpfreaks.com/topic/133711-solved-error-checking-help/
Share on other sites

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 = '';
}
?>

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
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.