Right now i changed from options table to my own table so things are getting difficult
i have a checkbox its name is selection in table, so here is what i tried
<input type="checkbox" name="selection" value="1"<?php checked("1",$item['selection']);?> />
<input type="checkbox" name="selection" value="2"<?php checked("2",$item['selection']);?> />
<input type="checkbox" name="selection" value="3"<?php checked("3",$item['selection']);?> />
This is not saving any value , Then i tried as array in below
<input type="checkbox" name="selection[]" value="1"<?php checked("1",$item['selection']);?> />
<input type="checkbox" name="selection[]" value="2"<?php checked("2",$item['selection']);?> />
<input type="checkbox" name="selection[]" value="3"<?php checked("3",$item['selection']);?> />
It is throwing warning Warning: mysql_real_escape_string() expects parameter 1 to be string, array given
How to store multiple values with the same name in checkbox when using my own table?
Any help help would be greatful.
i suppose checked is a function and you are returning vaule with it. if thats true than you are missing an echo before your checked function
<?php echo checked("1",$item['selection']);?>
Related
This question already has answers here:
POST unchecked HTML checkboxes
(44 answers)
Closed 1 year ago.
Need help. i have 5 checkboxes[] 3 is checked 2 unchecked
i want to insert all checkbox values but if checkbox is checked than insert value 1 and if unchecked insert 0
<input id="answer" type="text" name="answer[]" /> //checked
<input id="answer" type="text" name="answer[]" />
<input id="answer" type="text" name="answer[]" /> /checked
<input id="answer" type="text" name="answer[]" />
<input id="answer" type="text" name="answer[]" /> //checked
if i check the length or size of field than it displays and insert only checked items but i want to insert unchecked also with value 0
The browser does not send unchecked checkbox data to the server, PHP in this case.
So in your PHP you have to know about all the checkboxes that exist on your form and check for the existance of each to decide what to store to your database.
So something like
HTML
<input type="checkbox" name="cb1" value="1" /> //checked
<input type="checkbox" name="cb2" value="2" />
<input type="checkbox" name="cb3" value="3" /> /checked
<input type="checkbox" name="cb4" value="4" />
<input type="checkbox" name="cb5" value="5" /> //checked
if (isset($POST['cb1'])) {
$cb1 = $POST['cb1'];
}else{
// it was not checked because it was not sent
// so use the default or whatever UNCHECKED value when storing to the DB
$cb1 = 0; // for example
}
You can shorten that code a bit by doing
$cb1 = isset($POST['cb1']) ? $POST['cb1'] : 0;
I've got a lot of radio boxes on a single page within my website.
I know it is possible to submit all of the options as an array that can be directly manipulated by PHP.
<input type="radio" name="SOMETHING_HERE" value="1" />
There are several radio groups and I'd like all of them to submit to one array.
All I'd like to know is the syntax which has to be used in the name="".
Use like this
<input type="radio" name="optionname[]" value="1" />
<input type="radio" name="optionname[]" value="2" />
.
.
You can have your form in either of the following ways.
<input type="radio" name="name[]" value="1" />
<input type="radio" name="name[]" value="2" />
Or
<input type="radio" name="question['question1']" value="1" />
<input type="radio" name="question['question2']" value="2" />
There by you setting the array definition yourself. Hope this answers your query.
HTML looks like this:
<input type="radio" name="name[]" value="1" />
<input type="radio" name="name[]" value="2" />
PHP accesses it like this:
<?php
$_POST['name'] = array('1','2');
Some things to note:
Order in array as compared to as it exists in HTML is not guaranteed
Is it possible to conditionally add hidden input fields into a form?
eg I have a php form that is adding values to a table and if the appleID = 1 or 2 then I want 1 added to the fruits column of my table and if appleID =3 I want 1 added to the sweets column of my table. I thought I might be able to do something like the below but it is adding all hidden values no matter what I select. Or should I approach this a different way?
<input type="radio" value="1" name="appleID" />
<input type="hidden" value="1" name="fruits" />
<input type="hidden" value="0" name="sweets" />
<input type="radio" value="2" name="appleID" />
<input type="hidden" value="1" name="fruits" />
<input type="hidden" value="0" name="sweets" />
<input type="radio" value="3" name="appleID" />
<input type="hidden" value="0" name="fruits" />
<input type="hidden" value="1" name="sweets" />
Thanks I haven't done much with php so I will need to explore that option further. Thanks for the feedback.
I was also looking at something like the below. But PHP sounds likes the better option.
change field value when select radio buttons
You can either use all values in the radio buttons (1,1,0 - 2,1,0 - 3,0,1) and split them after receiving them in your PHP script or add/delete the hidden fields via JavaScript.
Split Example:
HTML:
<input type="radio" value="1,1,0" name="appleID" />
<input type="radio" value="2,1,0" name="appleID" />
<input type="radio" value="3,0,1" name="appleID" />
PHP:
if (!empty($_POST['appleID']))
{
list($appleID, $fruits, $sweets) = explode(",", $_POST['appleID']);
}
It is better to put that logic into the PHP script instead of using Javascript - because you have to do the validation anyway.
hi i have multiple option in check box and when visitor or customer select multiple option then how i can get multiple values? plz explain with code thanks
Name the checkboxes with [] (or PHP will drop all but one of them (I don't recall if it is the first or last)).
<input type=checkbox name="foo[]" value="some value">
Then they will be accessible as an array in the $_GET or $_POST superglobal.
$_GET['foo'][]
Basically, set all the name tags to be the same for all your checkboxes (with []). Then in your script, the values will be available as an array
Html:
<input type="checkbox" name="tags[]" value="1" />
<input type="checkbox" name="tags[]" value="2" />
<input type="checkbox" name="tags[]" value="3" />
<input type="checkbox" name="tags[]" value="4" />
PHP:
print_r($_REQUEST['tags']);
Reference: http://www.kavoir.com/2009/01/php-checkbox-array-in-form-handling-multiple-checkbox-values-in-an-array.html
Like this
<input type="checkbox" name="foo[]" value="bar" />
<input type="checkbox" name="foo[]" value="baz" />
<input type="checkbox" name="foo[]" value="qux" />
<?php
print_r($_POST['foo']);
I have to validate that maximum 3 checkboxes are clicked. There are 11. How could I do this efficiently and without testing every possible situation?
You can do like this:
if (count($_POST['checkbox_name']) === 3)
{
// your code here.....
}
where your checkbox names should be suffixed with [] eg:
<input type="checkbox" name="checkbox_name[]" value="1" />
<input type="checkbox" name="checkbox_name[]" value="2" />
<input type="checkbox" name="checkbox_name[]" value="3" />