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
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";
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

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.