简体   繁体   English

Java脚本警报在我的php文件中不起作用?

[英]Java script alert doesn't work in my php file?

$data = mysqli_query ($conn,$query)or     die(mysqli_error($conn));
if($data)
{

    echo '<script language="javascript">';
    echo 'alert("YOUR REGISTRATION IS COMPLETED...")';
    echo '</script>';


    header("Location:booking.php");
}

(This is reservation.php and same folder having booking.php) (这是reservation.php和具有booking.php的同一文件夹)

There are many ways to simplify this but if I were to keep it to how you want it then this should do 有很多方法可以简化此操作,但是如果我要按照您的要求进行操作,则应该这样做

$data = mysqli_query($conn,$query) or die(mysqli_error($conn));
if($data) {
      echo '<script type="text/javascript">';
      echo 'alert("YOUR REGISTRATION IS COMPLETED...")';
      echo '</script>';

      echo '<script type="text/javascript">';
      echo 'setTimeout(function() {';
      echo 'window.location.href = "http://stackoverflow.com"';
      echo '}, 5000); // <-- redirect after 5 seconds';
      echo '</script>';
}

Basically -- you can't do that. 基本上-您不能那样做。

Browsers don't execute JS on pages that include location header redirects. 浏览器不会在包含位置标头重定向的页面上执行JS。 (At least the ones I've tested.) It wouldn't make much sense for a browser to render the complete page, execute all the JS, and then throw it away and load the requested redirect. (至少我已经测试过了。)对于浏览器而言,呈现整个页面,执行所有JS,然后丢弃并加载请求的重定向并没有多大意义。 If your desire is to have a message display and then redirect the user after they've clicked to acknowledge it, you'll have make a form or attach a bit of custom JS to a button. 如果您希望显示消息,然后在用户单击确认后重定向用户,则需要制作一个表单或在按钮上附加一些自定义JS。

Alternatively, you might use what's commonly called a flash message. 或者,您可以使用通常所说的即插即用消息。 Upon successful operation, you put the message text into the user's session and then immediately redirect to the new page. 成功操作后,将消息文本放入用户的会话中,然后立即重定向到新页面。 The new page looks in the session, and if it contains any message, displays it. 新页面将在会话中查找,如果包含任何消息,则显示该页面。

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

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