I want to put all values from selected checkboxes into its own PHP variable. These checkboxes have their values generated by PHP/SQL. This same variable has to be shown in a simple <div>
below the dropdown boxes. I am also using Bootstrap Multiselect Plugin. But I get a null
value from my var_dump()
test.
here is what I have attempted so far:
<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>
I can however accomplish what I want to do by using jQuery but I want to be able to send those values to a database and my tutor wants them to be in PHP variables. I know I can use AJAX to send them to my database but this is what my tutor wants instead. I don't know if I am on the right track, so any help is greatly appreciated
At the very first run you wont read anything from that POST variable, so handle it. Try this code, it should work:
<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>