简体   繁体   English

AJAX没有从php返回变量

[英]AJAX not returning a variable from php

I know there is a few questions like this on here. 我知道这里有几个这样的问题。 but I have done a lot of researching and bug fixing all day to try work out why my ajax does not return a response from the php file. 但是我整天做了大量的研究和错误修复,试图解决为什么我的ajax没有从php文件返回响应。 All I want is for it to tell me a user has been registered so I can let the user move on with the signing up process. 我想要的只是告诉我一个用户已经注册,所以我可以让用户继续注册过程。 And I just need someones wise guidance to tell me what I am doing wrong!! 我只需要有人明智的指导告诉我我做错了什么!

so I wont bore you with the validation part of the js file just the ajax 所以我不会厌倦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;
}

currently with this, if I remove the dataType bit the alert bit happens but currently with it there nothing does. 目前有这个,如果我删除dataType位警报位发生,但目前与它没有任何关系。

again I will just skip to the chase on the php file 再次,我将跳过对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'));

Any help would be amazing!!!!! 任何帮助都会很棒!!!!!

updated code: 更新的代码:

  <?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>

the last line it should be 它应该是最后一行

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

see the added > in the array declaration, that is used to assign arrays with keys. 请参阅数组声明中添加的> ,用于为数组分配键。

also in general you should put a error capture in your ajax statement, see this answer for more info 一般来说,您应该在ajax语句中添加错误捕获, 请参阅此答案以获取更多信息

EDIT: Ok wow, that's some spaghetti code you have there, but after a little clean-up your problem is too many closing braces } you have to remove the } just before the following line also get rid of the closing and opening tags around this line, they serve no use. 编辑:好的哇,这是你在那里的一些意大利面条代码,但经过一点点清理你的问题是关闭括号太多}你必须在下面的行之前移除}也摆脱这个关闭和打开标签他们没有用。

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

} else if (loggedIn()) {

    echo 'you are already registed and logged in';

}

I should also mention two other issues with your code 我还应该提到代码的另外两个问题

  1. You are accepting input from the user without cleaning it up and testing it properly. 您正在接受用户的输入而不进行清理并正确测试。 This is no no read here to find out more 这不是没有在这里阅读以了解更多
  2. You are using mysl_ functions, these are old and depreciated they are also security risks. 您正在使用mysl_函数,这些函数已经过时且折旧,它们也存在安全风险。 Check out PDO instead 请查看PDO

EDIT: 编辑:

Add ini_set('error_reporting',1); 添加ini_set('error_reporting',1); to the top of your php script. 到你的PHP脚本的顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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