Jump to content

a little validation help


dennismonsewicz

Recommended Posts

how would i validate the following without having to replicate the form for submission twice?

 

echo '<p>Select your state</p>';

				echo '<form action="file.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" name="state_submit" />';

				echo '</form>';

 

so i want to see if the user selects a state. but how do i check it without writing the form twice?

Link to comment
https://forums.phpfreaks.com/topic/134043-a-little-validation-help/
Share on other sites

Not sure I follow your statement "how do i check it without writing the form twice".

 

I typically have all my forms POST back to themselves. If validation fails I can reshow the form with the POSTED values that were valid and the others blank. If validatin passes I will either process the data on the same page or save the posted data to session variables and redirect to the processing page.

 

Here's an example:

//Create array of state abbr list
$query = "SELECT * FROM tbl_name";
$result = mysql_query($query)or die(mysql_error());
while($record = mysql_fetch_object($result))
{
    $state_list[] = $row->state_abbr
}

$valid = false;

//If form submitted validate data
if (isset($_POST['state_submit']))
{
    $valid = (in_array($_POST['state_submit'], $state_list));
}

if ($valid)
{
    //Process the data

    //Redirect to confirmation page
    header('Location: http://www.mysite.com/confirm.php');
}
else
{
    //Show the form
    echo "<p>Select your state</p>\n";
    echo "<form action=\"file.php?action=make\" method=\"post\">\n";
    echo "<select name=\"state\" onChange=\"this.form.submit();\">\n";
    echo "<option value=\"\"></option>\n";
    foreach ($state_list as $state_abbr)
    {
        echo "<option value=\"$state_abbr\">$state_abbr</option>\n";
    }
    echo "</select>\n";
    echo "<input type=\"submit\" value=\"Submit\" name=\"state_submit\" />\n";
    echo "</form>\n";
}

thanks! I think i have solved my problem... i just have one more question.

 

how do you grab all of the URL variables?

 

example:

 

http://www.mysite.com/index.php?action=view&id=56

 

how would i grab the action and the view say all together? I know how to use the PHP superglobal $_GET but is there a way to grab both vars at once?

Not sure what you mean by that. Are you wanting to grab the two variables as a concatenated string:

$combined = $_GET['action'] . $_GET['id'];

 

Although that would not be advised as you should validate those variables first. You could also use implode() on the $_GET var to combine ALL the GET vars, but again, not very safe in my opinion.

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.