Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at xhr.onload (settings.php:169:37)
I tried to convert a string into a JSON object using JSON.parse() which caused the above error, and here is my code I hope this helps:
function get_general() {
let site_title = document.getElementById('site_title');
let site_about = document.getElementById('site_about');
let shutdown_toogle = document.getElementById('shutdown-toogle');
let xhr = new XMLHttpRequest();
xhr.open("GET", "ajax/settings_crud.php", true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = function() {
general_data = JSON.parse(this.responseText);
site_title.innerText = general_data.site_title;
site_about.innerHTML = general_data.site_about;
site_title_inp.value = general_data.site_title;
site_about_inp.value = general_data.site_about;
if (general_data.shutdown == 0) {
shutdown_toogle.checked = false;
shutdown_toogle.value = 0;
} else {
shutdown_toogle.checked = true;
shutdown_toogle.value = 1;
}
}
xhr.send('get_general');
}
and this is the file of settings_crud.php , which is used to store the JSON structure in it so that it can be called. I hope this helps:
<?php
require('../inc/db_config.php');
require('../inc/essentials.php');
adminLogin();
if (isset($_POST['get_general'])) {
$q = "SELECT * FROM 'settings' WHERE 'sr_no'=?";
$values = [1];
$res = select($q, $values, "i");
$data = mysqli_fetch_assoc($res);
$json_data = json_encode($data);
echo $json_data;
}
if (isset($_POST['upd_general'])) {
$frm_data = filteration($_POST);
$q = "UPDATE `settings` SET `site_title` = ?, `site_about` = ? WHERE `sr_no` = ?";
$values = [$frm_data['site_title'], $frm_data['site_about'], 1];
$res = update($q, $values, "ssi");
echo $res;
}
if (isset($_POST['upd_shutdown'])) {
$frm_data = ($_POST['upd_shutdown']==0) ? 1 : 0 ;
$q = "UPDATE `settings` SET `shutdown` = ? WHERE `sr_no` = ?";
$values = [$frm_data, 1];
$res = update($q, $values, "ii");
echo $res;
}
New contributor
Indra Maulana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3