天天看點

PHP實作彈出提示框并跳轉到新頁面PHP實作彈出提示框後傳回上一個頁面

PHP實作彈出提示框後傳回上一個頁面

<?php
echo "<script>alert('退出成功!');location.href='".$_SERVER["HTTP_REFERER"]."';</script>"; 
?>           

複制

alert裡面是提示的消息,href是提示後跳轉的頁面。

如果alert中文亂碼,加入下面代碼

echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';           

複制

每次都複制一大段代碼還是比較麻煩的,我們可以考慮封裝成函數來調用,下面是我自己封裝的頁面跳轉函數

/**
* 頁面跳轉方法
* @param $msg 提示說明
* @param null $path 跳轉路徑
* @param null $parent 為ture則傳回父視窗
*/
function messageInfo($msg,$path=NULL,$parent=NULL){
  if($parent === true){
      $str="<script>alert('".$msg."');parent.location.href='".$path."‘</script>";
    }else if(empty($path)){
      $str="<script>alert('".$msg."');history.back()</script>";
    }else{
      $str="<script>alert('".$msg."');location.href='".$path."'</script>";
  }

  echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';//支援中文
  echo $str;
}
           

複制

使用方法:messageInfo(‘操作成功!’,’http://www.demourl.com/product_list.php’);

其他跳轉方法

代碼如下:

echo "<script> alert('no loginid'); </script>"; 
echo "<meta http-equiv='Refresh' content=0; URL=$url>";            

複制

$url就是要跳轉的頁面,同時,這個還能控制跳轉時間,content後面的0就是表示0秒後跳轉。

兩個直接跳轉的方式:

代碼如下:

header("Location:".PSYS_BASE_URL."user/index");

代碼如下:

// echo "<script> alert('建立tag成功!'); </script>";
   // header("refresh:3;url='createTag' ");            

複制