I need your help to try to sort out an issue with ajax callback. What happen is that the php script is called without issues, the query works fine and it generate the json output, but the callback doesn't works. No div is displayed on success and no changes on class happens.
This is my form
<form class="form-inline">
<input type="hidden" id="id_pro" value="1">
<input type="hidden" id="status" value="1">
<button id="submit" type="button" class="btn btn-link">
<span id="check" class="glyphicon glyphicon-check" title="my tytle"></span>
</button>
</form>
<span class="error" style="display:none"> Please Enter Valid Data</span>
<span class="success" style="display:none"> Form Submitted Success</span>
This is the js part
$(function() {
$("#submit").click(function() {
var id_pro = $("#id_pro").val();
var status = $("#status").val();
var dataString = 'id_pro='+ id_pro + '&status=' + status;
if(id_pro=='' || status=='')
{
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
}
else
{
$.ajax({
type: "POST",
url: "myphppage.php",
data: dataString,
datatype: 'json',
success: function(data)
{
if(data.result=='1')
{
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
$("#check").attr('class', data.check);
}
}
});
}
return false;
});
});
And this is the php part
<?
if($_POST)
{
$id_pro=$_POST['id_pro'];
$status=$_POST['status'];
if($status==0){$status=1;}else{$status=0;}
if ($mysqli->query("UPDATE mytable SET online=".$status." WHERE id=".$id_pro." ") === TRUE) {
header("Content-type: application/json");
$data = array('check'=>'new_class','check_text'=>'new text','result'=>'1');
print json_encode($data);
}
else
{
header("Content-type: application/json");
$data = array('result'=>'0');
print json_encode($data);
}
$result->close();
}else { }
?>
Any idea? Thank you in advance
console.log(data);datatypetypo here.code{"check":"new_class","check_text":"new text","result":"1"}code. What is strange is an error 500: POST myphppage.php 500 Internal Server Errordatatype->dataType, though, since your php is returning the appropriate contentType header, it's not really needed anyway.