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? Link to comment https://forums.phpfreaks.com/topic/202357-why-alert-not-execute-if-i-use-before-header/ 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. Link to comment https://forums.phpfreaks.com/topic/202357-why-alert-not-execute-if-i-use-before-header/#findComment-1061003 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? Link to comment https://forums.phpfreaks.com/topic/202357-why-alert-not-execute-if-i-use-before-header/#findComment-1061015 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? Link to comment https://forums.phpfreaks.com/topic/202357-why-alert-not-execute-if-i-use-before-header/#findComment-1061018 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. Link to comment https://forums.phpfreaks.com/topic/202357-why-alert-not-execute-if-i-use-before-header/#findComment-1061021 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. Link to comment https://forums.phpfreaks.com/topic/202357-why-alert-not-execute-if-i-use-before-header/#findComment-1061022 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'; Link to comment https://forums.phpfreaks.com/topic/202357-why-alert-not-execute-if-i-use-before-header/#findComment-1061024 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(); ?> Link to comment https://forums.phpfreaks.com/topic/202357-why-alert-not-execute-if-i-use-before-header/#findComment-1061027 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. Link to comment https://forums.phpfreaks.com/topic/202357-why-alert-not-execute-if-i-use-before-header/#findComment-1061034 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.... Link to comment https://forums.phpfreaks.com/topic/202357-why-alert-not-execute-if-i-use-before-header/#findComment-1061036 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. Link to comment https://forums.phpfreaks.com/topic/202357-why-alert-not-execute-if-i-use-before-header/#findComment-1061040 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(); ?> Link to comment https://forums.phpfreaks.com/topic/202357-why-alert-not-execute-if-i-use-before-header/#findComment-1061043 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. Link to comment https://forums.phpfreaks.com/topic/202357-why-alert-not-execute-if-i-use-before-header/#findComment-1061493 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. Link to comment https://forums.phpfreaks.com/topic/202357-why-alert-not-execute-if-i-use-before-header/#findComment-1061524 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.