簡體   English   中英

AJAX沒有從php返回變量

[英]AJAX not returning a variable from php

我知道這里有幾個這樣的問題。 但是我整天做了大量的研究和錯誤修復,試圖解決為什么我的ajax沒有從php文件返回響應。 我想要的只是告訴我一個用戶已經注冊,所以我可以讓用戶繼續注冊過程。 我只需要有人明智的指導告訴我我做錯了什么!

所以我不會厭倦js文件的驗證部分只是ajax

  if(ValidationComplete == true){
   var that = $(this),
url = that.attr('action'),
type = that.attr('method'),
data = {};

that.find('[name]').each(function(register, value) {
    var that = $(this),
        name = that.attr('name'),
        value = that.val();

        data[name] = value; 
});

    $.ajax({
    url:url, 
    type:type,
    data: data,
    dataType: 'json', 
    success: function(result){
        alert(result.status);
        console.log(result.data);

    },
error: function(xhr, textStatus, error){
  console.log(xhr.statusText);
  console.log(textStatus);
  console.log(error);
   }        

});

return false;
} else {
    return false;
}

目前有這個,如果我刪除dataType位警報位發生,但目前與它沒有任何關系。

再次,我將跳過對php文件的追逐

    $query = "INSERT INTO person 
   VALUES('','$first_Name','$surname','$email','$dob','$password',
  '1','','0','1','','','','$emailCode')";
  if($query_run =mysql_query($query)) {
      echo json_encode(array("response"='true'));

任何幫助都會很棒!!!!!

更新的代碼:

  <?php    

if( isset($_POST['firstname']) && 
isset($_POST['surname']) && 
isset($_POST['email']) && 
isset($_POST['day']) && 
isset($_POST['month']) && 
isset($_POST['year']) && 
isset($_POST['password']) && 
isset($_POST['re_type_password'])){

  $first_Name = $_POST['firstname'];
  $surname = $_POST['surname'];
  $email = $_POST['email'];
$password = $_POST['password'];
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$re_type_password = $_POST['re_type_password'];
$emailCode = md5($_POST['$first_Name'] + microtime());

if(!empty($first_Name)&& 
!empty($surname)&& 
!empty($email)&& 
!empty($day) && 
!empty($month) && 
!empty($year) && 
!empty($password)&&                                   
!empty($re_type_password)){



  if(strlen($firstname)>30 || strlen($surname)>30 || strlen($email)>50){
  echo 'the data enetered is to long';
} else {
  if($password != $re_type_password){
  echo 'passwords do not match, please try again.';
} else{ 
$query = "SELECT email FROM person WHERE email ='$email'";
$query_run = mysql_query($query);
if(mysql_num_rows($query_run)==1){
  echo 'Email address already on databse';         
} else{
  if($day>31 || $month>12){
    echo 'date of birth wrong';
  } else{

    $dob= $year.'-'.$day.'-'.$month;
  $query = "INSERT INTO person 
  VALUES('','$first_Name','$surname','$email','$dob','$password'
   ,'1','','0','1','','','','$emailCode')";
   if($query_run =mysql_query($query)) {
      email($email, 'Email Confirmation', "hello ". $first_Name." ,
    \n\n you need to   activate your account so click the link  ");
    $return_data['status'] = 'success';
     echo json_encode($return_data);
  } else {
      echo @mysql_error();
  }

}

 }

  } 
  }

  } else {
      echo "<p id='error'> All fields are required. Please try again.</p>";

  }
      }

   ?>

  <?php
  } else if (loggedIn()) {

echo 'you are already registed and logged in';

   }


  ?>

   </body>
  </html>

它應該是最后一行

echo json_encode(array("response"=>'true'));

請參閱數組聲明中添加的> ,用於為數組分配鍵。

一般來說,您應該在ajax語句中添加錯誤捕獲, 請參閱此答案以獲取更多信息

編輯:好的哇,這是你在那里的一些意大利面條代碼,但經過一點點清理你的問題是關閉括號太多}你必須在下面的行之前移除}也擺脫這個關閉和打開標簽他們沒有用。

} // <------- THIS ONE!

} else if (loggedIn()) {

    echo 'you are already registed and logged in';

}

我還應該提到代碼的另外兩個問題

  1. 您正在接受用戶的輸入而不進行清理並正確測試。 這不是沒有在這里閱讀以了解更多
  2. 您正在使用mysl_函數,這些函數已經過時且折舊,它們也存在安全風險。 請查看PDO

編輯:

添加ini_set('error_reporting',1); 到你的PHP腳本的頂部。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM