iskhan Posted May 20, 2010 Share Posted May 20, 2010 if a JS's alert() use before header() use in a function like this: e.g. function goto_page() { if(.............) { <? alert("Error:............"); <?php header("abc.php"); } } alert not execute. why? Quote Link to comment Share on other sites More sharing options...
trq Posted May 20, 2010 Share Posted May 20, 2010 If your example is anything to go by, its likely because its not within a js script block. Quote Link to comment Share on other sites More sharing options...
iskhan Posted May 20, 2010 Author Share Posted May 20, 2010 It is obvious that every JS code is in the <script> </script> tags. I give you a sample code If I forgot to mention JS tag it mean tags are there. so now what is the solution? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted May 20, 2010 Share Posted May 20, 2010 The solution to what? What are you looking to accomplish? Quote Link to comment Share on other sites More sharing options...
Mchl Posted May 20, 2010 Share Posted May 20, 2010 Why is there <? before alert()? It's breaking JS probably. Quote Link to comment Share on other sites More sharing options...
iskhan Posted May 20, 2010 Author Share Posted May 20, 2010 when I use the these two function in my function the alert dialog box not display and header() execute and program transfer to new page. but I need first alert message display when I click OK then header() execute and program transfer to new page. Quote Link to comment Share on other sites More sharing options...
anups Posted May 20, 2010 Share Posted May 20, 2010 function goto_page() { if(.............) { <? alert("Error:............"); <?php header("abc.php"); } } Javascript is executed at client side i.e in browser. But in above case you are redirecting the page to another location through PHP before sending o/p to the browser, If browser is not getting any data it won't interpret it. so u can use javascript to redirect the page alert('Error:---') like location.href = 'abc.php'; Quote Link to comment Share on other sites More sharing options...
phpchamps Posted May 20, 2010 Share Posted May 20, 2010 Try this... <?php function goto_page() { if(true){ ?> <script language="JavaScript1.2" type="text/javascript"> alert("Error"); </script> <?php header("http://www.google.com"); } } goto_page(); ?> Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted May 20, 2010 Share Posted May 20, 2010 1) "If true"... seriously? True is always true, by definition. 2) You cannot send a header after you've started sending the response body. Quote Link to comment Share on other sites More sharing options...
phpchamps Posted May 20, 2010 Share Posted May 20, 2010 yaa if true.. because i dont know which condition he is using.. i always want my condition to be true.. its just an example.... he can replace it by his own condition..... The only problem he is facing that he want to display an alert box.......and then he want user to be redirected to specific page.... So, my code solves the prob...4 him.... Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted May 20, 2010 Share Posted May 20, 2010 So, my code solves the prob...4 him.... No it doesn't. You cannot send a header after you start sending the body like you do in your script. What he is trying to do is not possible. Quote Link to comment Share on other sites More sharing options...
phpchamps Posted May 20, 2010 Share Posted May 20, 2010 as your requirement is only to redirect user. use javascript redirection.. use below code . <?php function goto_page() { if(true){ ?> <script language="JavaScript1.2" type="text/javascript"> alert("Error"); window.location = "http://www.google.com"; </script> <?php } } goto_page(); ?> Quote Link to comment Share on other sites More sharing options...
iskhan Posted May 21, 2010 Author Share Posted May 21, 2010 Is there any equivalent of JS's alert() function in PHP?If not it means that window.location = "http://.....";is the only solution. Quote Link to comment Share on other sites More sharing options...
Mchl Posted May 21, 2010 Share Posted May 21, 2010 No there is not. PHP does not run in browser. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.