0

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

  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.

again I will just skip to the chase on the php file

    $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>
  • Your ajax success method has a parameter of 'result', but you are logging 'response'. This is one issue along with other listed below. – mambrow May 08 '14 at 21:02

1 Answers1

2

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

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. Check out PDO instead

EDIT:

Add ini_set('error_reporting',1); to the top of your php script.

Community
  • 1
  • 1
dops
  • 800
  • 9
  • 17
  • Hey thanks, Still no luck with the '>' added but i did add the error bit and this was the response: OK validation.js:242 parsererror validation.js:243 SyntaxError {stack: (...), message: "Unexpected token <"} any ideas? – Jordanbaggs May 08 '14 at 16:51
  • @Jordanbaggs There is an issue somewhere in your php script causing the error, If you could edit your post and add your php code maybe I could see it or if you have firebug, right click on the ajax call to your script and click 'open in new tab' this allows you to simulate the POST to the script and see the full error page. I do this a lot when I'm testing ajax calls. – dops May 08 '14 at 16:56
  • @Jordanbags also I suspect if you take away the `dataType: 'json',` now you would get a better error – dops May 08 '14 at 16:58
  • hey thanks for that info i will take a look! i updated my php code im still getting the same error.. I'm removing the one your comment is pointing at. any ideas? – Jordanbaggs May 08 '14 at 17:27
  • have you moved the `dataType: 'json',` from your ajax call, all your error messages in the php are not json encoded. You might get a better idea of the php error. – dops May 08 '14 at 17:30
  • so put the dataType back? when I do i get this error SyntaxError {stack: (...), message: "Unexpected token <"} message: "Unexpected token <" stack: (...) get stack: function () { [native code] } set stack: function () { [native code] } __proto__: Error – Jordanbaggs May 08 '14 at 17:54
  • I should also mention that the form does submit.. And the database is updated.. So it's only where I'm trying to push the message back where there is a problem. Well I think so anyway! – Jordanbaggs May 08 '14 at 18:20
  • @Jordanbaggs I think something in the script is causing an error. I meant that the dataType should be removed, I think that error message would be more helpful – dops May 08 '14 at 19:47
  • Ok so I took the dataType bit out and there was no error message but the variable is undefined.. Sorry to keep pestering you! – Jordanbaggs May 08 '14 at 20:00
  • @Jordanbaggs no problem, I've edited my answer, see what error you get after you have made that edit. – dops May 08 '14 at 20:04
  • right I added that line and it returned null. I then deleted the return false bit just to see what happens.. and when the php runs it echos out the response array.. – Jordanbaggs May 08 '14 at 20:25
  • okay cool, but where was the return false bit? I can't see it on your code – dops May 08 '14 at 23:55
  • oh sorry its just after the ajax.. I'll amend code. – Jordanbaggs May 09 '14 at 06:50