views:

118

answers:

3

Thanks for checking out my problem... I'm having trouble submitting a login form via Ajax for a php script to run and return a new set of html items which will be replacing the HTML in #userlinks.... heres what I have so far

$("#login_form").submit(function() { return false; });

$('#login_button').click(function(event) {
    $form = $(this).parent("form");
    $.post($form.attr("action"), $form.serialize(), function(data){
        $('#userlinks').html(data);
    }); 
});

HTML:
    <form id="login_form" name="login" action="login.php">

PHP:
$username = empty($_COOKIE['username']) ? '' : $_COOKIE['username'];
$id = empty($_COOKIE['id']) ? '' : $_COOKIE['id'];

if ($username && $id) {
throw new RedirectBrowserException('index.php');
} else {
$action = empty($_POST['action']) ? '' : $_POST['action'];
if ($action == 'do_login') {
    $username = empty($_POST['username']) ? '' : $_POST['username'];
    $password = empty($_POST['password']) ? '' : $_POST['password'];
    echo handle_login($username, $password);
} else {
    throw new RedirectBrowserException('index.php');
}
}

The php script checks for post data of username && password, yet all I'm getting is a page refresh and no changes. Nothing returns unless I turn login_button into a link and change the script a bit... Any help would be appreciated!

A: 

Try doing the following

//$("#login_form").submit(function() { return false; }); <-- remove this line
$('#login_button').click(function(event) {
    $form = $(this).parent("form");
    $.post($form.attr("action"), $form.serialize(), function(data){
        $('#userlinks').html(data);
    });
    return false;
});
Nalum
Thanks for your response!Problem is it's directing to login.php now and showing me what the function returns. Same issue as what's above..
Stevie Jenowski
+1  A: 

Try this:

$("#login_form").submit(function(event){

    $.post($(this).attr("action"), $(this).serialize(), function(data){
        $('#userlinks').html(data);
    });

    event.preventDefault();  
});
Thanks for your response!Problem is it's directing to login.php now and showing me what it returns. Same goes with the suggestion below...
Stevie Jenowski
What does it return?
what should be in #userlinks
Stevie Jenowski
I had some code wrong. It should work as that's how I use it.
Here's my HTML, is this properly formatted for this?<form id="login_form" name="login" action="login.php">
Stevie Jenowski
Seems ok. If you've got firebug use that to see if there's any errors when you log in. Other than that the only thing i can think of is that you haven't got the code within $(document).ready(function(){ });
Firebug comes up with no errors, It's running the login script and logging me in, it just redirects me to login.php printing out exactly what should be in #userlinks
Stevie Jenowski
and it is within the (document).ready
Stevie Jenowski
Any chance you could post the link to it on here? Might make it easier so work out
Try changing the name of the button from submit to submitLogin. Then change it in the login.php if it is used. I've found that submit doesn't work properly if the name and/or id is the same as the type.
Alright, so I changed the button to submitLogin, yet now it reloads the index and doesn't log in
Stevie Jenowski
if you've used $_POST['submit'] in login.php you will need to change it to $_POST['submitLogin']
I'm using: edit, look's horrid in a comment, put it in the original question
Stevie Jenowski
try changing this $("#login_form").submit(function(event){ to $("#login_form").live("submit",function(event){
Tom, you're the man! Thank you sir!
Stevie Jenowski
No worries. The problem is that when the page loads, it tries adding the submit event to the form, which didn't exist and only appears when you click the login link. .live makes any future object (that appearing throught ajax for example) take on that event.
Ahhh that makes more sense now, appreciate it man!
Stevie Jenowski
A: 

What happens if the login fails ?

mexique1