Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post PHP redirect after ajax form validation

    Whilst trying to create a user friendly registration system i found an ajax form validator 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.

    PHP Code:
    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
    source files

    Any help is greatly appreciated!!!!
    Last edited by Schteeve; 11-15-2007 at 01:43 PM.
    You think anybody wants a roundhouse kick to the face while I'm wearing these bad boys? Forget about it.
    <!------------o\/o-----------?>

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    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...
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    Regular Coder
    Join Date
    Jun 2006
    Posts
    225
    Thanks
    6
    Thanked 3 Times in 3 Posts
    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() ).

  • #4
    New Coder
    Join Date
    Jun 2007
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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?
    You think anybody wants a roundhouse kick to the face while I'm wearing these bad boys? Forget about it.
    <!------------o\/o-----------?>

  • #5
    New Coder
    Join Date
    Jun 2007
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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
    You think anybody wants a roundhouse kick to the face while I'm wearing these bad boys? Forget about it.
    <!------------o\/o-----------?>

  • #6
    Regular Coder
    Join Date
    Jun 2006
    Posts
    225
    Thanks
    6
    Thanked 3 Times in 3 Posts
    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.

  • #7
    New Coder
    Join Date
    Jun 2007
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I see, that makes sense!

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

    Code:
    <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
    You think anybody wants a roundhouse kick to the face while I'm wearing these bad boys? Forget about it.
    <!------------o\/o-----------?>

  • #8
    Regular Coder
    Join Date
    Jun 2006
    Posts
    225
    Thanks
    6
    Thanked 3 Times in 3 Posts
    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).
    Last edited by name _F1; 11-16-2007 at 01:38 PM.

  • #9
    New Coder
    Join Date
    Jun 2007
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.
    You think anybody wants a roundhouse kick to the face while I'm wearing these bad boys? Forget about it.
    <!------------o\/o-----------?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •