在 PHP 中提交表单后的警报消息

我正在使用带有POST方法的表单来应用会话,一旦表单提交,页面就会重新加载,并且屏幕上没有任何警报。


所以我使用了一些我在网上找到的技巧,但它有一个问题,在表单提交后弹出警报,但没有一个页面设计工作,这意味着我只有空白页和警报消息。


单击“确定”关闭警报消息后,页面将加载。


if(empty($_SESSION["shopping_cart"])) {

    $_SESSION["shopping_cart"] = $cartArray;

    $status = "Product is added to your cart!";

    header('location:product.php?status=success');

}else{

    $array_keys = array_keys($_SESSION["shopping_cart"]);

    if(in_array($code,$array_keys)) {

        $status = "Product is already added to your cart!";

        header('location:product.php?status=failed');

    } else {

        $_SESSION["shopping_cart"] = array_merge($_SESSION["shopping_cart"],$cartArray);

        $status = "Product is added to your cart!";

        header('location:product.php?status=success');

    }


}


// This right here responsible to alert the message according to the status.

if( $_GET['status'] == 'success') {

   echo '<script>  alert("welldone");   </script>';

}

else{

   echo '<script>  alert("no good");   </script>';

}

如何解决页面加载顺序,使页面首先加载,警报加载后?

http://img2.mukewang.com/63130f5a0001b5d719081073.jpg

收到一只叮咚
浏览 55回答 4
4回答

翻阅古今

经过长时间的研究,我找到了使用jQuery函数延迟警报消息的解决方案,延迟允许HTML页面加载然后执行警报。感谢所有帮助我取得这个结果的人。delay()&nbsp; <script>&nbsp; &nbsp; &nbsp; $(document).ready(function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout(function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( $_GET['status'] == 'success') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo 'alert("welldone");';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo 'alert("no good");';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, 500);&nbsp; &nbsp; &nbsp; });&nbsp; </script>

守着一只汪

请尝试此代码,在PHP中提交表单后提醒消息。<?if ($_POST)&nbsp; &nbsp; {&nbsp; &nbsp; if (empty($_POST['testabc'])) $msg='<script type="text/javascript">alert("BLANK");</script>';&nbsp; &nbsp; else $msg='<script type="text/javascript">alert("NOT BLANK");</script>';&nbsp; &nbsp; }?><html><head></head><body><table>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <form id="finfo" method=POST>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="testabc">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" value="info">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; </tr></table><script type="text/javascript">alert("auto");</script><? echo $msg; ?></body></html>

慕姐4208626

alert()停止脚本的执行和 HTML 页在调用该页的点的加载。应将此脚本移动到页面底部。

人到中年有点甜

您可以延迟脚本标记,直到呈现页面后才会加载。<script&nbsp;src="alert.js"&nbsp;defer></script>然后它将在页面呈现后加载,并且不会有随机延迟(如使用等待/jquery延迟)。
打开App,查看更多内容
随时随地看视频慕课网APP