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)...
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)...
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'm looping through the $_POST array (generated form elements) and I need to get the keys and values of the next elements in my foreach loop.
Edit: The name of the elements for the answers (i.e canbeanynameABC and canbeanynameXYZ) are always unknown.
Here is an example array:
Array (
[inputid] => 87 [inputoutputtype] => radio [canbeanynameABC] => radio answer 2
[inputid] => 88 [inputoutputtype] => radio [canbeanynameXYZ] => radio answer 4 )
My code here
foreach ($_POST as $key => $value) {
switch ($key) {
case "inputid":
echo "<br/>Value of input_id : " . $value;
next($_POST);
echo "<br/>Value of inputoutputtype : " . $value;
next($_POST);
echo "<br/>Value of answer : " . $value;
break;
}
}
I thought that by doing next($_POST), the pointer would now be positioned on the next key/value.
Apparently it doesn't work, i'm getting the following displays:
Value of input_id : 87
Value of input_output_type : 87
Value of answer : 87
Any help would be greatly appreciated. Thanks
Edit: It was suggested that I use arrays to organize my form elements/values. I still can't figure out how to use arrays so that each answer (value) returned from the form, I actually get three values (answer, inputid, inputoutputtype)
Edit: #CBroe I spent the afternoon trying to figure out how to use arrays in $_POST. The form can contain any number of elements, and these elements can be text, radio, select/option, checkbox (can have more than one value returned). Each "value" I get from this form must be associated with an "inputid" and an "inputoutputtype". These form and elements are generated using php, so i'm trying to build a php form handler that will read any numbers of elements and types. The generator is creating unique names for each element so that values don't get overwritten. I'm trying to figure out how to integrate arrays into the generator, but not sure if I'll be able to assign them a row number (i.e [0], [1]..).. maybe I'm just not seeing how arrays would work for my situation.
Update: Ok, now i'm trying to modify my php form handler to deal with elements that could have any names. I need to be able to read id, type and value (3 different values) from element filled out in the form.
I'm playing around with the form (even though its entirely generated), but not sure what to name my elements
<input type='hidden' name='inputradio[inputid]' value='1'>
<input type='hidden' name='inputradio[inputoutputtype]' value='radio'>
<input type='radio' name='inputradio[output]' value='Radio answer 1'>
<input type='hidden' name='inputradio[inputid]' value='2'>
<input type='hidden' name='inputradio[inputoutputtype]' value='radio'>
<input type='radio' name='inputradio[output]' value='Radio answer 2'>
<input type='hidden' name='inputtext1[inputid]' value='3'>
<input type='hidden' name='inputtext1[inputoutputtype]' value='text'>
<input type='text' name='inputtext1[output]' value='Text answer 1'>
<input type='hidden' name='inputtext2[inputid]' value='4'>
<input type='hidden' name='inputtext2[inputoutputtype]' value='text'>
<input type='text' name='inputtext2[output]' value='Text answer 2'>
Should I change the title of this post if the direction changed a but?
Thanks
C
There are only two properties to an entry in $_POST, the key and the value. I don't know where you got your example array, but it doesn't work like that. So if I had a radio button group with all sharing the same name, I would get the name->value pair.
If I created the following form and I submitted it with value a selected in the radio button group:
<form method="POST" >
<input type="text" name="textbox" value="test"><br/>
a.<input type="radio" name="rd" value="a"><br/>
b.<input type="radio" name="rd" value="b"><br/>
c.<input type="radio" name="rd" value="c"><br/>
<input type="submit" value="submit">
</form>
my $_POST would contain the following values:
Array
(
[textbox] => test
[rd] => a
)
if I tried to step through my array, I would do so as follows:
for ($_POST as $key->$value) {
echo $key . " = " . $value . "\n";
}
Which would give me the following output:
textbox = test
rd = a
Can you tell me what problem you are trying to solve, since it seems you are approaching this from the point of view of some other language or framework, or possibly you are looking to work with some other array in PHP that is not $_POST?
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.">
I'm written a PHP program to display a tab delimited file. The purpose of this is to allow the user to views the rows and select which ones they want by checking the checkbox given in the row for each record. After they hit submit I have a PHP program to display the values, but the problem is only the last row's ID is being passed. However, when the user hits the SUBMIT button I can see all the values for the rows checked:
process_form.php?download=5108&download=5110&download=5114
How should I parse this in process_form.php? I've done a var_dump of $_POST and also
$_REQUEST but it only shows the last value which is 5114. I kind of understand the problem, most of the time in forms programmers only get one value per input field, but what happens when there are many records? It doesn't seem they should all have their own unique 'name'.
<td align=center><input type="checkbox" name="download" value="<?php echo $row['ID']; ?>"></td>
I'm doing something wrong here, but I'm not sure what. Is there a way to pass an array (I'm guessing) of IDs? Or should I be looking at parsing the URL of ?download=5108&download=5110&download=5114 because it has all the values I need there? If so, how do I do that? Thanks!
This is my solution:
<td align=center><input type="checkbox" name="download[]" value="<?php echo $row['ID']; ?>"></td>
Notice that download is now download[], thus creating an array to be passed to the PHP program to process the form.
Then using this demo PHP code I was able to get access to the array:
$my_array = ($_REQUEST["download"]);
print_r($my_array);
echo "<P>";
foreach ($my_array as $value)
{
echo $value . "<BR>";
}
I'd rather use your rowID to identify the name of <input> instead of the value.
<td align=center><input type="checkbox" name="download_<?php echo $row['ID']; ?>" value="1"></td>
Then you can process your request array like:
foreach( $_REQUEST as $key => $value ) {
if( preg_match('/^download_([0-9]+)$/', $key, $reg ) {
$rowId = $reg[1]; // Your row ID
$isChecked = $value; // State of checkbox
}
}
The row ID is parsed from variable name using regexp.
EDIT:
As mentioned in comments, this is not the simplest way to read an array of checkboxes. The simpliest is to name checkboxes download[] and parse this array in PHP then.
However, this is more universal, for example when you need to get array of input texts instead of checkboxes.