Currently, I am utilizing an AJAX call to retrieve data from a PHP file in JSON format. My goal is to change the color of my element based on whether a certain part of the data is true.
I attempted to implement it as suggested here (async:false
):
Submit form if ajax validator returns true using jquery
Regrettably, the approach did not yield the expected results.
$.ajax({
url: 'preveri.php',
data: {
"row": row,
"col": col
},
type: 'GET',
dataType: 'json',
async: false,
success: function( data ){
var jsonstuff = data;
var trys = data.trys[0];
var result = data.result[0];
var finished = data.finished[0];
if (result == "true") {
$(this).attr("style", "background-color: green");
} else {
$(this).attr("style", "background-color: red");
}
}
});
Prior to the AJAX call, I verified that this method indeed works:
$(this).attr("style", "background-color:red");
Moreover, attempts were made by replacing this
with the ID of the element but the outcome was still unsatisfactory. Another strategy involved making result
a global variable and applying the if-else statement after AJAX execution.
The PHP-generated JSON data consists of:
$data = array();
$data['result'] = array($Result);
$data['trys'] = array($trys);
$data['finished'] = array($finished);
$myJSON = json_encode($data);