PHP how to insert unchecked checkbox value [duplicate] - php

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;

Related

How to add checkbox with multiple values in table(WordPress)?

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']);?>

insert checkbox values in array into database

I have 3 checkboxes :
<input class="shoesChoice" name="trendy" type="checkbox" />
<input class="shoesChoice" name="luxury" type="checkbox" />
<input class="shoesChoice" name="sports" type="checkbox" />
And I want to store this information into (if possible) only one field in my database.
So, if the 1rst and the last input are checked, my shoesChoice field should have : "trendy, sports"
Try to change the name and keep them in value
<input class="shoesChoice" name="shoes[]" value="trendy" type="checkbox" />
<input class="shoesChoice" name="shoes[]" value="luxury" type="checkbox" />
<input class="shoesChoice" name="shoes[]" value="sports" type="checkbox" />
In PHP, try to get values in this way:
<?php
if(isset($_REQUEST['shoes']))
{
$shoes = '';
foreach
}
?>

Different checkboxes send the user to different pages when checked

I need a form in which the checkboxes would open different pages based on their selection when the form is submitted.
So, say I have this simple form:
<form action="" method="post">
<input value="1" type="checkbox" name="sign" />
<input value="2" type="checkbox" name="sign" />
<input value="3" type="checkbox" name="sign" />
<input value="4" type="checkbox" name="sign" />
<input value="5" type="checkbox" name="sign" />
<input type="submit" />
</form>
When an user checks value 1 and submits, he will be redirected to page A. If a user checks 1 and 4 he will be redirected to a different page (page F, for instance). If a user checks 2, 3 and 4 he will be redirected to page R, and so on... There would be 25 different combinations, and therefore, 25 different page results for this form when an user submits it.
In other words, when the form is submitted somehow the system would read which checkboxes were checked and associate each possible combination with a different URL.
Can it be done? If so, how? Anyone ever made something similar? I've searched a long time for solutions, but found only slightly similar ones, not exactly what I need, so any help would be appreciated.
HTML:
<form action="" method="post">
<input value="1" type="checkbox" name="sign[1]" />
<input value="2" type="checkbox" name="sign[2]" />
<input value="3" type="checkbox" name="sign[3]" />
<input value="4" type="checkbox" name="sign[4]" />
<input value="5" type="checkbox" name="sign[5]" />
<input type="submit" />
</form>
PHP:
if (isset($_POST['sign'][1]))
header("Location: a.php");
elseif(isset($_POST['sign'][2]) AND isset($_POST['sign'][3]))
header("Location: b.php");
This can be done in a couple of ways. One is that you can use javascript to intercept the form submission, change the value of the form "action", and then execute the submit.
A second approach might be to just send all this data to a single script and based on the selected values perform a redirect to the intended page.
From should look like:
<form action="" method="post">
<input value="1" type="checkbox" name="sign[]" />
<input value="2" type="checkbox" name="sign[]" />
<input value="3" type="checkbox" name="sign[]" />
<input value="4" type="checkbox" name="sign[]" />
<input value="5" type="checkbox" name="sign[]" />
<input type="submit" />
</form>
And the post like:
if(!empty($_POST['sign'])) {
if(in_array(1, $_POST['sign'])) {
// if just 1, go to page
} elseif(in_array(1, $_POST['sign']) && in_array(4, $_POST['sign'])) {
// if 1 and 4, go to page
}
}
You can just consider each of the inputs as a bit. So, you will always have 5 bits to consider.
Then define an associative array of 25 entries corresponding to each of the possible values:
00001
00010
...
var links = {
"00001" : "www.google.com",
];
Then, when you submit the form, just set the target attribute of the form based on the value.
If I were you and I had no choice than following this idea, I would use something like that in jQuery (assuming your post vars are treated in the page you want to reach):
<form action="" method="post" id="myForm">
<input value="1" type="checkbox" name="sign[]" />
<input value="2" type="checkbox" name="sign[]" />
<input value="3" type="checkbox" name="sign[]" />
<input value="4" type="checkbox" name="sign[]" />
<input value="5" type="checkbox" name="sign[]" />
<input type="submit" />
</form>
$("#myForm").submit(function() {
var checked_array = new Array();
$("#myForm input").each(function() {
if ($(this).is(":checked")
checked_array.push($(this).attr("value"));
});
if ( checked_array.indexOf(2) !== -1 && checked_array.indexOf(5) !== -1)
("#myForm").attr("action", "/url1.php") ;
else if etc...
});

how i can select multiple values from check box?

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']);

How to validate that a specific ammount of checkboxes are checked in PHP

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" />

Categories