我想将所选复选框中的所有值放入其自己的PHP变量中。这些复选框的值由PHP / SQL生成。必须在下拉框下方的简单<div>
中显示相同的变量。我也在使用Bootstrap Multiselect插件。但我从null
测试中得到var_dump()
值。
这是我到目前为止所尝试的内容:
<form id="menu1" method="POST" action="index.php">
<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 var_dump($result) ?></div>
然而,我可以通过使用jQuery完成我想要做的事情,但我希望能够将这些值发送到数据库,我的导师希望它们在PHP变量中。我知道我可以使用AJAX将它们发送到我的数据库,但这是我的导师想要的。我不知道我是否走在正确的轨道上,所以非常感谢任何帮助
答案 0 :(得分:1)
在第一次运行时,您不会从该POST变量中读取任何内容,因此请对其进行处理。 试试这段代码,它应该可以工作:
<form id="menu1" method="POST">
<h2>Area Code</h2>
<select id="multi-select1" name="multi_select1[]" multiple="multiple" action="index.php">
<?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) { ?>
<option class="menuoption1" name="menuAreaCode" value="<?php echo $areaCodeResult['AreaCode'] ?>" ><?php echo $areaCodeResult['AreaCode'] ." ". $areaCodeResult['AreaName']; ?></option>
<?php } ?>
</select>
<input type="submit" value="Send" />
</form>
<div id="showResults1">
<?php
$result = isset($_POST['multi_select1']) ? $_POST['multi_select1'] : [];
var_dump($result);
?>
</div>