0

I would just like to know how to go about comparing the resulting echo from a $.ajax call in JavaScript. I attempted this and even though I get 1, it doesn't actually compare the results correctly.

jQuery:

        $.ajax({
        type: "POST",
        url: "login.php",
        data: user,
        dataType: 'html',
        success: function(result)
        {
            alert(result);
            if(result == '1')
            {
                alert("logged in :D");
                //document.location.replace('home.php');
            }
            else
            {
                alert("not logged in :<");
            }
        },
        failure: function()
        {
            alert('An Error has occured, please try again.');
        }
    });

PHP:

<?php
session_start();
$host = "localhost";
$user = "root";
$passw = "";
$con = mysql_connect($host, $user, $passw);


if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

$json = $_REQUEST['json'];
$json = stripslashes($json);

$jsonobj = json_decode($json);

$password = $jsonobj->password;
$email = $jsonobj->email;

mysql_select_db("tinyspace", $con);

$result = mysql_query("SELECT 1 FROM users WHERE email = '"
                    . $email . "' AND password = '" . $password . "'");

 while($info = mysql_fetch_array( $result )) 
 { 
    if($info[0] == 1)
    {
        echo '1';
    }
}
 ?> 
pimvdb
  • 151,816
  • 78
  • 307
  • 352
Crossman
  • 278
  • 5
  • 18
  • what do you mean it doesn't compare correctly? – jbabey Oct 16 '12 at 19:48
  • @jbabey It echos back 1 to the Call, but does not see them as equal – Crossman Oct 16 '12 at 19:49
  • Use firebug, form data is posting in correct way, also what's AJAX response ? – Riz Oct 16 '12 at 19:49
  • 1
    Warning: your code is vulnerable to [SQL injection](http://stackoverflow.com/questions/332365/xkcd-sql-injection-please-explain) – bfavaretto Oct 16 '12 at 19:52
  • Also failure: function() is supposed to be error: function() – Sushanth -- Oct 16 '12 at 19:52
  • 2
    Does the result contain the trailing space your posted PHP code has? Try removing it or comparing against `'1 '`. – pimvdb Oct 16 '12 at 19:53
  • have you tried comparing to the number 1 instead of the string 1? – mistersender Oct 16 '12 at 19:49
  • He is not using === , so that should not be a problem – Sushanth -- Oct 16 '12 at 19:50
  • Please, do not post requests for clarifications as answers. When you reach 50 reputation, you'll be able to post comments anywhere. – bfavaretto Oct 16 '12 at 19:50
  • @Sushanth-- Ah thank you, :< moment of stupidity. Thought the echo would return a char or a string. – Crossman Oct 16 '12 at 19:50
  • Like @pimvdb suggests try trimming the whitespace. You can use `$.trim(result)` – Snuffleupagus Oct 16 '12 at 19:58
  • I don't expect that it really matters in this case, but the dataType you specify is HTML and the response is not HTML. – Semicolon Oct 16 '12 at 20:01
  • I don't think that's true, @Snuffleupagus. From jQuery's documentation: "Returns HTML as plain text; included script tags are evaluated when inserted in the DOM." So it's harmless to use for text (if there are no script tags in the response), but the real type for plain text is "text". – Semicolon Oct 16 '12 at 20:05
  • @Semicolon Haha yeah, I deleted that. I skimmed the docs and caught `(xml, json, script, or html)` and didn't see that it was only for intelligent guessing. – Snuffleupagus Oct 16 '12 at 20:07

2 Answers2

0

There's probably a space or line break after the '1' that is echoed. Check if there's no space before the opening <?php tag and remove the closing ?> tag (you're allowed to do that, and it will prevent accidental whitespace being outputted.

You should be able to check by changing the javascript to:

alert('X' + result + 'X');

You'll see soon enough if there's any whitespace around result.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
0

try to send json response

php:

echo json_encode(array(
    'status' => 'ok',
));

js:

dataType : "json",
success : function(response) {
    if (response.status == "ok") {
        alert('success');
    } else {
        alert('error');
    }
}
ladamalina
  • 56
  • 2