i have send the value dynamically from check-box and when i tried to retrieve all the value dynamically using loop, it just goes on loading.
my code for sending value from check-box:
while ($row=mysql_fetch_array($query))
{
$member_id=$row["member_id"];
<input type='checkbox' name='check' value='$member_id'>
}
// this is working. but when i try to fetch the data from checkbox from only where the tick is given it doesn't work. this is how i tried to fetch the data
while(isset($_POST['check']))
{
echo $_POST['check']."<br>";
}
The trick here is, when you have multiple checkboxes with the same name and you want to get all the checked values on the server side, then you need to add [] after the name of the checkbox field in the html, eg.
<input type='checkbox' name='check[]' value='$member_id'>
if you do this, then $_POST['check'] will be an array of all the checked elements. As pointed out by others,
while(isset($_POST['check']))
represents an infinite loop. It should be
if(isset($_POST['check']))
foreach($_POST['check'] as $each_check)
echo $each_check;
And finally, its a duplicate of an existing question. Please search before asking again :)
You added While loop, with the condition that will always true. So loop will become infinite.
Change you loop to foreach, like this
foreach ($_POST['check'] as $value)
{
echo $value."<br>";
}
AND your check-boxes will not show till then you add echo, like this
while ($row=mysql_fetch_array($query))
{
$member_id=$row["member_id"];
echo "<input type='checkbox' name='check' value='$member_id'>";
}
if you want to get all the checkboxes
WRONG WILL CAUSE INFINITE LOOP
while(isset($_POST['check']))
{
echo $_POST['check']."<br>";
}
One of the many correct alternatives:
foreach ($_POST['check'] as $val) {
echo $val.'<br>';
}
foreach ($_POST['check'] as $selected) {
$selections[] = $selected;
}
print_r($selections);
and change your html tag as :
<input type="checkbox" name="check[]" value=".$member_id.">
Related
I am trying to send data from multiple checkboxes (id[]) and create an array "info" in php to allow me to run a script for each value (however the quantity of values may change each time) however first I am trying to display the content of each array value. I am not quite sure how to put my array populating line to save all the content to the array.
HTML
echo("<input name='id[]' type='checkbox' value='".$shopnumb."'>");
my hopeful processing code currently is -
$info=$_POST['id[]'];
Echo(array_values($info));
what do I need to do to make the content sent by post from the form checkboxes populate the array info
any help is greatly appreciated
edited for clarification.
Change
$info=$_POST['id[]'];
to
$info=$_POST['id'];
by adding [] to the end of your form field names, PHP will automatically convert these variables into arrays.
You should get the array like in $_POST['id']. So you should be able to do this:
foreach ($_POST['id'] as $key => $value) {
echo $value . "<br />";
}
Input names should be same:
<input name='id[]' type='checkbox' value='1'>
<input name='id[]' type='checkbox' value='2'>
...
On the form page, field names must look like this
<input name="id[]" type="checkbox" value="x">
<input name="id[]" type="checkbox" value="y">
<input name="id[]" type="checkbox" value="z">
On the destination page, $_POST['id'] is your array variable
$id = implode(",", $_POST['id']);
echo $id; //Should print "1,2,3"
You cannot echo an array directly, because it will just print out "Array". If you wanna print out the array values use print_r.
print_r($_POST['id']);
I don't know if I understand your question, but maybe:
foreach ($_POST as $id=>$value)
if (strncmp($id,'id[',3) $info[rtrim(ltrim($id,'id['),']')]=$_POST[$id];
would help
That is if you really want to have a different name (id[key]) on each checkbox of the html form (not very efficient). If not you can just name them all the same, i.e. 'id' and iterate on the (selected) values of the array, like: foreach ($_POST['id'] as $key=>$value)...
I dont know how to manage this situation, I'm a noob coder, I have a page that shows you all available lots where you can unload a specific item from that lot.
This is the foreach that prints out:
$lotto, $totalelotto, $data, and ask for qtyvalue to unload from $lotto (input can be also NULL)
foreach ($dataslotto as $data) {
$totalelotto = totlotto($database, $data['lotto']);
$lotto = $data["lotto"];
$data = $data["data"];
echo "<tr>";
echo "<td>".$lotto."</td>";
echo "<input type=\"hidden\" value=\"".$lotto."\" name=\"array[]\" />";
echo "<td>".$totalelotto."</td>";
echo "<input type=\"hidden\" value=\"".$totalelotto."\" name=\"array[]\" />";
echo "<td>".$data."</td>";
echo "<input type=\"hidden\" value=\"".$data."\" name=\"array[]\" />";
echo "<td><input type=\"text\" class=\"form-control\" placeholder=\"Qta.\" required name=\"qtyvalue\"></td>";
echo "</tr>";
}
I dont know how to set name="" of input fields (because the number of fields can change if there are many lots) and I dont know how to send $_POST data as array, and then foreach group of $lotto, $totalelotto, $data, $qtyvalue where is set $qtyvalue do another query.
I put it in no regular code, I know it looks bad but it's just for giving you an idea.
$_POST[''formarray];
foreach ( /* values recieved in each <tr> inside formarray where $_POST['qtyvalue'] is not empty */ ){
#EXECUTE THIS
}
Thanks for help!!
And sorry for my bad coding skills.
$_POST is an Associative/Key Value pair, it's key is whatever is set as the inputs name.
so if you wanted to send the users input username to the backend PHP script
You set the value and it's name
<input type="text" name="username" value="User123">
then to retrieve the user name you can do
print_r($_POST["username"]);
to print the value.
So you ask how do you loop over each one in $_POST, that's pretty simple you can could a foreach loop over the entire $_POST array.
foreach($_POST as $key => $value)
{
//check something has been entered for the current value we are iterating over
if($value != null)
{
print_r($key . " value is : " . $value);
}
}
this would loop over each item in the $_POST array with key being set to whatever the DOM elements name is.
Don't forget the $_POST array is just that an array, you could do
var_dump($_POST);
and see everything that was sent in the POST request.
You can use array names for your inputs. Say you have a row of your table like this:
<tr>
<td><input ... name="lotto[]"></td>
<td><input ... name="totalelotto[]"></td>
<td><input ... name="data[]"></td>
<td><input ... name="qtyvalue[]"></td>
</tr>
Then you will get arrays $_POST['lotto'], $_POST['totalelotto'] and so on, each with the same number of elements, and elements with same index belonging to one row of the table. You could then process those elements like this
foreach ($_POST['lotto'] as $i=>$lotto) {
if ($_POST['qtyvalue'][$i] > 0) {
...
}
}
After a SQL request, I obtain a form which contains a list of data, with checkboxes at the end of each line. The goal is the following. If the user checks some of them, the line will be deleted in the database when the form will be submitted.
I name my checkboxes like this, with my SQL request results, so my Php script could find the line to delete:
<input type="checkbox" name="chk[<?echo '/'.'$t[datetr]'.'/'.'$t[beneficiaire]'.'/'.'$t[objet]'.'/'.'$t[montant]';?>]">
My goal is to get all the checkboxes values with $_POST to my Php script. But even with this...
foreach ($_POST as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
...my php script does not seem to get the checkboxes values... Did I do something wrong ? Thanks for help.
Aside from the other (entirely accurate) comments about unchecked boxes not being passed with the HTTP request - you have a couple of issues with your actual PHP.
<?echo '/'.'$t[datetr]'.'/'.'$t[beneficiaire]'.'/'.'$t[objet]'.'/'.'$t[montant]';?>
Firstly, your echo is wrong; it should probably be <?php echo or <?= rather than <?echo (although that might work with short start tags enabled).
Secondly, Apostrophes in PHP are non-interpolated string literals (i.e. '$t[objet]' will literally be treated as the string '$t[objet]' not the variable).
Finally, assuming $t is an array, your associative indexes need to have quotes or they'll be interpreted as constants - which is likely to throw an error.
I think what you want could be written as:
<?= "/{$t['datetr']}/{$t['beneficiaire']}/{$t['objet']}/{$t['montant']}"; ?>
Once you've sorted that out, the $_POST['chk'] data should be set properly and it'll be an associative array as you're expecting.
Then a foreach($_POST['chk'] as $key => $value) { ... } loop should work... though, of course none of your inputs actually have values at the moment.
When using <input type="checkbox" name="foo" value="42"/>, the variable foo=42 is sent only if the checkbox is checked. When the box is not checked, nothing is sent at all.
If you need some 0/1 information, i suggest you use either a <select> or a couple of <input type="radio"> tags instead:
<input type="radio" name="foo" value="1"/> Yes
<input type="radio" name="foo" value="0"/> No
Blank checkboxes do not get posted to the receiving script, only checked ones do. To get around this have a corresponding set of hidden fields, and set their values to something like "ON" or "OFF" depending on how the checkboxes are clicked. You will probably want to use the onClick event, determine of the box is clicked or not then set the appropriate hidden field, i.e.
Checkbox_One Hidden_One
Checkbox_Two Hidden_Two etc.
When you post the form, have your script ignore the checkboxes, and just process the hidden fields.
Something to remember when doing these multi-checkboxes is that when you submit, it will be interpreted in PHP as an array, in your case $_POST['chk'] will be an array of checked checkboxes.
However, you should also make sure you give the checkboxes a value, even if just a 1.
When handling your POST, initially just try using a var_dump($_POST); die(); to see what the data looks like.
use the code below:
foreach ($_POST['chk'] as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
If you name all your checkboxes like this name="boxArray[]" it will create an array named $_POST["boxArray"] when the form is submitted.
Then you can do your foreach loop to display the values:
foreach ($_POST["boxArray"] as $item) {
echo "<tr>";
echo "<td>";
echo $item;
echo "</td>";
echo "</tr>";
}
To add to this, if you want to delete only the items checked then for each checkbox assign the record's ID as its value:
<input type="checkbox" name="boxArray[]" value="RECORD ID">
Now when you run your foreach loop only the checked boxes will post values so you change the code to delete each item in the array:
foreach ($_POST["boxArray"] as $item) {
//SQL TO DELETE RECORD WHERE ID = $item;
}
I've tried to get those checkbox that are unchecked. At this moment i'm just getting those that are checked. Here is my code. That value will be inserted on a table which i don't want to leave it null, that's why i need to get those checkbox unchecked, so that i could insert 1 for checked or 0 for unchecked:
<?php
if (!empty($_POST['menu'])) {
# code...
foreach ($_POST['menu'] as $value) {
# code...
echo "<p>ID Checkbox checked: ".$value;
}
}
?>
One of the reasons for what i need to get both status: checked or unchecked is because i don't want to leave database fields empty.
Unchecked checkboxes don't get POSTed. If you know what fields should be there, you'll probably have to list them out manually.
The wonderful thing about checkboxes is that when you don't have one checked, and you submit a form, nothing is sent to the server. Take this checkbox for example:
<input type="checkbox" name="test"/>
If you left it unchecked and looked for $_POST['test'] it would error out, as it is not set.
So, try doing this:
if(!isset($_POST['name_of_checkbox'])){
// Fill database with some empty value.
} else {
// Do what you need to do for checked value.
}
Hope that gives you some insight!
Say if they are named the same, and you know the number of checkbox you can use a for loop:
if (!empty($_POST['menu'])) {
for ($i=0; $i < 10; $i++) {
if(isset($_POST['menu'][$i])){
echo "Checkbox checked: " . $_POST['menu'][$i];
}else{
echo "Checbox uncheck #" . $i;
}
}
}
You can put the names in an array, then iterate:
$checkboxes = array('cbx1', 'cbx2', 'cbx3');
foreach ($checkboxes as $checkbox) {
if (isset($_POST[$checkbox])) {
echo "<p>ID Checkbox checked: " . $_POST[$checkbox];
} else {
echo "Checbox uncheck :" . $checkbox;
}
}
So yeah many ways to achieve this, it depends on the situation.
Check for #Artur approach as well for client side solution.
<input type="hidden" name="checkbox[8]" value="0">
<input type="checkbox" name="checkbox[8]" value="8">
This would be one way to also get 0's posted. Important part is that the name's are the same. So if checkbox is checked, it's value would override the hidden input value.
Thank you all for your time and be so kind to answer my question. The reason for what i needed to have those checkboxes unchecked and checked is because i have a dynamic menu and so i assign menus to users. Here is the solution i found with a friend of mine at work:
Code for the checkboxes. I query all the menus available on my table Menus so that i will show the administrator all the menus availables so then he could choose which menus he will assign to the users:
´
<table>
<tr>
<td>Menus a asignar:</td>
<td>
<?php
$query_menus_checkbox = mysql_query("SELECT id_menu, nombre_menu FROM menus");
while ($checkbox_mostrar = mysql_fetch_row($query_menus_checkbox)) {
# code...
?>
<input type="checkbox" name="menus[<?php echo $checkbox_mostrar[0]; ?>]" value="<?php echo $checkbox_mostrar[0] ?>"><?php echo $checkbox_mostrar[1] ?>
<p></p>
<?php
}
?>
</td>
</tr>
</table>
´
Then here is the code to process which checkboxes are checked or not to insert on my table (id_user is not shown but i have taken from another query which is not shown so you'll have to query yourself):
´
$res=mysql_query("select id_menu from menus");
$arid=array();
while($xd=mysql_fetch_assoc($res)){
$arid[]=$xd['id_menu'];
}
for($i=0;$i<count($arid);$i++){
$id_menu=$arid[$i];
$activo=(isset($_POST['menus'][$id_menu]) && $_POST['menus'][$id_menu]!="")?1:0;
$inserta_menus = mysql_query("INSERT INTO menus_usuarios(id_menu, id_usuario, estado) values ('$id_menu', '$id_user[0]', '$activo')");
}
´
I am trying to send data from multiple checkboxes (id[]) and create an array "info" in php to allow me to run a script for each value (however the quantity of values may change each time) however first I am trying to display the content of each array value. I am not quite sure how to put my array populating line to save all the content to the array.
HTML
echo("<input name='id[]' type='checkbox' value='".$shopnumb."'>");
my hopeful processing code currently is -
$info=$_POST['id[]'];
Echo(array_values($info));
what do I need to do to make the content sent by post from the form checkboxes populate the array info
any help is greatly appreciated
edited for clarification.
Change
$info=$_POST['id[]'];
to
$info=$_POST['id'];
by adding [] to the end of your form field names, PHP will automatically convert these variables into arrays.
You should get the array like in $_POST['id']. So you should be able to do this:
foreach ($_POST['id'] as $key => $value) {
echo $value . "<br />";
}
Input names should be same:
<input name='id[]' type='checkbox' value='1'>
<input name='id[]' type='checkbox' value='2'>
...
On the form page, field names must look like this
<input name="id[]" type="checkbox" value="x">
<input name="id[]" type="checkbox" value="y">
<input name="id[]" type="checkbox" value="z">
On the destination page, $_POST['id'] is your array variable
$id = implode(",", $_POST['id']);
echo $id; //Should print "1,2,3"
You cannot echo an array directly, because it will just print out "Array". If you wanna print out the array values use print_r.
print_r($_POST['id']);
I don't know if I understand your question, but maybe:
foreach ($_POST as $id=>$value)
if (strncmp($id,'id[',3) $info[rtrim(ltrim($id,'id['),']')]=$_POST[$id];
would help
That is if you really want to have a different name (id[key]) on each checkbox of the html form (not very efficient). If not you can just name them all the same, i.e. 'id' and iterate on the (selected) values of the array, like: foreach ($_POST['id'] as $key=>$value)...