猿问

多个标头/

我使用标题在提交后显示成功消息。我还想在5秒后将用户重定向到索引.php页面,但不知何故它不起作用。


if(mysqli_query($link, $sql)){

    header("Location: addbusiness.php?message=<div class='alert alert-success' role='alert' style='text-align: center; margin-bottom: 50px;'>Succes.</div>");

    header( "refresh:5;url=index.php" );

} else{

    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);

}


慕哥9229398
浏览 112回答 1
1回答

慕斯王

您根本无法同时执行,因此您必须将两个标题分开,即应该是显示消息的页面的一部分。此外,没有理由(实际上是一种不好的做法)将整个HTML传递到URL中,最好使用一些ID,例如:locationrefreshrefresh数据库.php<?php&nbsp; &nbsp; if(mysqli_query($link, $sql)){&nbsp; &nbsp; &nbsp; &nbsp; header("Location: addbusiness.php?message=1");&nbsp; &nbsp; } else{&nbsp; &nbsp; &nbsp; &nbsp; echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);&nbsp; &nbsp; }?>附加业务.php<?php&nbsp; &nbsp; if(!empty($_GET['message'])){&nbsp; &nbsp; &nbsp; &nbsp; if($_GET['message'] == 1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header( "refresh:5;url=index.php" );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<div class='alert alert-success' role='alert' style='text-align: center; margin-bottom: 50px;'>Success.</div>";&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //do something else&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }?>基于这样的条件,您还将避免每次访问时刷新addbusiness.php当然,在任何情况下,您都必须确保在调用之前没有任何HTML输出到浏览器。在两个页面中。header()
随时随地看视频慕课网APP
我要回答