473,721 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

php javascript alert redirect

41 New Member
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 39831
unauthorized
81 New Member
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 New Member
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 Recognized Expert Moderator Expert
@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 New Member
@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
unauthorized
81 New Member
@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 Recognized Expert Moderator Expert
@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 Recognized Expert Moderator Expert
@unauthorized
RFC 2616 that is.

......
Jul 8 '09 #8
Atli
5,058 Recognized Expert Expert
@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
roseblue
1 New Member
this code seems to be not working
Jul 20 '13 #10

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

Similar topics

7
11143
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 some changes to settings. 3) Press a "reset to defaults" button that uses a confirmation box to let the user know what is about to be reset (lists items). 4) Walk away while the confirm box is displayed and come back after the page timeout. 5) My screen now has the page, a confirm popup on top...
6
3138
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 so far). I've been trying, with limited success, to produce a javascript alert box which shows the user a validation error message. The error message itself is dynamic, i.e., it's contained within a php variable. I seem to be running into two issues:
4
29917
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 the page has been posted back? I am pressing a button, validating data and then wanting to simply pop-up an alert if say the login is empty. When I click the button though the page is blank but the pop-up is there.
2
2881
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 Response.Redirect("myPage.aspx") End If What I really would like to have is a notification for the
8
1944
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 function does is insert values from a data grid into the database. after that is done i want to send the user to PageC, but before i do that i check a variable value. if that value is "0" then i want to raise a javascript alert telling the user they need to set some info in PageA and then...
0
1136
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 entered either person or company. If not then it shows this javascript alert box. Now my problem is when this javascript alert box pops up then after clicking on "OK" , the font of page changes. It becomes bold and also the size increases. In short my aspx page layout is no more same as before. ...
2
1973
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 ....... strScript &= alert('This is an alert') strscript &="</script> ' this is all an abbreviated script - - but you get the idea response.write(strscript) end if response.redirect("another page.aspx")
2
2410
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 record has deleted when a DataGridCommandEventArgs event occured. protected void SetDeleteButton(Object sender, DataGridCommandEventArgs e) { try { if (e.CommandName == "Delete") {
0
2460
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 bellow code in our application ~/Profile/MyProfile.aspx.cs file string redirectTo = "~/Logins/Login.aspx"; //The page I wish to redirect to. string script = "<script>"; //Begin Script tag script += "alert('" + ui_lblError.Text + "');"; //Javascript Alert...
0
8844
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9370
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9218
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9067
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8009
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6672
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5986
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4487
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.