我的页面中有以下下拉列表
<label for="state">State: </label>
<select name="state" class="dropdown">
<option value="" selected>Please Select</option>
<?php echo $states->getStates(); ?>
</select>
下拉列表本身可以正常工作并填充状态没有问题。但现在我希望它能够选择一个州并根据该州的状态显示城市下降与该州相应的城市。
以下是我的课程。我只是无法弄清楚如何调用myclass的getCity函数并从状态下拉列表中传递选定的值。
class states{
function getStates(){
$success = false;
try{
$con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "SELECT DISTINCT(state) FROM tbl_zip ORDER BY state ASC";
$stmt = $con->prepare( $sql );
$stmt->execute();
$valid = $stmt->fetchAll();
if( $valid ) {
$success = true;
$list = "";
// Loop through list of states
foreach($valid as $row) {
$list .= '<option value=\"' . $row["state"] .'\">' . $row["state"] . '</option>';
}
}
$con = null;
return $list;
}catch (PDOException $e) {
echo $e->getMessage();
return $success;
}
}
function getCity($state){
$success = false;
try{
$con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "SELECT DISTINCT city FROM tbl_zip WHERE state = :state ORDER BY city ASC";
$stmt = $con->prepare( $sql );
$stmt->bindValue( "state", $state , PDO::PARAM_STR );
$stmt->execute();
$valid = $stmt->fetchAll();
if( $valid ) {
$success = true;
$list = '<select name=\"state\">';
$list .= '<option value=\"\" selected>Please Select</option>';
// Loop through list of states
foreach($valid as $row) {
$list .= '<option value=\"' . $row["city"] .'\">' . $row["city"] . '</option>';
}
$list .= '</select>';
}
$con = null;
return $list;
}catch (PDOException $e) {
echo $e->getMessage();
return $success;
}
}
}
答案 0 :(得分:0)
不确定,究竟是什么让你烦恼,但是这个怎么样:
$city = $states->getCity($_POST['state']);