Can't send ajax request to server

<div class="test"> <input type="checkbox" value="1" name="setting">Test </div> 
 $('.test input[type="checkbox"]').change(function(setting) { if($(this).is(':checked')) { var chkVal = $(this).attr("value"); $.ajax({ url: 'index.php', type: 'POST', data: { val: chkVal }, beforeSend: function() { alert(chkVal); }, success: function(data) { alert(data); } }); } }); 
 if($_POST['setting']) { $result = add_checked($_POST['setting']); if(!$result) { exit(FALSE); } exit(json_encode($result)); } function add_checked($id_test) { $setting = (int)$_POST['setting']; $query = "UPDATE test SET setting = '$setting' WHERE id_test = $id_test"; $res = mysql_query($query) or die(mysql_error()); return true; } 

When clicking a checkbox, a request should be sent.
In the test table, the setting cell needs to be set 1

Why is this not happening?

  • Does the execution inside if go into javascript? Does the PHP script itself start running? Does the browser console write any errors (usually caused by pressing F12)? - yeputons
  • $(this).attr("value"); - it is more correct to use $(this).val(); and WHERE id_test = $id_test should be WHERE id_test = '$id_test' - Alex
  • if the problem is with sending the form, then it is better to show html, and not php - Alex
  • value is not a checkbox status, always one is sent. At the output of php, the answer is returned for some reason in the form of json_encode , when add_checked returns only (boolean) true - Mr. Black

1 answer 1

One checkbox

 <input id='fuckyeah' type='checkbox' value='1'> 
 checkbox = document.querySelector('#fuckyeah'); checkbox.onchange = function() { $.ajax({ url: 'index.php', type: 'POST', data: { settings: 0, id: this.value, checked: this.checked ? 1:0 }, beforeSend: function() { checkbox.disabled = true; }, complete: function() { checkbox.disabled = false; }, success: function(response) { console.log(response); } }); } 
 if(isset($_POST['settings'])) { $checkbox = (int) $_POST['id']; $checked = (int) $_POST['checked']; settingsUpdate($checkbox, $checked); } function settingsUpdate($checkbox, $checked) { $query = mysql_query("UPDATE settings SET status = '$checked' WHERE id = $checkbox"); if(!$query) echo 'err'; } 

Several checkboxes

value id input
name settings name

 <input class='fuckyeah' type='checkbox' value='1' name='settings'> <input class='fuckyeah' type='checkbox' value='2' name='settings'> 
 checkboxes = Array.from(document.querySelectorAll('.fuckyeah')); checkboxes.forEach(function(checkbox, i) { checkbox.onchange = function() { $.ajax({ url: 'index.php', type: 'POST', data: { settings: this.name, id: this.value, checked: this.checked ? 1:0 }, beforeSend: function() { checkbox.disabled = true; }, complete: function() { checkbox.disabled = false; }, success: function(response) { console.log(response); } }); } }); 
 if(isset($_POST['settings'])) { $settings = (string) $_POST['settings']; $checkbox = (int) $_POST['id']; $checked = (int) $_POST['checked']; settingsUpdate($settings, $checkbox, $checked); echo 'id: '.$checkbox.', status: '.$checked; } function settingsUpdate($settings, $checkbox, $checked) { $query = mysql_query("UPDATE $settings SET status = '$checked' WHERE id = $checkbox"); if(!$query) echo 'err'; } 

data_base_name database name
table_name name of the table, in this case, settings

  $query = mysql_query(" CREATE TABLE IF NOT EXISTS `data_base_name`.`table_name` ( `id` INT NOT NULL AUTO_INCREMENT , `checked` INT NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM ; "); 
  • When you click on the checkbox, the status cell in the settings table is where the id cell is set to the value of the checkbox, 0 (not ticked) or 1 (ticked). Also, when requesting, you need to block the checkbox until the answer comes or the request time is out - Mr. Black
  • I certainly apologize, but nothing happens. My table looks like this: CREATE TABLE IF NOT EXISTS settings ( id int (3) unsigned NOT NULL, checked int (5) NOT NULL) ENGINE = InnoDB DEFAULT CHARSET = utf8; if you can help pozh. - Karen
  • @Seva, added an example of creating a table - Mr. Black
  • When sending, it gives this result, I do not know whether it is correct or not. Prntscr.com/bftk5f - Karen
  • @Seva, the answer should not be json - Mr. Black