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.