Welcome to Questionaries, where you can ask questions and receive answers from other members of the community.

Let us know at info@questionaries.org

jQuery Ajax form submit, to call php login script

10 like 0 dislike
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!
asked 2 years ago by webmaster (25,380 points)

1 Answer

1 like 0 dislike
 
Best answer
Try this:

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

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

    event.preventDefault();  
});
answered 2 years ago by monirulislam (51,620 points)

Related questions

9 like 0 dislike
1 answer
asked 2 years ago by DBA-boss (120,990 points)
4 like 0 dislike
1 answer
10 like 0 dislike
2 answers
asked 2 years ago by anonymous
9 like 0 dislike
1 answer
asked 2 years ago by eagles11 (179,830 points)
0 like 0 dislike
0 answers