...

View Full Version : PHP redirect after ajax form validation



Schteeve
11-15-2007, 11:56 AM
Whilst trying to create a user friendly registration system i found an ajax form validator (http://www.roscripts.com/Ajax_form_validation-152.html) this works by sending the form data to register.php which in turn processes the data and displays an error above the form if anything is incorrect or missing.

However, the problem arises when i try to redirect the user to the next stage of registration.


if(is_array($errors))
{
echo '<p class="error"><b>The following errors occured</b></p>';
while (list($key,$value) = each($errors))
{

echo '<span class="error">'.$value.'</span><br />';
}
}
else {
//store to db, send email
header("Location: new_page.php?username=$username");
exit();
}

The redirected page is displayed where there error messages were???

Does anyone see a way of redirecting so that the new page is opened as the parent page?

example (http://www.eightballmedia.co.uk/test)
source files (http://www.eightballmedia.co.uk/test/ajax_validator.rar)

Any help is greatly appreciated!!!!

shyam
11-15-2007, 12:20 PM
you should return an empty message if the validations were successful and actually POST the form contents to another php script so that they are stored in the database...

name _F1
11-15-2007, 12:32 PM
You'll need to redirect the form using Javascript, rather than PHP. Generate a blank page on the validation page and then have Javascript check if the returned value == ''. If so, submit the form (using formNode.submit() ).

Schteeve
11-15-2007, 12:51 PM
Sounds like this is alot more complicated than i envisaged.

Shyam, sounds like that'll work. As a fairly new coder could you give me a bit more detail?

Won't this need to redirect to another page and hence encounter the same problem?

Schteeve
11-16-2007, 10:30 AM
Any further sugestions on this.

I like the functionality of this but am willing to try a different method if anyone has any suggestions?

Thanks in advance :confused:

name _F1
11-16-2007, 11:01 AM
What AJAX does is return whatever is output on the page which is called. In this example, AJAX is calling the PHP page, which prints the error message. The error message is returned to the function that called it (in your case, it inserts the error message onto the page). When you redirect the page that you have called from AJAX, it doesn't affect the page which is running the AJAX, it just means that the script returns the output of the page you redirected to.

You need to redirect the actual page which you want to redirect. Post the Javascript code and we can help. :)

Schteeve
11-16-2007, 12:15 PM
I see, that makes sense!

Sorry if i'd realised this wasnt a php prob i would've posted in the javascript forum.


<script type="text/javascript" src="script/mootools.js"></script>
<script type="text/javascript">
window.addEvent('domready', function(){
$('registerForm').addEvent('submit', function(e) {
new Event(e).stop();
var log = $('log_res').empty().addClass('ajax-loading');
this.send({
update: log,
onComplete: function() {
log.removeClass('ajax-loading');
}
});
});
});
</script>

Where log_res is the div displaying the errors

name _F1
11-16-2007, 12:33 PM
I'm not familiar enough with Mootools to help much, but you should check if the value that is returned (which is currently inserted directly into the log_res element) is equal to an empty string, and if so, use formNode.submit(). To return an empty string if there are no errors on your PHP page simply remove the else clause.

EDIT: Actually, using node.submit() won't work because you're executing the AJAX query onsubmit of that form, and cancelling the normal process. You could try executing the AJAX function before you stop the event, and then stopping the event only if there are errors. I think that the page will wait for the Javascript to complete before submitting the form.

As an aside, make sure you are also testing the form contents when you actually submit the form, as people can easily get around Javascript-based form validation (which is what this is).

Schteeve
11-16-2007, 01:05 PM
Thanks, you have been very helpful.

Am a bit out of my depth with javascript hence most of the validation is being done via PHP.

Am using the javascript to display the errors on the same page rather than forcing the user to click back on a results page.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum