I have a table with data and checkboxes aside. When I click in checkboxes I am taking the id of the item of each selected and when submit I am receiving in PHP BUT now I changed my approach. Now, I want to submit the entire array to PHP however I don't receive it:
<?php foreach ($arr_cases as $cases) : ?>
<tr>
<td>
<input type="checkbox" name="devices" value="<?php $dataa = serialize($cases); $encodedd = htmlentities($dataa); echo $encodedd; ?>">
</td>
</tr>
<?php endforeach; ?>
The way I am doing above, I can ONLY receive ONE array in PHP, so, tried naming the checkboxes from devices to devices[] but this time its printing blank(nothing).
I, of course, unserialize it in PHP.
What am I doing wrong here?
You need to use an array-style name for the checkbox so you receive all of them in an array.
<input type="checkbox" name="devices[]" value="<?php $dataa = serialize($cases); $encodedd = htmlentities($dataa); echo $encodedd; ?>">
Then $_POST['devices'] will be an array of all the checked values. Each element of this array will be serialized, and you'll have to unserialize it.
if (!empty($_POST['devices'])) {
$devices = array_map('unserialize', $_POST['devices']);
} else {
$devices = [];
}
var_dump($devices);
Related
I am currently displaying a table with checkbox and the checkbox is displayed using while loop. For the value of each checkbox, I used value = "$key" . The problem that I am currently having now is, how can I post the value of the checkbox to another php page ?`
<?php $sm_sql = "SELECT * FROM submodule WHERE mod_id = '1'";
$sm_res = DB_Query($sm_sql);
while($sm_row = DB_FetchRow($sm_res)) {?>
<tr> <?php
?> <td><div align="center"> <?php echo $sm_row["submod_name"];
?></div>
</td>
<!-- key 1 = submod_create_id -->
<td> <div align="center">
<?php
$fi_sql = mysql_query("SELECT * FROM finance_controlling WHERE staff_id = '".$STAFF_ID."'");
<?php
$fi_sql = mysql_query("SELECT * FROM finance_controlling WHERE staff_id = '".$STAFF_ID."'");
while($row = mysql_fetch_assoc($fi_sql))
{
foreach($row as $key => $value)
{
if($key == $sm_row["submod_create_id"])
{
if($value == '1')
{ ?><input type=checkbox checked="checked" name="$key" id="$key"> <?php
print $key; //it prints out the correct key
} else { ?><input type=checkbox name="$key" id="$key"> <?php
print $key;
}
}
}
}
?>
Above are my checkbox in Module_Form.php.
Below are Summary.php page where the value of the checkbox should be displayed on.
if ($_POST["Submit"] == "Review Risk")
{
$a = $_POST['fi_pp_c']; echo $a;
}?>
The problem now is that the checkbox value are not passed to another page. Ive tried json_encode but there are still no value displayed.
There are a few thing I feel should be addressed here.
First up: Prepared statements
Having raw SQL in your PHP code which just inserts variables inside some quotes is dangerous. It might be ok for your current use case, but in future if this changes, it is all too easy for a developer to just make $STAFF_ID a $_POST['varName'], which immediately introduces SQL injection.
Please take some time to read up on prepared statements and stored procedures in SQL/PHP: http://php.net/manual/en/pdo.prepared-statements.php
Second up: HTML attribute values
This is ok and it is valid HTML, however, I'd suggest it is good practice to include all attribute values in quotes, so that if there are spaces in values, they are correctly interpreted. This is clearly not going to happen with the type attribute but it does make code easier to read and to process using more primitative parsers, if it were:
<input type="checkbox" checked="checked" name="<?= $key ?>" id="<?= $key ?>">
As to the original question: have you enabled debugging? Primatively you can do this by adding error_reporting(E_ALL); to the top of your PHP file.
The issue I suspect you are facing is that is the checkbox is not checked, then there is no value for the key with the same name passed in the HTTP POST request.
For example, if you have:
<input type="checkbox" name="myCheckbox" />
And that checkbox is unchecked when the form is submitted, then in PHP $_POST['myCheckbox'] is null.
So the solution is to check whether these fields are null, e.g.:
if(isset($_POST['myCheckbox'])) {
$a = $_POST['myCheckbox'];
echo $a;
}
I generally think this is a gap in protocol, and that unchecked checkboxes should be submitted in forms with a value of 0, but ho hum!
In your code :
?><input type=checkbox checked="checked" name="$key" id="$key"> <?php
you closed your PHP tag and reopened it after the HTML.
You need to reopen it to print your $key in the name and id parameters. As it is, it`s only treated as text. The browser just thinks that the id and name attributes are "$key".
?><input type=checkbox checked="checked" name="<?= $key ?>" id="<?= $key ?>"> <?php
and here :
} else { ?><input type=checkbox name="<?=$key ?>" id="<?= $key ?>"> <?php
This would solve your current problem, as long as the "name" of the checkbox is "fi_pp_c" otherwise, it will be stored in whatever is named the checkbox.
You can just var_dump($_POST); to check where you data is stored if you still have issues.
In my html I have this,
<tr>
<td class="grid_cell" width="5%">
<input type="checkbox" name="workspace_trees_rpt_target" id="<?php echo $t["tree_id"];?>" value="<?php echo $t["tree_id"];?>" />
</td>
</tr>
this are inside a loop so what will be displayed is a lot of checkboxes with different values. And in my script I have this,
if(confirm("Delete cannot be undone, click OK button to proceed.")) {
document.forms[0].method="POST";
document.forms[0].action="delete.php";
document.forms[0].submit();
}
after selecting two of the checkboxes and click the OK button, I then go to delete.php to print_r my post data. But when the result of print_r is displayed it only showed one value for the checked checkboxes, but I was expecting two of them. How can I make it such that when I check multiple checkboxes, the post data for the checkboxes will be an array of values of the checked checkboxes?
you must set the name of the inputs as array:
name="workspace_trees_rpt_target[]"
You have given the same name to all of your checkboxes. So only one value can be returned.
Change to
<input type="checkbox" name="workspace_trees_rpt_target[]" id="<?php echo $t["tree_id"];?>" value="<?php echo $t["tree_id"];?>" />
Then in your PHP code you will get and array of $_POST['workspace_trees_rpt_target'][]
So process it like this:
if ( isset( $_POST['workspace_trees_rpt_target'] ) ) {
// at least one checkbox has been ticked
foreach ( $_POST['workspace_trees_rpt_target'] as $checkboxe ) {
// do whatever $checkbox it will be set to the value="" that you set earlier
}
}
I've got this problem that I can't solve. Partly because I can't explain it with the right terms. I'm new to this so sorry for this clumsy question.
Below you can see an overview of my goal. I am displaying my check boxes in a for loop.
Here I am getting the all values in an array, but I want store the array elements based on the check box checked.
<?php
$j=0;
$arr = Array();
foreach($collection as $data) {
$mageid=$data['mageproductid'];
$products = Mage::getModel('catalog/product')->load($mageid);
$productMediaConfig = Mage::getModel('catalog/product_media_config');
$checkeven=0;
$arr[$j]=$products->getId();
//echo $arr[$j];
$j++;
} ?>
My checkbox code:
<form id="check_all" action="" method="POST" name="check" >
<input type="checkbox" class="multid[]" id="<?php echo $products->getId();?>" value="checked" /> </form>
What do I have to do in order to get checked values in my array? Did I do anything wrong?
Use input name attribute
<input type="checkbox" name="multid[<?php echo $products->getId();?>]" value="checked" />
No in you php code, check if multid[yourProductId] is set, and store them if it is set.
<?php
$j=0;
$arr = Array();
foreach($collection as $data) {
$mageid=$data['mageproductid'];
$products = Mage::getModel('catalog/product')->load($mageid);
$productMediaConfig = Mage::getModel('catalog/product_media_config');
$checkeven=0;
$arr[$j]=$products->getId();
if(!empty($_GET['multid['.$arr[$j].']']))
its checked, do something.
//echo $arr[$j];
$j++;
} ?>
After submiting the form you can get an array of checked products with $_POST['multid']
Can you use javascript? Try this one.
HTML:
<input type="hidden" id="hdnCheckedIDs" value="" />
Before submit, on submit button's client click, Javascript:
var CheckedIDs = "";
for each checkbox
if(document.getelementbyid('multid1').checked)
CheckedIDs = CheckedIDs + document.getelementbyid('multid1').id;
document.getelementbyid('checkboxID') = CheckedIDs;
In PHP, you can use this comma seperated string $_POST['hdnCheckedIDs'] to get the IDs of checked chekboxes.
I have a form with rows which are populated from a table. Each row has a "checkbox" which the user can check or not.
When the form is submitted I want to be able to read which checkbox have been selected and insert the result in to a data table.
My code so far
FORM:
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
<table
<?php do { ?>
<tr>
<td>input type="text" name="InspectRoomNo" value="<?php print $row_InspectItems['AuditItemNo']; ?>"></td>
<td>php echo $row_InspectItems['AuditItem']; ?>td>
<td>input name="check[]" type="checkbox" ></td>
</tr>
<?php } while ($row_InspectItems = mysql_fetch_assoc($InspectItems)); ?>
<input type="submit" value="Insert record">
</table>
The insert: fetchs $Items from table
while($row = mysql_fetch_assoc($Items))
{
$array[] = $row['AuditItem'];
}
foreach($array as $id) {
$AuditItemID = mysql_real_escape_string($id);
if(isset($_POST['check'])){
$Checked = mysql_real_escape_string($_POST['check'][$row]);
}
}
The problem I am having is the returned values for all the checkbox is true, even if a checkbox was not selected.
Can anyone help me sort this issue.
Many thanks.
Do it like this:
if(!empty($_POST['check'])) {
foreach($_POST['check'] as $check) {
echo $check;
}
}
You should put the item id inside the checkbox name:
<td><input name="check[<?= $row_InspectItems['AuditItem']; ?>]" type="checkbox" /></td>
Then, you can simply iterate over it:
foreach ($_POST['check'] as $id => $value) {
// do stuff with your database
}
I'm assuming than whomever runs this script is trusted, because it would be easy to forge the list of ids; make sure the current user has permissions to update those records.
What is happening, is that only selected checkboxes get sent to the server, so you will see that your $_POST['check'] array (this is an array!) is smaller than the number of items you have displayed on the screen.
You should add your ID's so that you know what checkboxes got checked and adapt your php processing code to handle an array instead of a single value.
You are also overwriting your InspectRoomNo every row, so you should use an array there as well.
The form side would look something like:
<td><input type="text" name="InspectRoomNo[<?php echo row_InspectItems['AuditItemNo']; ?>]" value="<?php print row_InspectItems['AuditItemNo']; ?>"></td>
<td><?php echo $row_InspectItems['AuditItem']; ?></td>
<td><input name="check[<?php echo row_InspectItems['AuditItemNo']; ?>]" type="checkbox" ></td>
<?php
// code that connects to database
?>
<table>
<form method="get" action="processorder.php">
<?php
while (list($pizzaId, $pizzaName, $pizzaNumber, $pizzaPrice) = mysql_fetch_row($resultaat))
{
echo "<tr>
<td>".$pizzaName."</td>
<td>".$pizzaNumber."</td>
<td>".$pizzaPrice."</td>
<td> <input type='text' name='$pizzaId' value='$qty' size='3' /></td>
</tr>";
}
mysql_close($db);
?>
<input type="submit" value="Order now" />
I would like to display the pizzas of where there is a value in the input element.
the processorder.php file would look like:
my url shows the pizzaId's with the values after the '='. So I figured I have an associative array on my hands.
I thought I'd put a foreach loop in my processorder.php going like
foreach ($_GET['pizzaId'] as $pizza => $qty)
{
echo $pizza." ".$qty."<br />";
}
Yet, when I use the foreach loop, the error in my browser says that the argument of my foreach loop is invalid because $_GET['pizzaId'] isn't an array to begin with (I checked with is_array).
So how do I get access to those values in my value attribute of the input element?
Your $pizzaId is an integer.
I would change the name of the input elements to name="pizzas[$pizzaId]", and then you could access it through PHP like this:
foreach ($_GET["pizzas"] as $pizzaId => $pizzaQty) {
echo "$pizzaId $pizzaQty<br />";
}
with this method, instead of just plain name="pizzas[]", you also retain the association with the actual pizzaId.
If you need to put an array into $_GET then your url should look like:
//site/page?pizzaID[]=123&pizzaID[]=124
You can achive this with similar usage of name of input field
<input name="pizzaID[]" />
<input name="pizzaID[]" />