0

I have one issue while trying to display alert message using PHP.I am explaining my code below.

<?php
if($id){
      $message = "Added successfully.";
      echo "<script type='text/javascript'>alert('$message');</script>";
      header("location:http://localhost/crm_complain/index.php");
  }else{
      $message = "Unable to add.\\nTry again.";
     echo "<script type='text/javascript'>alert('$message');</script>";
      header("location:http://localhost/crm_complain/index.php");
  }
?>

Here my problem is the alert message box is not working at all.I need inside the if/else condition the alert message should work.Please help me.

5
  • you need to call a function Commented Jan 20, 2016 at 10:51
  • how come you echo before sending out a header? Enable Error reporting... Commented Jan 20, 2016 at 10:52
  • You cannot print something and redirect using header. If PHP can send the header, the user won't see the printed message. So either redirection OR printing message. Commented Jan 20, 2016 at 10:55
  • if you want to alert, than y r u using header()?? Commented Jan 20, 2016 at 10:57
  • If i am not redirecting to header the post data is submitting again again at each page reload.My aim is i have to display the alert message and then i will redirect to orginal page. Commented Jan 20, 2016 at 11:02

2 Answers 2

1

The alert message works fine, but the header does not. If you want to redirect after the alert message, try this:

die('<script>location.href = "'. $url .'"</script>');
Sign up to request clarification or add additional context in comments.

2 Comments

what this $url contains.
any url. you can of course also hard code it like this: die('<script>location.href = "page.html"</script>');
0

You can not get alert as like that because you are using header(). You can resolve this as:

Your PHP Code (pass success status):

<?php
if($id)
{
    //$message = "Added successfully.";
    header("location:http://localhost/crm_complain/index.php?success=1"); // success status
}
else
{
    //$message = "Unable to add.\\nTry again.";
    header("location:http://localhost/crm_complain/index.php?success=0"); // failure status
}
?>

You need to add following code in index.php file for getting alerts:

<script type="text/javascript"> 
    var phpVar = "<?php if(isset($_GET['success'])) {echo $_GET['success'];} else { echo ""; } ?>";
    if(phpVar == 1){
        alert('Added successfully.');
    }
    else if(phpVar == 0){
        alert('Unable to add.\\nTry again.');   
    }
    else{
        // nothing
    }
</script>

2 Comments

@ devpro : But as per you each time of reload the else part alert is coming.
No,same issue but it resolved by checking phpVar == 0 && phpVar!=' '.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.