php javascript alert redirect

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vinpkl
    New Member
    • Oct 2008
    • 41

    php javascript alert redirect

    hi all

    i want to show an alert box and then redirect page to another location through php code but its not working as needed.
    As i cannot use PHP HEADER LOCATION because i m using echo before header. so i have to use javascript but through

    PHP.

    Code:
    if(mail($to,$subject,$body,$headers))
    {
    echo "<script language='javascript'>";
    echo "alert('order email sent')";
    echo "window.location='manage_orders.php?choice=PENDING'";
    echo "</script>";
    }
    But If i use any one of them then they BOTH work fine like
    Code:
    if(mail($to,$subject,$body,$headers))
    {
    echo "<script language='javascript'>";
    echo "alert('order email sent')";
    echo "</script>";
    }
    or

    Code:
    if(mail($to,$subject,$body,$headers))
    {
    echo "<script language='javascript'>";
    echo "window.location='manage_orders.php?choice=PENDING'";
    echo "</script>";
    }
    but how can i get them both to work together through php.

    vineet
    Last edited by vinpkl; Jul 8 '09, 02:49 PM. Reason: code
  • unauthorized
    New Member
    • May 2009
    • 81

    #2
    You can put header() anywhere in the source file, as long as you enable output buffering using the ob_start() function. For example:
    Code:
    <?php
    ob_start();
    echo "hello world";
    header("Location: http://www.example.com");
    ?>
    The above code is completely valid and will run properly, although it will result at very least in increased memory overhead.

    Now, your JS code appears to be little poor on the syntax. At very least try to terminate each line with ";" as per language rules. The following code will work in all browsers and can be used with HTML 4.01 strict documents:
    [code=html]
    <script type="text/javascript">
    alert("this is an alert!");
    document.locati on = "http://www.example.com ";
    </script>[/code]

    Oh, and one more thing. <script> has no "language" property. Use <script type="text/javascript"> instead.
    Last edited by Atli; Jul 8 '09, 06:16 PM. Reason: Added the second [code] tags.

    Comment

    • vinpkl
      New Member
      • Oct 2008
      • 41

      #3
      php javascript

      hi unauthorised

      i have amended code acording to you and the PHP Header redirect is working fine with ob_start() but the alert is not working.

      Code:
      if(mail($to,$subject,$body,$headers))
      {
      echo "<script type='text/javascript'>";
      echo "alert('order email sent')";
      echo "</script>";
      header("Location:manage_orders.php?choice=PENDING");
      }
      vineet

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Originally posted by vinpkl
        i have amended code acording to you and the PHP Header redirect is working fine with ob_start() but the alert is not working.
        obviously, the browser is redirected to the new page before any javascript is executed (because PHP is done on the server and Javascript on the Browser)

        Comment

        • vinpkl
          New Member
          • Oct 2008
          • 41

          #5
          php javascript

          Originally posted by Dormilich
          obviously, the browser is redirected to the new page before any javascript is executed (because PHP is done on the server and Javascript on the Browser)
          hi Dormilich

          i have heard that javascript works on client side and works faster than server side scripts.
          so if this is true then alert should popup first before the PHP Header redirect.

          Or the popup is not working because we are generating alert popup through PHP.

          vineet

          Comment

          • unauthorized
            New Member
            • May 2009
            • 81

            #6
            Originally posted by vinpkl
            hi Dormilich

            i have heard that javascript works on client side and works faster than server side scripts.
            so if this is true then alert should popup first before the PHP Header redirect.

            Or the popup is not working because we are generating alert popup through PHP.

            vineet
            You should never rely on JS to be enabled client side. There is no reason for it to be faster, but it often misbehaves client side (broken browsers or it's disabled) which isn't worth the few CPU cycles you save on the server.

            Also, go read about ob_start() in the PHP documentation. Make sure you know what it does before copy-pasting it in your code.

            If you want to execute JS code, you can't use header redirects. Use a JS redirect or don't use alerts at all. Just like Dormilich said, browsers will not render the page if they receive a redirect header. Look up the right RFC if you want more info.

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              Originally posted by vinpkl
              i have heard that javascript works on client side and works faster than server side scripts.
              I doubt that in general (there may be cases...)

              Originally posted by vinpkl
              so if this is true then alert should popup first before the PHP Header redirect.
              you're mixing up server code and browser code. first, all of the server code is executed (i.e the redirect via header()), afterwards, all browser code is executed (i.e. show the popup via alert()).

              Originally posted by vinpkl
              Or the popup is not working because we are generating alert popup through PHP.
              nope, how you create the popup doesn't matter here. you could use Perl, Java or ASP and get the same result.

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                Originally posted by unauthorized
                Look up the right RFC if you want more info.
                RFC 2616 that is.

                ......

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  Originally posted by vinpkl
                  i have heard that javascript works on client side and works faster than server side scripts.
                  so if this is true then alert should popup first before the PHP Header redirect
                  Never believe anything you've heard, unless you can verify it yourself :-)

                  It's important to realize the difference between server-side and client-side code if you intend to work with server-side code at all.

                  Comment

                  • roseblue
                    New Member
                    • Jul 2013
                    • 1

                    #10
                    this code seems to be not working

                    Comment

                    • Atli
                      Recognized Expert Expert
                      • Nov 2006
                      • 5062

                      #11
                      Which is why this thread was created in the first place. (Four years ago!)

                      If you read what has been posted - rather than just copying the broken code the OP was asking for help fixing - then you'll find out why it's not working and how to fix it.

                      Comment

                      Working...