473,394 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

why JS's alert() not execute before header()?

20
if a JS's alert() use before header() use in a function like this:
e.g.
Expand|Select|Wrap|Line Numbers
  1. function goto_page()
  2. {
  3.         if(.............)
  4.           {
  5.            <? alert("Error:............"); <?php
  6.            header("abc.php");
  7.            }
  8. }
alert not execute. why?
May 20 '10 #1
4 4823
Dormilich
8,658 Expert Mod 8TB
a) remove the <?
b) PHP is always executed before JS.
c) close the <?php tag
May 20 '10 #2
iskhan
20
I need first alert message display when I click OK then header() execute and program transfer to new page.
If PHP execute 1st then what is the alternative?
May 20 '10 #3
Dormilich
8,658 Expert Mod 8TB
you cannot mix JS and PHP.

to forward to another page from JS, use
Expand|Select|Wrap|Line Numbers
  1. window.location.href = "new URI";
May 20 '10 #4
Atli
5,058 Expert 4TB
you cannot mix JS and PHP.
To explain this in a bit more detail, this is how a PHP page works:
  1. A browser sends a request to a server, to receive a PHP page.
  2. The server executes the PHP, creating a webpage, usually made up of HTML, CSS and Javascript content.
  3. The server sends the content created by PHP to the browser.
  4. The browser displays the HTML and CSS, and executes the JavaScript code.

So, you see, a header call (which is a PHP function), is always executed before any JavaScript code, like the alert function. If you need the client-side code (JavaScript) to redirect the page, use the method Dormilich described in hist last post. If you need PHP to redirect, use the header function. You can, however, not mix the two methods, because they aren't even being executed by the same computer.
May 21 '10 #5

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

Similar topics

6
by: iceking | last post by:
Hi, I have a php script that checks whether the user is allowed to perform an action. If he is not allowed; I display a warning; using the alert function. After clicking away this function, I want...
6
by: Jorntk | last post by:
<script language="JavaScript"> !-- function onLoad() { if( self.innerWidth < 1024 ) { self.moveTo( 0, 0 ); self.resizeTo( screen.availWidth, screen.availHeight ); }
4
by: hugo2 | last post by:
Most of these characters are not on the standard keyboard. Here's a successful contrivance, with comments & cautions, a little page that works in IE6. <HTML><HEAD><SCRIPT TYPE="text/javascript">...
3
by: Christophe | last post by:
Hello, I use php mail() function for a feedback form with the following header: Content-type: text/plain; charset=iso-8859-1 Inside the body part of my message, I convert characters with...
0
by: Stephanie | last post by:
I have a problem with an Alert that is supposed to invoke a Job. Here are some details: Windows Server 2000 SQL Server Standard Ed. ver. 8.00.818 (Sp3) Alert: SQL Server performance condition...
2
by: Ben | last post by:
Hi, When a user wants to start the application, aspnet checks a boolean value in a field of the database. If that value is 'false', then i want to execute a ALERT in javascript telling the...
8
by: M.L. | last post by:
Hello. I created a form using JS validation with the form tag as follows: <form name="form1" action="dynaform.php" method="post" onsubmit="return pvg_sub();"> The js validation script sends...
10
by: vinpkl | last post by:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.