I have a form to update personal data with multiple checkboxes who can be initially checked or not.
<form method="post" id="up_mission">
<input checked type="checkbox" id="first">
<input type="checkbox" id="second">
<button class="btn btn-default" type="submit" id="update_mission<?php echo $id_mission ?>"</button>
</form>
and the JS:
$(function() {
$("form[id^='up_mission']").submit(function() {
var value1 = document.getElementById("first").checked;
var value2 = document.getElementById("second").checked;
$.post("update_mission.php", {value1: value1, value2:value2}, functon(data) { console.log(data) });
});
});
In update_mission.php
there is a simple SQL query to update with news values and I also do var_dump($_POST);
and the values printed for first and second are always the ones that I initially put in the <input>
it doesn't matters if I uncheck or check the checkbox.
How can I fix it please?
Here is the part of the php file :
var_dump($_POST);
$first = $_POST['value1'];
$second = $_POST['value2'];
if ($first == "true") { $first = "on";}
else {$first = "0";}
if ($second == "true") { $second = "on";}
else {$second = "0";}
update_miss = "UPDATE table SET First = 'first', Second = 'second'[...] WHERE my_condition";
// I connect to my DB, and do the query, and there is no problem here
// I also have text input in my form and they update pretty well
Use $('#first').is(':checked')
and $('#second').is(':checked')
to detect state of checkbox.
$(function() {
$("form[id^='up_mission']").submit(function() {
var value1 = $('#first').is(':checked') ? 1 : 0;
var value2 = $('#second').is(':checked') ? 1 : 0;
$.post("update_mission.php", {value1: value1, value2: value2}, functon(data) { console.log(data) });
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.