将选中的复选框放入 PHP 变量中,然后我可以直接在下面查看
Getting selected checkboxes into a PHP variable, that I can then view directly below
我想将选定复选框的所有值放入它自己的 PHP 变量中。这些复选框的值由 PHP/SQL 生成。同样的变量必须显示在下拉框下方的简单 <div>
中。我也在使用 Bootstrap 多选插件。但是我从 var_dump()
测试中得到了 null
值。
这是我目前尝试过的:
<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 将它们发送到我的数据库,但这是我的导师想要的。我不知道我是否在正确的轨道上,所以非常感谢任何帮助
在第一个 运行 你不会从那个 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>
我想将选定复选框的所有值放入它自己的 PHP 变量中。这些复选框的值由 PHP/SQL 生成。同样的变量必须显示在下拉框下方的简单 <div>
中。我也在使用 Bootstrap 多选插件。但是我从 var_dump()
测试中得到了 null
值。
这是我目前尝试过的:
<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 将它们发送到我的数据库,但这是我的导师想要的。我不知道我是否在正确的轨道上,所以非常感谢任何帮助
在第一个 运行 你不会从那个 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>