I have built a form that has a checkbox input array (saving to an array). However, when I POST it and retrieve the results, it only offers the last selection.
<input type="checkbox" value="Friendly" name="quest[9]"> Friendly<br>
<input type="checkbox" value="Attentive" name="quest[9]"> Attentive<br>
<input type="checkbox" value="Enthusiastic" name="quest[9]"> Enthusiastic<br>
<input type="checkbox" value="Understanding" name="quest[9]"> Understanding<br>
<input type="checkbox" value="Well Mannered" name="quest[9]"> Well Mannered<br>
<input type="checkbox" value="Efficient" name="quest[9]"> Efficient<br>
<input type="checkbox" value="Genuine" name="quest[9]"> Genuine<br>
For example, say I chose "Friendly", "Efficient", and "Genuine".
When I POST it over to a PHP document and run
print_r($_POST['quest']);
I only receive
Array ( [9] => Genuine )
back so "Genuine" is the only item I get back. Is there a way to fix this? What have I done wrong?
This is the 9th question on the survey, so changing the name unfortunately is not an option. Is there any way to combine the results to that single array separated by commas? I could always explode on the php side.
All your checkboxes have the same name, make them unique
<input type="checkbox" value="Friendly" name="quest[3]"> Friendly<br>
<input type="checkbox" value="Attentive" name="quest[4]"> Attentive<br>
<input type="checkbox" value="Enthusiastic" name="quest[5]"> Enthusiastic<br>
<input type="checkbox" value="Understanding" name="quest[6]"> Understanding<br>
<input type="checkbox" value="Well Mannered" name="quest[7]"> Well Mannered<br>
<input type="checkbox" value="Efficient" name="quest[8]"> Efficient<br>
<input type="checkbox" value="Genuine" name="quest[9]"> Genuine<br>
or use empty square brackets so php will treat the inputs as an array
<input type="checkbox" value="Friendly" name="quest[]"> Friendly<br>
<input type="checkbox" value="Attentive" name="quest[]"> Attentive<br>
<input type="checkbox" value="Enthusiastic" name="quest[]"> Enthusiastic<br>
<input type="checkbox" value="Understanding" name="quest[]"> Understanding<br>
<input type="checkbox" value="Well Mannered" name="quest[]"> Well Mannered<br>
<input type="checkbox" value="Efficient" name="quest[]"> Efficient<br>
<input type="checkbox" value="Genuine" name="quest[]"> Genuine<br>
use quest[] in name instead of quest[9].
also in php part use this to add multiple choices .
<?php
$quest = implode(',',$_post['quest']);
print_r($quest);
?>
Happy coding!!
I'm posting a new answer about your comments on the previous one:
Since you must keep quest[9] as the organization for the checkbox array..
You may want to try and make it a more complex array, where each <input> has name="quest[9][1]", name="quest[9][2]" and so on.
And find the contents by
print_r($_POST['quest']);
again
Related
All the inputs have the same name as option<?=$x?> for each loop but always the input with type=hidden of the last line overrides all of them although one of the radio buttons with same name option<?=$x?> is selected
Is there anyway so that I can check whether anyone out of four radios of above is chosen or not, if not then I have to pass the last input instead of them
<input type="radio" name="option<?=$x?>" value="<?=$chioce[0].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[0]?>
<input type="radio" name="option<?=$x?>" value="<?=$chioce[1].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[1]?>
<input type="radio" name="option<?=$x?>" value="<?=$chioce[2].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[2]?>
<input type="radio" name="option<?=$x?>" value="<?=$chioce[3].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[3]?>
<input type="hidden" name="option<?=$x?>" value="null;<?=$data['id'].';'.$data['subject']?>">
You can try this
<input type="radio" name="option[<?=$x?>]['radio']" value="<?=$chioce[0].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[0]?>
<input type="radio" name="option[<?=$x?>]['radio']" value="<?=$chioce[1].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[1]?>
<input type="radio" name="option[<?=$x?>]['radio']" value="<?=$chioce[2].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[2]?>
<input type="radio" name="option[<?=$x?>]['radio']" value="<?=$chioce[3].';'.$data['id'].';'.$data['subject']?>" ><?=$chioce[3]?>
<input type="hidden" name="option[<?=$x?>]['hidden']" value="null;<?=$data['id'].';'.$data['subject']?>">`
Now when you post you will get option array
$options = $_POST['option'];
$radioValue = $options[$x]['radio'];
$hiddenValue = $options[$x]['hidden'];
You can read value like this
$optionValue = isset($options[$x]['radio']) ? $options[$x]['radio'] : $options[$x]['hidden'];
Hope it may help you
I have 1 form in with multiple checkboxes in it (each with the code):
<input type="checkbox" name="check_list" value="<? echo $row['Report ID'] ?>">
Where $row['Report ID'] is a primary key in a database -so each value is different.
How would I be able to tell which checkboxes have been checked? (Maybe multiple)
This is for an inbox system and I have a button below that I want (when clicked) to delete all messages (ids of: $row['Report ID']) which have the checkbox's checked.
Set the name in the form to check_list[] and you will be able to access all the checkboxes as an array($_POST['check_list'][]).
Here's a little sample as requested:
<form action="test.php" method="post">
<input type="checkbox" name="check_list[]" value="value 1">
<input type="checkbox" name="check_list[]" value="value 2">
<input type="checkbox" name="check_list[]" value="value 3">
<input type="checkbox" name="check_list[]" value="value 4">
<input type="checkbox" name="check_list[]" value="value 5">
<input type="submit" />
</form>
<?php
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
echo $check; //echoes the value set in the HTML form for each checked checkbox.
//so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
//in your case, it would echo whatever $row['Report ID'] is equivalent to.
}
}
?>
Edit To reflect what #Marc said in the comment below.
You can do a loop through all the posted values.
HTML:
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
PHP:
foreach($_POST['check_list'] as $item){
// query to delete where item = $item
}
you have to name your checkboxes accordingly:
<input type="checkbox" name="check_list[]" value="…" />
you can then access all checked checkboxes with
// loop over checked checkboxes
foreach($_POST['check_list'] as $checkbox) {
// do something
}
ps. make sure to properly escape your output (htmlspecialchars())
<input type="checkbox" name="check_list[<? echo $row['Report ID'] ?>]" value="<? echo $row['Report ID'] ?>">
And after the post, you can loop through them:
if(!empty($_POST['check_list'])){
foreach($_POST['check_list'] as $report_id){
echo "$report_id was checked! ";
}
}
Or get a certain value posted from previous page:
if(isset($_POST['check_list'][$report_id])){
echo $report_id . " was checked!<br/>";
}
It's pretty simple. Pay attention and you'll get it right away! :)
You will create a html array, which will be then sent to php array.
Your html code will look like this:
<input type="checkbox" name="check_list[1]" alt="Checkbox" value="checked">
<input type="checkbox" name="check_list[2]" alt="Checkbox" value="checked">
<input type="checkbox" name="check_list[3]" alt="Checkbox" value="checked">
Where [1] [2] [3] are the IDs of your messages, meaning that you will echo your $row['Report ID'] in their place.
Then, when you submit the form, your PHP array will look like this:
print_r($check_list)
[1] => checked
[3] => checked
Depending on which were checked and which were not.
I'm sure you can continue from this point forward.
i already saw other pages about this topic, but i can't find the solution for my case.
I have a form with almost 70 fields, and 3 of them are Checkbox where the user can choose more than one option and i need to save them all.
I know that probably thats an easy way to resolve this but i'm about 8 hours straight and i can't see nothing at this point...
HTML Form:
(...)
<p><b>Quais as empresas que reconhece como patrocinadores desta edição da prova?</b></p>
<input type="checkbox" name="Patrocinadores_Prova" value="EDP"><span style="padding-left:10px"></span>EDP<br>
<input type="checkbox" name="Patrocinadores_Prova" value="Agência Abreu"><span style="padding-left:10px"></span>Agência Abreu<br>
<input type="checkbox" name="Patrocinadores_Prova" value="DietSport"><span style="padding-left:10px"></span>DietSport<br>
<input type="checkbox" name="Patrocinadores_Prova" value="Dolce Vita Douro"><span style="padding-left:10px"></span>Dolce Vita Douro<br>
<input type="checkbox" name="Patrocinadores_Prova" value="Europcar"><span style="padding-left:10px"></span>Europcar<br>
<input type="checkbox" name="Patrocinadores_Prova" value="Fruut"><span style="padding-left:10px"></span>Fruut<br>
<input type="checkbox" name="Patrocinadores_Prova" value="Jumbo"><span style="padding-left:10px"></span>Jumbo<br>
<input type="checkbox" name="Patrocinadores_Prova" value="Liberty Seguros"><span style="padding-left:10px"></span>Liberty Seguros<br>
<input type="checkbox" name="Patrocinadores_Prova" value="Reccua"><span style="padding-left:10px"></span>Reccua<br>
<input type="checkbox" name="Patrocinadores_Prova" value="Vindimar"><span style="padding-left:10px"></span>Vindimar<br>
<input type="checkbox" name="Patrocinadores_Prova" value="Vitalis"><span style="padding-left:10px"></span>Vitalis<br>
<input type="checkbox" name="Patrocinadores_Prova" value="Wall Street English"><span style="padding-left:10px"></span>Wall Street English<br><br>
(...)
PHP Code:
(...)
$Patrocinadores_Prova = $_POST['Patrocinadores_Prova'];
(...)
mysql_query ("INSERT INTO questionario(...'" . implode(',', $Patrocinadores_Prova) ."'....)
if you want to save multiple check boxes value in database you have to use array[], so first change your chackboxes name from name="Patrocinadores_Prova" to name="Patrocinadores_Prova[]" and then execute your php code.
I have a form:
<form name="form1" method="post" action="form_to_write.php">
<h4>q1</h4>
<input type="radio" name="a1" value="someValue1" />someValue1<br />
<input type="radio" name="a1" value="someValue2" />someValue2<br />
<input type="radio" name="a1" value="someValue3" />someValue3
<h4>q2</h4>
<input type="radio" name="a2" value="someValue4" />someValue4<br />
<input type="radio" name="a2" value="someValue5" />someValue5<br />
<input type="radio" name="a2" value="someValue6" />someValue6
<h4>q3</h4>
<input type="radio" name="a3" value="someValue9" />someValue9<br />
<input type="radio" name="a3" value="someValue7" />someValue7<br />
<input type="radio" name="a3" value="someValue8" />someValue8
<input type="submit" value="submit" name="submit"/>
</form>
And want to read all inputs to array by type (radio). I know, how to read it by name, but how by type?
The input type attribute is not sent to the server when the form is submitted. Only the name and the value are sent. You will need to keep track of what's what yourself on the server using useful names.
make your form_to_write.php like this:
<?php
print_r($_POST);
and study it's output.
It contains everything you can get from the form. You are free to choose what to use. Enjoy.
As your question being a perfect example of a badly asked question, I can only guess your real needs.
It seems you want to get an array contains all radio buttons. You still can do it by using names.
make your radio buttons names like this
<input type="radio" name="radios[a1]" value="someValue1" />someValue1<br />
<input type="radio" name="radios[a2]" value="someValue4" />someValue4<br />
<input type="radio" name="radios[a3]" value="someValue9" />someValue9<br />
and you'll be able to access $_POST['radios'] array which contains all your radio fields
If you are looking for a PHP function like GetAllInputsOfType("radio") then you won't find it (unless you can do somethign fancy with the DOM, like JS does; maybe this will help?).
What I have done in similar circumstances is to rename my input fields according to type, so instead of a1, a2, a3, you could have radio_a1, radio_a1, radio_a3 and text_a4, memo_a5, listbox_a6, etc (and, btw, use some meaningful names, not a1, a2, a3 ;-)
Then you can loop thorough the array $_GET or $_POST looking for elements beginning radio_ ...
You could use something like a Zend_Form which keeps track of it (and could even do validating jobs etc). But you can't get the type of a form field with just php - you'll need to do things in JS which is on the client side and may not be trusted.
I having around 20 check boxes in my form as
<input type="checkbox" name="c1" value="1" />
<input type="checkbox" name="c2" value="2" />
<input type="checkbox" name="c3" value="3" />
<input type="checkbox" name="c4" value="4" />
i would like to these values to a database. I just thought rather than creating 20 fields in my database grab all the values at store in the db as 1,2,3,4 and then when querying to explode the values and display it accordingly using php.
can someone please tell me how am i supposed to concatenate the values 1,2,3,4 from the check fields when submitted so i can pass to the database.
i would appreciate if anyone can suggest a different effective way to achieve this using php.
You can change the name of the checkboxes to be the same, something like
<input type="checkbox" name="cb[]" value="1" />
<input type="checkbox" name="cb[]" value="2" />
Then access them via $_GET or $_POST via
if (isset($_POST['cb'])) {
$my_values = implode(",", $_POST['cb']);
}
If you want them sorted, then you will want to do something like this:
if (isset($_POST['cb'])) {
$my_values = $_POST['cb'];
sort($my_values);
$my_db_value = implode(',', $my_values);
}
For the record, I agree with #Shef in the case that the database can handle the extra load. Depending on when this information will be needed in a highly scalable solution, however, this is a perfectly acceptable way to handle this.
To answer your initial question, first off you need to name your checkboxes all the same name, so:
<input type="checkbox" name="box[]" value="1" />
....
<input type="checkbox" name="box[]" value="20" />
PHP script:
$boxes = $_POST['box'];
$values = implode(",", $boxes); // $values now has "1,2,3,4...", insert into table
A better way would be to have a separate table (see #Shef 's comment).