Jump to content

why alert() not execute if I use before header()??


iskhan

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.