首頁  >  文章  >  後端開發  >  PHP 如何實現提示後跳轉?

PHP 如何實現提示後跳轉?

Guanhui
Guanhui原創
2020-06-22 11:12:243309瀏覽

PHP 如何實現提示後跳轉?

PHP 如何實現提示後跳轉?

先建立一個PHP文件,並開啟編輯;

然後在PHP程式碼中用函數「echo()」輸出JS程式碼;

接著在JS程式碼中使用函數「alert()」提示訊息;

最後將JS中「location.href」指向跳轉連結即可。

程式碼範例

function show_msg($msg, $url)
{
    echo "<script>alert(&#39;{$msg}&#39;);window.location.href=&#39;{$url}&#39;;</script>";
    exit();
}

show_msg(&#39;this is Message!&#39;, &#39;/user/show&#39;);

進階版

function message($msgTitle,$message,$jumpUrl){ 
    $str = &#39;<!DOCTYPE HTML>&#39;; 
    $str .= &#39;<html>&#39;; 
    $str .= &#39;<head>&#39;; 
    $str .= &#39;<meta charset="utf-8">&#39;; 
    $str .= &#39;<title>页面提示</title>&#39;; 
    $str .= &#39;<style type="text/css">&#39;; 
    $str .= &#39;*{margin:0; padding:0}a{color:#369; text-decoration:none;}a:hover{text-decoration:underline}body{height:100%; font:12px/18px Tahoma, Arial,  sans-serif; color:#424242; background:#fff}.message{width:450px; height:120px; margin:16% auto; border:1px solid #99b1c4; background:#ecf7fb}.message h3{height:28px; line-height:28px; background:#2c91c6; text-align:center; color:#fff; font-size:14px}.msg_txt{padding:10px; margin-top:8px}.msg_txt h4{line-height:26px; font-size:14px}.msg_txt h4.red{color:#f30}.msg_txt p{line-height:22px}&#39;; 
    $str .= &#39;</style>&#39;; 
    $str .= &#39;</head>&#39;; 
    $str .= &#39;<body>&#39;; 
    $str .= &#39;<div class="message">&#39;; 
    $str .= &#39;<h3>&#39;.$msgTitle.&#39;</h3>&#39;; 
    $str .= &#39;<div class="msg_txt">&#39;; 
    $str .= &#39;<h4 class="red">&#39;.$message.&#39;</h4>&#39;; 
    $str .= &#39;<p>系统将在 <span style="color:blue;font-weight:bold">3</span> 秒后自动跳转,如果不想等待,直接点击 <a href="{$jumpUrl}">这里</a> 跳转</p>&#39;; 
    $str .= "<script>setTimeout(&#39;location.replace(&#39;".$jumpUrl."&#39;)&#39;,2000)</script>"; 
    $str .= &#39;</div>&#39;; 
    $str .= &#39;</div>&#39;; 
    $str .= &#39;</body>&#39;; 
    $str .= &#39;</html>&#39;; 
    echo $str; 
}

推薦教學:《PHP

以上是PHP 如何實現提示後跳轉?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn