How come checkbox state is not always passed along to PHP script?
If you want to overcome this design feature, try this:
<input type="hidden" name="check_box_1" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
This way, you'll always have $_POST['check_box_1']
set in the callback page, and then you can just check its value to see whether they checked it or not. The two inputs have to be in that order though, since the later one will take precedence.
Checkbox values are NOT passing to PHP using ajax
first of all, you have three elements with the same id="color".
Then:
document.getElementById("color")
Will always return first of them, and no matter is it checked or not.
It's good idea to declare another function:
function __(selector) {
return document.querySelectorAll(selector);
}
Then, you could write:
var colors = __('input[name="color"]:checked');
var result = [];
for(var i = 0; i < colors.length; i++) {
result.push(colors[i].value);
}
formdata.append("color", result);
Pass checkbox values 1 if checked if not 0to PHP using AJAX
thanks a lot sir for replay; but the problem is the checkbox is dynamique and the selected value depends on the users !!
and i have used form serliaze to send pot data via ajax : the checkbox are in the form
<form name="form" id="form" role="form" method="post" action="">
<<tr>
<td>Answer</td>
<td><input type="text" id="name" name="answer[]"></td>
<td>
<input type="checkbox" id="check" name="check[]" ></td>
<td><input type="button" value="Ajouter" " class="button"></td>
</tr>
</form>
how could i choose the selected answer ! the idea is too store one if the answer is true if not store 0 in the colum 'correct' in database
i try by this way process data by AJAX
$(function() {
$("#form").on("submit", function(event) {
event.preventDefault();
$.ajax({
url: "test.php",
type: "post",
data: $(this).serialize(),
success: function(d) {
alert(d);
}
});
});
});
});
Checkbox values don't get send correctly to PHP with AJAX
The way to handle a checkbox in PHP is this...
You can give them values if you would like but it's not necessary. There is only one way to validate a checkbox in php. This is the correct way.
// This checks to see if the checkbox is "checked"
if(isset($_POST['checkbox_name'])){
// Here you can set the value manually
$checkbox_name = 1;
// Or you can set it to the checkbox value
$checkbox_name = $_POST['checkbox_name'];
}else{
// Here is where you can set the value if it is NOT "checked"
$checkbox_name = 0;
}
And on the javascript side you can do this...
// To set the value manually based on being checked or not
var heli = $("#helicopter").is(':checked') ? 1 : 0;
// Or get the value if checked
var heli = $("#helicopter").is(':checked') ? $("#helicopter").val() : 0;
PHP How to pass checkbox states after POST
HTML:
<input type='checkbox' name='selected' value='1'>
PHP:
$selected = isset($_POST['selected']) && $_POST['selected'] ? "1" : "0";
This code will set selected checkbox with value 1 in variable $selected
and 0 if not selected.
PHP not processing checkboxes on form
Why strip_tags
?
Do this, Below code will check whether you have selected checkbox or not. If you have checked, it send Yes
else No
. Change text No
to null if you don't want to sent.
$name = strip_tags($_POST['name']);
$attending = strip_tags($_POST['attending']);
$guests = strip_tags($_POST['guests']);
$prenuptial = (isset($_POST['prenuptial']) && $_POST['prenuptial'] == 'Yes') ? "Yes" : "No";
$transportation = (isset($_POST['transportation']) && $_POST['transportation'] == 'Yes') ? "Yes" : "No";
$to = 'email@yahoo.com';
See You have given value is yes
in both the checkbox, So when you post a for yes
will be the value of both the checkbox.
<input type="checkbox" name="prenuptial" value="Yes"><span class="additions">I Will Be Attending the Prenuptial Dinner</span><br>
<input type="checkbox" name="transportation" value="Yes"><span class="additions">I Will Be Requiring Transportation To and From the Wedding</span>
If you want any other value you can put it. And you can check using isset
Eg - you can add it like this
<input type="checkbox" name="prenuptial" value="I Will Be Attending the Prenuptial Dinner"><span class="additions">I Will Be Attending the Prenuptial Dinner</span><br>
<input type="checkbox" name="transportation" value="I Will Be Requiring Transportation To and From the Wedding"><span class="additions">I Will Be Requiring Transportation To and From the Wedding</span>
And in php write like this
$prenuptial = '';
if (isset($_POST['prenuptial'])) {
$prenuptial = $_POST['prenuptial'];
}
Update database values for checkboxes using PHP PDO not updating all checkboxes
I added a hidden input for each one.
So I changes this:
<input type="checkbox" name="mysocial[]" value="myfacebook" <?php echo ($result['my_facebook']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mytwitter" <?php echo ($result['my_twitter']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mygoogle" <?php echo ($result['my_google']==1 ? 'checked' : '');?> />
To this:
<input type="hidden" name="mysocial[]" value="myfacebook1" <?php echo ($result['my_facebook']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="myfacebook" <?php echo ($result['my_facebook']==1 ? 'checked' : '');?> />
<input type="hidden" name="mysocial[]" value="mytwitter1" <?php echo ($result['my_twitter']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mytwitter" <?php echo ($result['my_twitter']==1 ? 'checked' : '');?> />
<input type="hidden" name="mysocial[]" value="mygoogle1" <?php echo ($result['my_google']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mygoogle" <?php echo ($result['my_google']==1 ? 'checked' : '');?> />
This seems to have got it working.
Problem collecting the checkbox value using AJAX and PHP
You can simplify your code quite a bit. If you just want to pass the state of the checkbox, you only need:
Instead of your checkMark()
function, just check it in the submit callback.
// This will set the value 1 if it is checked and 0 if it isn't.
var checkBox = document.getElementById("spouse");
var type = {
'spouse' : checkBox.checked ? 1 : 0
}
// Don't stringify the object, just pass it as is. jQuery will take care if it.
$.ajax({
...
data: type,
...
Related Topics
Laravel - Database, Table and Column Naming Conventions
How to Set Global Environment Variables for PHP
Php: Locale Aware Number Format
Laravel 5.2 Cors, Get Not Working with Preflight Options
File_Get_Contents When Url Doesn't Exist
Can PHP Detect If Its Run from a Cron Job or from the Command Line
Print Currency Number Format in PHP
Best Way to Access Exchange Using PHP
Symfony 2.0 Getting Service Inside Entity
Trying to Send Mail Using Swift Mailer, Gmail Smtp, PHP
How to Create a Facade Class with Laravel
Running Python Script in Laravel
PHP - Add Item to Beginning of Associative Array
How to Access an Object Property Named as a Variable in PHP
How to Sort MySQL Fulltext Search Results by Relevancy
Determine If Business Is Open/Closed Based on Business Hours