我希望从我的数据库生成的下拉菜单中获取选定的值(可以选择多个选项)并将它们存储到PhP变量中。然后我希望将该变量的内容显示在下面的简单div元素中 到目前为止,这是我的代码,在我的简单div中没有任何内容:
<form id="menu1" method="POST">
<h2>Area Code</h2>
<select id="multi-select1" name="multi_select1" multiple="multiple">
<?php
//The query asking from our database
$areaCodeSQL = "SELECT ac.Number AS `AreaCode`, ac.Name AS `AreaName`
FROM `AreaCodes` ac"; //SQL query: From the table 'AreaCodes' select 'Number' and put into 'AreaCode', select Name and put into 'AreaName'
$areaCodeResults = $conn->query($areaCodeSQL); // put results of SQL query into this variable
if ($areaCodeResults->num_rows > 0) { // if num_rows(from $results) is greater than 0, then do this:
// output data of each row
foreach($areaCodeResults as $areaCodeResult) //for each item in $areCodeResults do this:
{
$areaNameAndCode = $areaCodeResult['AreaCode'] ." ". $areaCodeResult['AreaName']; //get AreaCode and AreaName from query result and concat them
$areaName = $areaCodeResult['AreaName']; // get AreaName
$areaCode = $areaCodeResult['AreaCode']; //get AreaCode
?><option class="menuoption1" name="menuAreaCode" value="<?php echo $areaCode ?>" ><?php echo $areaNameAndCode; ?></option><?php //Create this option element populated with query result variables
}
}
$result = $_POST['multi_select1'];
?>
</select>
</form>
<div id="showResults1"><?php echo $result ?></div>
在线查看建议我可能需要使用AJAX和jQuery,但我的导师伙伴坚持认为这可以在这个脚本中完成。但是我不知道为什么我的尝试不起作用,有人能指出我正确的方向吗???? : - )