success function in ajax jquery is not being called with Json datatype?? it always jumps to error function??pls tell me whats wrong with this code!! //jquery ajax code
$.ajax({
type: 'GET',
dataType: 'json',
async:false,
contentType:'application/json', data:'fname1='+$('#fname').val()+'&lname1='+$('#lname').val()+'&email1='+$('#email').val()+'&contact1='+$('#contact').val()+'&password1='+$('#password').val(),
beforeSend: function(x){
if(x && x.overrideMimeType){ x.overrideMimeType("application/json;charset=UTF-8");
}
},
url: "./register.php",
success:function(data) {
if(data1.success=="true")
{
$('#register_response').html("<img src='images/36.gif' alt='Signing Up..' />");
$('#dialog').fadeOut('slow');
$('#overlay').fadeOut('slow');
window.location="index.php";
}
},
error: function(data) {
console.log(textStatus, errorThrown); }
});
this my php code from where m accepting json data!!
//php code
<?php
$fname=$_POST['fname1'];
$lname=$_POST['lname1'];
$contact=$_POST['contact1'];
$email=$_POST['email1'];
$pass=$_POST['password1'];
require("connect.php");
$qry="SELECT * FROM register WHERE $email='$email'";
$result=mysql_query($qry,$con);
$count=mysql_num_rows($result);
if($count==0)
{
$data = array('success'=>"true");
echo json_encode($data);
$query="INSERT INTO register(fname,lname,contact,email,password)VALUES('$fname','$lname','$contact','$email','$pass')";
$rslt=mysql_query($query,$con);
setcookie("user",$email,time()+36000);
}
else
{
$data = array('success'=>"false");
echo json_encode($data);
}
mysql_close($con);
?>
You have an extra $
in your query, it should be:
$qry="SELECT * FROM register WHERE email='$email'";
You need to add error checking so you'll see these errors:
$result = mysql_query($qry, $con) or die (mysql_error($con));
if your return values are only true or false, you can skip using json and try using a simple jQuery.post or jQuery.get http://api.jquery.com/jQuery.get/
var data = 'fname1=' + $('#fname').val() + '&lname1=' + $('#lname').val() + '&email1=' + $('#email').val() + '&contact1=' + $('#contact').val() + '&password1=' + $('#password').val();
jQuery.get('/register/'+data,function(response){
if (response == "true") {
$('#register_response').html("<img src='images/36.gif' alt='Signing Up..' />");
$('#dialog').fadeOut('slow');
$('#overlay').fadeOut('slow');
window.location = "index.php";
}else{
//handle your error
}
});