0

Having a problem getting a form with ajax to get a php response. The javascript seems right because it works if I erase everything on login_ajax.php and only use the following

echo 'CORRECT' 
//or 
echo 'INCORRECT'

Once I use the real php code, the ajax doesn't get any response from the PHP. Even more weird if I remove the

return false

from the javascript and then submit the form, I do see CORRECT or INCORRECT being displayed on the login_ajax.php in the browser.

HTML:

<form id="login" action='login_ajax.php' method="get">

    <label>Email: <input type="text" name="email" id="email" value="[email protected]"></label>
    <label>Password: <input type="text" name="password" id="password" value="x"></label>

    <input type="submit" value="login">
    <div id="straight_response">php response here</div>
    <div id="message">status</div>
</form>

PHP:

if (isset($_GET['password'])) {
    $email = $_GET['email'];
    $password = $_GET['password'];
    $stored_pass = "123";

    if ($password == $stored_pass) {
        echo "CORRECT";
    } else {
        echo "INCORRECT";
    }
}
//Javascript works when using one of the two lines below instead of the code above
//echo 'INCORRECT';
//echo 'CORRECT'

JAVASCRIPT:

$('#login').submit(function(){


var email;
    var password;

    email       = $('#email').val();
    password    = $('#password').val();

    var data = {};
    data.email  = email;
    data.password   = password;

    var options = {};
    options.dataType = 'text';
    options.type = 'get';
    options.url= 'login_ajax.php';
    options.success = function (response) {
    //  jQuery.trim(response);
        console.log(response.results);
        console.log(response.query);
        $('#always').text(response);

        if (response === 'CORRECT') {
            $('#message').text('you got it right');
            console.log("good combination");
        }
        else if (response === 'INCORRECT') {
            $('#message').text('sorry, try again');
            console.log("bad combination");
        }
    };

    $.ajax(options);
    return false;
});//#login function
7
  • 1
    Perhaps it would be beneficial to see your PHP code as well. Commented Apr 18, 2013 at 22:41
  • 3
    Where in your javascript code are you submitting the login form data to login_ajax.php? Commented Apr 18, 2013 at 22:43
  • first thing to do is to dump out what's in your $_GET array. (You might want to use post, anyway, to reduce problems with browser caching.) See if it matches what you expect. Commented Apr 18, 2013 at 23:08
  • Also using post won't advertise people's passwords quite as flagrantly. Although they still won't be safe. Commented Apr 18, 2013 at 23:09
  • @JerrySeeger I would like if you could elaborate on the password advertising. We often overlook AJAX's security issues, it would be great to know a bit more of why it would make the difference. Commented Apr 18, 2013 at 23:12

1 Answer 1

1

Oh god I actually spent 40 minutes trying to understand your problem inside out, and turns out that the fact is that you don't pass the data you collect from the form to the PHP through AJAX.

Easy fix, add this line before you make your call:

options.data = data;

Though, as the documentation states:

It is often unnecessary to directly call this function, as several higher-level alternatives like $.get() and .load() are available and are easier to use.

In your case I think $.get() works just as fine. Although you might want to take a look of what JerrySeeger said in the comments, looks like using GET for password might represent a huge security hole, also you should perhaps be using SSL.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much. Just looking at your response I knew that would definitely be it. I knew about the data too but I just couldn't see it. Thank you again.
and no worries about the security I'm just using a login form as an example. The real thing is not a login form but an image upload and process script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.