Connecting Tech Pros Worldwide Help | Site Map

php javascript alert redirect

  #1  
Old July 8th, 2009, 03:48 PM
Newbie
 
Join Date: Oct 2008
Posts: 26
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.

Expand|Select|Wrap|Line Numbers
  1. if(mail($to,$subject,$body,$headers))
  2. {
  3. echo "<script language='javascript'>";
  4. echo "alert('order email sent')";
  5. echo "window.location='manage_orders.php?choice=PENDING'";
  6. echo "</script>";
  7. }
  8.  
But If i use any one of them then they BOTH work fine like
Expand|Select|Wrap|Line Numbers
  1. if(mail($to,$subject,$body,$headers))
  2. {
  3. echo "<script language='javascript'>";
  4. echo "alert('order email sent')";
  5. echo "</script>";
  6. }
  7.  
or

Expand|Select|Wrap|Line Numbers
  1. if(mail($to,$subject,$body,$headers))
  2. {
  3. echo "<script language='javascript'>";
  4. echo "window.location='manage_orders.php?choice=PENDING'";
  5. echo "</script>";
  6. }
  7.  
but how can i get them both to work together through php.

vineet

Last edited by vinpkl; July 8th, 2009 at 03:49 PM. Reason: code
  #2  
Old July 8th, 2009, 04:03 PM
Member
 
Join Date: May 2009
Posts: 81

re: php javascript alert redirect


You can put header() anywhere in the source file, as long as you enable output buffering using the ob_start() function. For example:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. ob_start();
  3. echo "hello world";
  4. header("Location: http://www.example.com");
  5. ?>
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:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. alert("this is an alert!");
  3. document.location = "http://www.example.com";
  4. </script>
Oh, and one more thing. <script> has no "language" property. Use <script type="text/javascript"> instead.

Last edited by Atli; July 8th, 2009 at 07:16 PM. Reason: Added the second [code] tags.
  #3  
Old July 8th, 2009, 04:18 PM
Newbie
 
Join Date: Oct 2008
Posts: 26

re: php javascript alert redirect


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.

Expand|Select|Wrap|Line Numbers
  1. if(mail($to,$subject,$body,$headers))
  2. {
  3. echo "<script type='text/javascript'>";
  4. echo "alert('order email sent')";
  5. echo "</script>";
  6. header("Location:manage_orders.php?choice=PENDING");
  7. }
  8.  
vineet
  #4  
Old July 8th, 2009, 04:21 PM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

re: php javascript alert redirect


Quote:
Originally Posted by vinpkl View Post
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)
  #5  
Old July 8th, 2009, 04:28 PM
Newbie
 
Join Date: Oct 2008
Posts: 26

re: php javascript alert redirect


Quote:
Originally Posted by Dormilich View Post
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
  #6  
Old July 8th, 2009, 04:37 PM
Member
 
Join Date: May 2009
Posts: 81

re: php javascript alert redirect


Quote:
Originally Posted by vinpkl View Post
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.
  #7  
Old July 8th, 2009, 04:40 PM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

re: php javascript alert redirect


Quote:
Originally Posted by vinpkl View Post
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...)

Quote:
Originally Posted by vinpkl View Post
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()).

Quote:
Originally Posted by vinpkl View Post
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.
  #8  
Old July 8th, 2009, 04:42 PM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

re: php javascript alert redirect


Quote:
Originally Posted by unauthorized View Post
Look up the right RFC if you want more info.
RFC 2616 that is.

......
  #9  
Old July 8th, 2009, 07:26 PM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,701
Provided Answers: 4

re: php javascript alert redirect


Quote:
Originally Posted by vinpkl View Post
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.
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP redirect after posting gnawz answers 1 February 25th, 2009 01:27 PM
Form validation with PHP/Javascript Chris answers 27 September 1st, 2006 02:25 AM
Javascript alert question bonehead answers 6 October 18th, 2005 12:45 PM
redirect to URL passed as variable in php Donna Hawkins answers 7 July 20th, 2005 02:03 PM