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 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation PHP header redirect problem

    Hey guys,

    I'm working on a form where you can fill in some data, the next thing is that he checks these values if they are invalid and such. After that, I made a pop-up where the user can confirm if the data he entered is correct.

    Now, I made a confirm box (with some javascript help) and when the user clicks agree, he should get the message that the insert was succesful and that he will be redirected to an other page in 3 seconds. If the user clicks cancel, the application should show the message that the user can click on the back button of their browser to get back to the form again to adjust the values. (may be better if it automatically goes back to the form, but I tried and when it worked, all the entered values were gone, they only remained when I push the back button of my browser)

    Everything works except for the redirect, the confirm box shows up after the checks etc and when I click on agree, it shows the succesful message and the user will get redirected as it should do, but if I click on cancel it will not show the message that it was succesful (so far so good), but he will get redirected in 3 seconds AND the message that he can click on the previous button of the browser isn't showing at all too...

    My code for the confirm box:
    PHP Code:
    echo '<script type="text/javascript">';
        echo 
    'var agree = confirm("Validity: ' $val '\nSupplier: ' $sup '\nType nr: ' $type '\n\nAre you sure the information is correct and that you want to insert it into the database?");';
        
        echo 
    'if(agree == true) {';
                
    header("refresh: 3; url=somepage.php");
                echo 
    'document.write("<p align=\"center\" style=\"margin-top: 50px;\">The component is succesfully inserted, you will be redirected in 3 seconds, or click <a href=\"somepage.php\">here</a> to go now.</p>");';
        echo 
    '} else if(agree == false) {'// << I know, this was just for some debugging, an else is enough.
                
    echo 'document.write("<p align=\"center\" style=\"margin-top: 50px;\">Please click on the Previous button of your browser to change the entered data or click <a href=\"somepage.php\">here</a> to go back to the search screen</p>");
              }'
    ;
    echo 
    '</script>'
    I can't find the problem, please help!
    Thanks for your time!
    'x.

    EDIT: This is my first time posting on a forum about an issue, normally I can solve it on my own with some help from previous posts, but this is a weird one, so please state if you need anything else or if I should do something otherwise in the future.

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    I'm kind of surprised that you are not getting a "headers already sent" error from PHP...

    Anyway, PHP is echoing the header refresh in the process of setting up the javascript (check the page source - you won't see it there either way, but the header is still sent). So no matter what the user does the refresh is made because the header says so.

    Since you want to tie this to javascript processes what you actually want to be echoing there is some javascript that will make the refresh/redirect for you. Try something like this:

    PHP Code:
    echo '<script type="text/javascript">';
        echo 
    'var agree = confirm("Validity: ' $val '\nSupplier: ' $sup '\nType nr: ' $type '\n\nAre you sure the information is correct and that you want to insert it into the database?");';

        echo 
    'if(agree == true) {';
                echo 
    'setTimeout("window.location.href=\'somepage.php\'",3000);';
                echo 
    'document.write("<p align=\"center\" style=\"margin-top: 50px;\">The component is succesfully inserted, you will be redirected in 3 seconds, or click <a href=\"somepage.php\">here</a> to go now.</p>");';
        echo 
    '} else if(agree == false) {'// << I know, this was just for some debugging, an else is enough.
                
    echo 'document.write("<p align=\"center\" style=\"margin-top: 50px;\">Please click on the Previous button of your browser to change the entered data or click <a href=\"somepage.php\">here</a> to go back to the search screen</p>");
              }'
    ;
    echo 
    '</script>'
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    Pattatj (09-12-2011)

  • #3
    New Coder
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Rowsdower, you just rock, that's the statement I was looking for, thanks a lot!
    +ty!
    'x.


  •  

    Posting Permissions

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