I am trying to return variables within the success of an ajax request to validate that "such a thing" was done within the system, in this case not to let it advance until it adds elements to a list.
$("#saveList").on(\'submit\',(function(e) {
e.preventDefault();
formString = $("#previewList").serialize();
$.ajax({
url: "lists/list.php",
data: formString,
type: "GET",
dataType: "json",
success: function(data){
if(data.valida == "zero"){
toastr.warning("Agrega al menos un elemento a la lista");
}else{
alert("Lista guardada");
location.reload(true);
}
},
error: function(data){
console.log("error")
}
});
})
);
And in the script of lista.php the validation I do it in this way, return a variable with the value of string "zero" and finish the execution of the script:
$result = mysqli_query($link,"SELECT * FROM lists WHERE emp = $idemp");
$count = mysqli_num_rows($result);
// if there is no rows
if($count == 0){
$data['valida'] = 'zero';
echo json_encode($data);
exit;
}
And when I have elements in the list, I have to create a pdf, but at the moment of printing with the script the pdf the else of the validation in the succes:
}else{
alert("Lista guardada");
location.reload(true);
}
It does not work, it does not do the alert and it does not refresh the page, my question is, is there any other way than by json to return script variables to the success of the request to be able to use them within the success?