简体   繁体   English

无法使用PHP显示警报消息

[英]Can not display alert message using PHP

I have one issue while trying to display alert message using PHP.I am explaining my code below. 尝试使用PHP显示警报消息时出现一个问题。我在下面解释我的代码。

<?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. 这是我的问题是警报消息框根本无法工作。我需要在if / else条件下警报消息应该起作用。请帮助我。

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>');

You can not get alert as like that because you are using header(). 您无法像这样收到警报,因为您正在使用header()。 You can resolve this as: 您可以通过以下方式解决此问题:

Your PHP Code (pass success status): 您的PHP代码(通过成功状态):

<?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: 您需要在index.php文件中添加以下代码以获取警报:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM