json returns nothing after value changed
json returns nothing after value changed
Hi guys, I have DataTables table with row details which are opening by clicking on row. The row details are displayed in 3 different languages which are selected by checkbox. When the row is clicked, the language is send to details.php script (langy:chLanguage) which returns json values for this selected language.
Initialy the language is set to chDe and everything is working fine until I click on another language. When the language is changed to EN or RU, or even back to DE, the returned json is empty.
Index.php:
<h5>Language</h5>
<input type="checkbox" id="chDe" name="languages" checked>DE
<input type="checkbox" id="chEn" name="languages">EN
<input type="checkbox" id="chRu" name="languages">RU
<script>
var chLanguage = "chDe";
$('input[name=languages]').click(function() {
$('input[name="' + this.name + '"]').not(this).prop('checked', false);
chLanguage = [];
$('input[name=languages]:checked').each(function() {
chLanguage.push($(this).attr('id'));
});
if (chLanguage == '') {
chLanguage = 5;
}
$('#example').DataTable().ajax.reload();
});
function format ( rowData, row, $chLanguage ) {
// FOLLOWING CONSOLE.LOG RETURNS THE ACTUALLY SELECTED LANGUAGE CORRECTLY
console.log("chLanguage in format function: " + chLanguage);
var div = $('<div/>')
.addClass( 'loading slider' )
$.ajax( {
url: 'scripts/details.php',
data: {
name: rowData[0],
langy: chLanguage,
},
dataType: 'json',
type: 'post',
success: function ( json ) {
// FOLLOWING CONSOLE.LOG RETURNS [Object object] unless the language is changed, then it returns nothing
console.log("json: " + json);
var childTable = '<div id="rowdetails" class="slidera"><ul class="slider">';
childTable = childTable.concat(
'<div class="row rdstyle">' +
'<table class="detailstable detailserrors">' +
'<tr><td><b>Error class:</b></td><td>' + json[0].ERRTRIEDA + '</td></tr>' +
'<tr><td><b>Error group:</b></td><td>' + json[0].ERRSKUPINA + '</td></tr>' +
'<tr><td><b>Error:</b></td><td>' + json[0].ERRPOPIS + '</td></tr>' +
'</table>' +
'</div>
);
});
}
details.php:
$language = $_POST['langy'];
if ($language == 'chDe' ) {
$setLang = 'DE';
}else if($language == 'chEn') {
$setLang = 'EN';
} else{$setLang = 'RU';}
and $setLang is used in SQL query to filter data by language.
I hope I'm not far away from working solution, because it's working already, just the language switch don't work. Sorry not to attach working sample. I don't know how to implement all these parts including mysql db and several differenct php scripts
Thank you.
Answers
If the returned JSON is empty then you will need to debug your server script (details.php). First start by verifying the SQL query is correct. There is noting in the client code that we can debug to see why the server is responding with an empty JSON.
If you want to post the relevant portions of your details.php script someone may be able to see the problem or tell you what to do to help debug. I don't use PHP so won't be much help with the script.
Kevin
Hi Kevin, I don't think it's problem with SQL query, because when I change the value in line 9:
var chLanguage = "chDe";
to chEn or chRu, the data are displayed in the selected language. I tried to check the value of $language (from details.php) when the page loads and when the language is changed. So, when the page is loaded, everything is working fine with the DE language, but when I click on another language, the console.log at line 25 has the correct value of new selected language, but the console.log at line 38 is empty.
But if you still think that the problem is in details.php, I can share it.
I would start by validating the jQuery Ajax request by looking at the browser's developer tools. Make sure the request has the proper
langy
value then see what the response is. If the correct parameter is there and the response is empty then the issue is with your server script.Kevin
Hi Kevin, I'm able to check the chLanguage at line 25 and it changes it's value correctly. I don't know how to check the langy at line 32, because I cannot use the console.log there.
But then I'm testing the $language variable from details.php and it has the correc value only at the beginning. After I click on any ceckbox, it's empty.
My suggestion was to use the browser's developer tools to see what is in the Ajax request and response. This [technote'(https://datatables.net/manual/tech-notes/1#Diagnosis) has some steps.
When you say its empty, do you mean the
$language
variable or the JSON response?Can you post a link to your page so we can take a look?
Kevin