Jump to content

Recommended Posts

what I am trying to accomplish is for the user to select the state they wish, hit submit. At this point another form should show asking which county based on the state they picked, then hit search.

 

At this point, I'm having an issue having the state variable being passed to the 2nd form also having the state they selected stay selected.

 

<?php
$default = "Step 1. Pick Your State";
$select = "<option name='statebox'>$default</option>";
  echo "<br/><form method='POST' action=".$_SERVER['PHP_SELF']." >";
  		echo "<select name='search'>";
	echo "$select";
	///////////////////////////////////////////////////////////////
	//Connect to the database
	include_once 'phpforms/connect.php';

	$sql = mysql_query("SELECT * FROM states");
	while($row = mysql_fetch_array($sql)){
	$state = $row['states'];
		echo "<option name='statebox'>$state</option>";
	}//End While
	///////////////////////////////////////////////////////////////
        echo "</select>";
        echo "<input align='left' type='submit' name='stateboxbutton' value='Ok'> ";
  echo "</form>";
  
	echo "</td>";
    	echo "<td>";

if(isset($_POST['stateboxbutton'])){
$statesearch = $_POST['statebox'];
$selected = $_POST['statebox'];
$select = "<option name='statebox'>$selected</option>";

  echo "<br/><form action='../search-results.php' method='POST'>";
  		echo "<select name='search'>";
		echo "<option name='default'>Step 2. Pick Your County</option>";

	///////////////////////////////////////////////////////////////
	//Connect to the database
	include_once 'phpforms/connect.php';
	$sql = mysql_query("SELECT * FROM counties WHERE state LIKE '$statesearch'");
	while($row = mysql_fetch_array($sql)){
	$co = $row['counties'];
            echo "<option name='county'>$co</option>";
	}//End While
	///////////////////////////////////////////////////////////////

        echo "</select>";
        echo "<input align='left' type='submit' name='button' value='Search'> ";
  echo "</form>";

}else{
}//End Else
  	
?>

 

Link to comment
https://forums.phpfreaks.com/topic/210344-post-form-same-page-define-variables/
Share on other sites

When using drop down lists you only give the <select> tag a name. But for each <option> you'll assign their value. Example dropdown list

<form action="" method="post">
<select name="list_name">
   <option value="option1">Option One</option>
   <option value="option2">Option Two</option>
   <option value="option3">Option Three</option>
</select>
<input type="submit" name="submit" value="Submit" />
</form>

Notice how each option has a unique value assigned.

Now when the form is submitted $_POST['list_name'] will contain the selected value.

 

So to correct your code, change

		while($row = mysql_fetch_array($sql)){
	$state = $row['states'];
		echo "<option name='statebox'>$state</option>";
	}//End While

To

		while($row = mysql_fetch_array($sql)){
	$state = $row['states'];
		echo "<option value=\"$state\">$state</option>";
	}//End While

 

Next change $statesearch = $_POST['statebox']; to $statesearch = $_POST['search'];

The next two lines can be remove, they are not necessary.

	$selected = $_POST['statebox'];
$select = "<option name='statebox'>$selected</option>";

 

On your second form you'll want to pass the selected state as a hidden value

        echo "</select>";
        echo "<input type='hidden' name='statesearch' value='$statesearch'> ";
        echo "<input align='left' type='submit' name='button' value='Search'> ";

Made all of your changes, works great thanks!

 

One question, I'd like to understand why you used the slashes

echo "<option value=\"$state\">$state</option>";

 

And one more thing, How do I retain the state I selected in the first dropdown after the submit button has been pressed?

Sure, Change the states while loop to

		while($row = mysql_fetch_array($sql))
	{
	    $state = $row['states'];
	    $selected = (isset($_POST['search']) && $_POST['search'] == $state) ? ' selected="selected"' : '';
		echo "<option value=\"$state\"$selected>$state</option>";
	}//End While

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.