I'm currently working on a basic ajax comment form that includes a textarea and a yes/no radio button.
- If the user selects 'yes', their comments are posted and then they are redirected to a new page.
- If the user selects 'no', their comments are still posted, but they remain on the same page.
However, I've encountered an issue where when users select 'yes', the comments are not posted and the page redirects anyways.
Below is the current script:
// Grabbing the value of the yes/no radio button
flag_yesno = getRadioValue('form_comments', 'flag_yesno');
// Initiating POST request to process.php with timestamp appended
request.open('POST', 'process.php?t=' + new Date().getTime());
request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
request.onreadystatechange = function()
{
if(request.readyState == 4)
{
// Display response text in the comment_posts div
comment_posts.innerHTML = request.responseText;
}
}
// Setting up POST parameters
var params = "&foo=" + foo + "&bar=" + bar;
// Sending the POST request
request.send(params);
// Handling redirection after request.send() is triggered
if (flag_yesno=='yes')
{
window.location = '/foo.php?foo='+foo;
}
Is there a way to properly manage the redirect without interfering with the request.send()
function?