0

I'm learning the way to work with ajax with php now i send request with;

    $.ajax({
        url: 'check_exists.php',
        type: "POST",
        data: registerForm.serialize(),
        success: function(data) {
            console.log(data)
            registerMsg = $('.registerMsg');
            if (data == 'nickname_exists') { 
                nickname.addClass('error'); 
            } else if (data == 'email_exists') { 
                email.addClass('error'); 
            } else {
                registerMsg.html(''); 
            }
        }
    });

Now this send to the php file and checks if data exists if i input nickname i get back nickname_exist back and if i do the same with the email i get email_exists back.

But now if i get both data it console.log like nickname_existsemail_exists this way it doesn't trigger the if statement.

i send from php file like;

require_once('db_connect.php');

if(isset($_POST['nickname'])){

    $nickname = $_POST['nickname'];

    $st = $db->prepare("SELECT * FROM users WHERE nickname=?");

    $st->bindParam(1, $nickname);
    $st->execute();

    if($st->rowCount() > 0) { echo 'nickname_exists';  }
}
if(isset($_POST['email'])){

    $email = $_POST['email'];

    $st = $db->prepare("SELECT * FROM users WHERE email=?");

    $st->bindParam(1, $email);
    $st->execute();

    if($st->rowCount() > 0) { echo 'email_exists';  }
}

How do i fix this, and is the way how i handle ajax to php the right way, can somebody help me a hand.

I need to make it console.log like

nickname_exists

email_exists

INSTEAD OF

nickname_existsemail_exists

Cœur
  • 37,241
  • 25
  • 195
  • 267
John
  • 17
  • 3
  • I am not sure, but your question seems to be how to add a new line rather than about AJAX calls? To add a new line you need "\n" and on windows "\n\r". echo 'nickname_exists\n\r';echo "email_exists\n\r"; – Dawid O Nov 19 '14 at 11:55
  • You might want to consider using [JSON](http://stackoverflow.com/questions/383692/what-is-json-and-why-would-i-use-it) and for PHP there is a function to automatically generate it from an array http://php.net/json_encode – Spokey Nov 19 '14 at 11:56

4 Answers4

0

Use a store variable and use that like below:

require_once('db_connect.php');

$what_exists = [];

if(isset($_POST['nickname'])){

    $nickname = $_POST['nickname'];

    $st = $db->prepare("SELECT * FROM users WHERE nickname=?");

    $st->bindParam(1, $nickname);
    $st->execute();

    if($st->rowCount() > 0) { $what_exists[] = 'nickname_exists';  }
}
if(isset($_POST['email'])){

    $email = $_POST['email'];

    $st = $db->prepare("SELECT * FROM users WHERE email=?");

    $st->bindParam(1, $email);
    $st->execute();

    if($st->rowCount() > 0) { $what_exists[] = 'email_exists';  }
}

echo json_encode($what_exists);

Output that variable as JSON and make a change to AJAX call like:

$.ajax({
        url: 'check_exists.php',
        type: "POST",
        data: registerForm.serialize(),
        dataType: 'json',
        success: function(data) {
          console.log(data)
           registerMsg = $('.registerMsg');
           if ($.inArray(data, 'nickname_exists')) { 
              nickname.addClass('error'); 
           } else if ($.inArray(data,'email_exists')) { 
              email.addClass('error'); 
           } else {
              registerMsg.html(''); 
           }
         }
      });
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
0

Collect your results into an array like this:

require_once('db_connect.php');
//Initalize the array
$result = array(
    'nickname_exists' => 0,
    'email_exists' => 0
);
if(isset($_POST['nickname'])){
    $nickname = $_POST['nickname'];
    $st = $db->prepare("SELECT * FROM users WHERE nickname=?");
    $st->bindParam(1, $nickname);
    $st->execute();
    if($st->rowCount() > 0) { 
        $result['nickname_exists'] = 1;
    }
}
if(isset($_POST['email'])){
    $email = $_POST['email'];
    $st = $db->prepare("SELECT * FROM users WHERE email=?");
    $st->bindParam(1, $email);
    $st->execute();
    if($st->rowCount() > 0) { 
        $result['email_exists'] = 1;
    }
}
//Gives back the result in JSON.
echo json_encode($result);

Then return back with the json. After this you can check all of them in your javascript:

//Initialize the hasError
var hasError = false;
if (data.nickname_exists == 1) {
    //Set error for nickname
    nickname.addClass('error');
    hasError = true;
}
if (data.email_exists == 1) {
    //Set error for email
    email.addClass('error');
    hasError = true;
}
//If we had no error:
if (!hasError) {
    registerMsg.html('');
}
vaso123
  • 12,347
  • 4
  • 34
  • 64
0

You need to return data in json format

echo json_encode(array('nickname_exists'=>1,'email_exists'=>1)); //here 1 is for true and 0 if for false

IN JS YOU NEED TO WRITE THIS CODE

$.ajax({
    url: 'check_exists.php',
    type: "POST",
    data: registerForm.serialize(),
    success: function(data) {
        parsedData = jQuery.parseJSON(data);
        registerMsg = $('.registerMsg');
        if (parsedData.nickname_exists != 1) { 
            nickname.addClass('error'); 
        } else if (parsedData.email_exists !=1) { 
            email.addClass('error'); 
        } else {
            registerMsg.html(''); 
        }
    }
});
Lalit Sharma
  • 555
  • 3
  • 12
0
$.ajax({
        url: 'check_exists.php',
        type: "POST",
        data: registerForm.serialize(),
        success: function(data) {
                myfunction(data);
        }
    });

function myfunction(data){
    console.log(data)
    registerMsg = $('.registerMsg');
    if (data == 'nickname_exists') { 
         nickname.addClass('error'); 
       } else if (data == 'email_exists') { 
          email.addClass('error'); 
       } else {
          registerMsg.html(''); 
       }

}
Faruk Omar
  • 1,173
  • 2
  • 14
  • 22