简体   繁体   中英

cant get success message of ajax

I don't know why I couldnt get the sucess message in JSON. The data is inserted good to database, so it works but the callback message (success ) no. I don't know why.

this file is where to insert data in database.

include ("connect_to_mysql.php");
if ( isset($_POST['register'])) {
    $First_Name = mysql_real_escape_string($_POST['firstname']);
    $Last_Name = mysql_real_escape_string($_POST['lastname']);
    $Email = mysql_real_escape_string($_POST['email']);
    $Password = mysql_real_escape_string($_POST['password']);
    $Gender = mysql_real_escape_string($_POST['gender']);

    $sql3 =mysql_query("SELECT email FROM members WHERE email = '$Email' ");
    $result3= mysql_num_rows($sql3) ;
    $found ='';
    if ($result3 == 1 ) { 
        $found = "email already exist ! ";
    } else {
        mysql_query("INSERT INTO members (firstname, lastname,gender, email, password,bio_body, sign_up_date , account_type)
        VALUES( '".$First_Name."', '".$Last_Name."','".$Gender."','".$Email."','".$Password."','NULL' ,NOW() , 'a' )  ");
        $found = "registered succefully !";
    }
    echo json_encode(array('returned_val' => "$found"));
    //-- i have tried this also echo json_encode(array('returned_val' => $found));
}

this is my ajax

$(document).ready(function(){

    $('#register').click(function() {
        $('#mydata').html('Saving your data...<img src="images/loading.gif" />');
        $.ajax({
            url: "sendata.php",
            dataType: 'json' ,
            data: {
                firstname: firstname.value,
                lastname: lastname.value,
                email: email.value,
                password: password.value,
                gender: gender.value
            },
            type: 'POST',
            success: function(data){
                $('#mydata').html("<span style='font-size:18px; color: green;'> Your data is saved succefully! </span>").delay(3000).fadeOut('fast');
                alert(data.returned_val);
                alert (1);
            }
        });
    });
});

OBS: about mysql or some security stuff (passowrd) I know , I will fix them when I fix this problem.

thanks for your time.

Edit: I have no error message

EDIT. html form

<form action="" method="post" enctype="multipart/form-data">
    <table width="100%" class="noborder" >
        <tr>
            <td width="23%" height="47" class="right">First Name:</td>
            <td width="77%" class="left left_nowrap">
                <input type="text" class="left left_nowrap tb10  border1" name ="firstname" id="First_Name" value="<?php echo $_SESSION['firstname'];  ?>" />
            </td>
        </tr>
        <tr>
            <td height="47" class="right">Last Name:</td>
            <td valign="middle" class="left left_nowrap">
                <input type="text" class="left  left_nowrap tb10 border1" name ="lastname" id="Last_Name"  value="<?php echo  $_SESSION['lastname'];  ?>" /><br/>
            </td>
        </tr>
        <tr>
            ........
            <td valign="middle" class="left"><input id="register" name="register" type="button" class="submit1 tb10 border4" value="Sign - Up" /></td>

You can use the error callback ( documentation ) that will be triggered if jQuery thinks that something has gone wrong sending the data, for example:

$.ajax({
  url: "sendata.php",
  dataType: 'json' ,
  data: {
    firstname : firstname.value,
    lastname : lastname.value,
    email : email.value,
    password : password.value,
    gender : gender.value
  },
  type : 'POST',
  success: function(data){
    $('#mydata').html("<span style='font-size:18px; color: green;'> Your data is saved succefully! </span>").delay(3000).fadeOut('fast');
    alert(data.returned_val);
    alert (1);
  },
  error: function(data){
    console.log(data);
  }
});

By looking at the data printed in the console, you should be able to tell why jQuery doesn't invoke your success callback.

You should check the PHP error log. There has to be something there. Your javascript is not getting back any answer, probably, due to some error on the PHP side. If there is nothing in the PHP error log, then implement the error handler in the ajax request, there has to be something there.

使用console.log(data)快速验证ajax响应。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM