1 (edited by littlebigfred 2010-07-31 09:07)

Topic: [SOLVED] AJAXing login form?

Hello

I can succesfully use the script shown in the Login form outside the forum section of the "PunBB 1.3 integration" page.

However, I'd like to turn this standard form into an AJAX alternative so that I put this code in a DIV and only update this section of the page.

I tried rewriting the code, but it fails with the following error:

Confirm action

Please confirm or cancel your last action

Unable to confirm security token. A likely cause for this is that some time passed between when you first entered the page and when you submitted a form or clicked a link. If that is the case and you would like to continue with your action, please click the Confirm button. Otherwise, you should click the Cancel button to return to where you were.

Here's the code, using jQuery for the AJAX call:

This is test.php
<?php
define('FORUM_ROOT', '/var/www/nginx-default/punbb/');
require FORUM_ROOT.'include/common.php';

$forum_page['redirect_url'] = "/blog/test.php";

$forum_page['form_action'] = forum_link($forum_url['login']);
$forum_page['hidden_fields'] = array('form_sent'=> '<input type="hidden" name="form_sent" value="1" />',
        'redirect_url' => '<input type="hidden" name="redirect_url" value="'.forum_htmlencode($forum_page['redirect_url']).'" />',
        'csrf_token' => '<input type="hidden" name="csrf_token" value="'.generate_form_token($forum_page['form_action']).'" />'
);

echo "<div id='subscribers_list'>\n";
?>
    <?php echo implode("\n\t\t", $forum_page['hidden_fields'])."\n" ?>
    
    User:
    <input type="text" id="fld1" name="req_username" value="" />
    <br />
    
    Password:
    <input type="password" id="fld2" name="req_password" value="" />
    <br />
    
    <input type="button" id="mybutton" name="mybutton" value="Login" />
<?php
}
echo "</div>\n";
?>

<script type="text/javascript" src="/blog/admin/includes/js/jquery/jquery.js"></script>
<script type='text/javascript'>
$("#mybutton").click(function() {
        switch($("#mybutton").attr("value")) {
                case "Login":
                        var username = $("#req_username").val();
                        var password = $("#req_password").val()
                        //hidden vars set through implode() above
                        var form_sent = $("#form_sent").val();
                        var redirect_url = $("#redirect_url").val();
                        var csrf_token = $("#csrf_token").val();
                        $("#subscribers_list").load("<?php echo $forum_page['form_action'] ?>", {username : username, password : password, form_sent : form_sent, redirect_url : redirect_url, csrf_token : csrf_token});
                        break;
        }
})
</script>

Has someone succeeded in using an AJAX version of the login script?

Thank you.

2

Re: [SOLVED] AJAXing login form?

Hi

Well I guess one problem could be with these variables - input in forms don't have IDs (except those "fld1" and so, which won't help you wery much).

var username = $("#req_username").val();
...

Guess proper code should be like this

var username = $("input[name=req_username]").val();
...

Eraversum - scifi browser-based online webgame

3 (edited by littlebigfred 2010-07-31 00:17)

Re: [SOLVED] AJAXing login form?

Thanks a lot for the tip. I hadn't noticed that the hidden fields only had names and not ids.

I'm getting a bit further, but PunBB now complains that the username/password is wrong, although I can see in the proxy that my test.php does send the correct information:

POST http://192.168.0.3/punbb/login.php HTTP/1.1
Referer: http://192.168.0.3/blog/test.php

username=admin&password=test&form_sent=1&redirect_url=%2Fblog%2Ftest.php&csrf_token=58009a05448ccac128f352df01f28a5deb00f571

Warning! The following errors must be corrected before you can login:
* Incorrect username and/or password.


Thank you.

4 (edited by Grez 2010-07-31 00:42)

Re: [SOLVED] AJAXing login form?

Logging script expects variables $_POST['req_username'] and $_POST['req_password'] - you are sending 'username' and 'passwordwink

Eraversum - scifi browser-based online webgame

Re: [SOLVED] AJAXing login form?

Stupid me smile I copy/pasted the line from my first test and forgot to update it to actually match the var names expected by PunBB. Works great now, thank you.

Thank you.