php - Variable not being passed to second select dropdown

436

I have two dropdowns, one displays States from HTML SELECT, and the next one populates from MYSQL query of markets matched to the state selected. After selection of the Market, the SID should appear. The problem is the code remembering the $pickstate var when you select the $market var so Mysql query can display the SID result.

<?php
// Get State from above form.
  $pickstate = $_POST['state'];
$result = mysql_query("SELECT Market FROM ppc WHERE State='" . $pickstate . "'");

// After State is selected from MySQL, populate Market Dropdown.
echo '<form action="" method="POST"><select id="Market" name="Market" onchange="this.form.submit();">';
echo '<option value="">Select Your Market</option>';
while ($row = mysql_fetch_array($result)) {
  echo ( '<option value= "' .$row['Market']. '">'.$row['Market'].'</option>' );
}
echo "</select></form><br />";

// Get SID Result
$market = $_POST['Market'];
$sid = mysql_query("SELECT SID FROM ppc WHERE State='" . $pickstate . "' AND Market='" . $market . "'");

// This is for debugging only.  
// This shows, until market is selected.. then vanishes.
echo $pickstate . "<br />"; 
// This shows after market has been chosen. 
echo $market . "<br />"; 


// "SHOULD" Display SID.
if ($state != null && $market != null) {
echo '<p >&nbsp;&nbsp; Use: ';
}
while ($row = mysql_fetch_array($sid)) {
  echo $row['SID'] . ' ';
  }
?>
773

Answer

Solution:

it will not work like that .

the query is server side . you may consider to send your selected value to another page and then get values from your query

OR you can use ajax .

People are also looking for solutions to the problem: PHP Session start on button click

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.