变量未传递给第二个选择下拉列表

时间:2014-02-04 15:09:49

标签: php mysql select

我有两个下拉列表,一个显示来自HTML SELECT的状态,下一个下拉列表来自与所选状态匹配的市场的MYSQL查询。选择市场后,应出现SID。问题是当您选择$ market var时,代码会记住$ pickstate var,因此Mysql查询可以显示SID结果。

<?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 style="display:inline-block;" 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 style="display:inline-block;margin:0;padding:0;">&nbsp;&nbsp; Use: ';
}
while ($row = mysql_fetch_array($sid)) {
    echo $row['SID'] . '  ';
    }
?>

1 个答案:

答案 0 :(得分:0)

它不会像那样工作。

查询是服务器端。您可以考虑将所选值发送到另一个页面,然后从查询中获取值

或者你可以使用ajax。