dennismonsewicz Posted November 24, 2008 Share Posted November 24, 2008 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? Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 24, 2008 Share Posted November 24, 2008 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"; } Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted November 24, 2008 Author Share Posted November 24, 2008 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? Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 24, 2008 Share Posted November 24, 2008 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. 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.