472,348 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,348 software developers and data experts.

php javascript alert redirect

41
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
Jul 8 '09 #1
10 39397
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.
Jul 8 '09 #2
vinpkl
41
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
Jul 8 '09 #3
Dormilich
8,658 Expert Mod 8TB
@vinpkl
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)
Jul 8 '09 #4
vinpkl
41
@Dormilich
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
Jul 8 '09 #5
@vinpkl
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.
Jul 8 '09 #6
Dormilich
8,658 Expert Mod 8TB
@vinpkl
I doubt that in general (there may be cases...)

@vinpkl
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()).

@vinpkl
nope, how you create the popup doesn't matter here. you could use Perl, Java or ASP and get the same result.
Jul 8 '09 #7
Dormilich
8,658 Expert Mod 8TB
@unauthorized
RFC 2616 that is.

......
Jul 8 '09 #8
Atli
5,058 Expert 4TB
@vinpkl
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.
Jul 8 '09 #9
this code seems to be not working
Jul 20 '13 #10
Atli
5,058 Expert 4TB
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.
Jul 20 '13 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Tasha's Dad | last post by:
A description of the problem: 1) Go to a page with various settings and a timeout (forces re-login if over 10 minutes) 2) Before the timeout, make...
6
by: bonehead | last post by:
Greetings, I'm working on an e-mail form (btw many thanks to Philip Ronan for the very cool email address format tester function, best I've seen...
4
by: PH | last post by:
Does anyone know how to have the pop-up javascript alert, Response.Write("<script language=javascript>alert('HEY!');</script>); , pop-up after...
2
by: Kenneth | last post by:
Hi, In a document I have three lines that detects and redirects to another page is a session variable is 0 If Session("Customer_ID")=0 Then...
8
by: simon | last post by:
hello. hopefully another quick newbie question for you.... I have a codebehind function coded to handle the click event of a button. what this...
0
by: puja | last post by:
hi all, I have this aspx page where user has to enter person or company information. I have proceed button which if clicked checks if user has...
2
by: Seth Williams | last post by:
In the middle of my event handler (for a button), I have an if/then statement (using vb.net): if (whatever) then strScript="<script ..........
2
by: PrasathPalani | last post by:
Dear Experts, I have a Datagrid with delete options. I use to confirm from users and delete record from data grid. I give confrimation after each...
0
by: Anupam Jana | last post by:
Hello All, I want to redirect to some other directory page after displaying JavaScript alert message in ASP.net in a page. For that I tried...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.