重定向到标头而不在php中打印js警报

Redirect to header without printing js alert in php

本文关键字:php 打印 js 警报 重定向      更新时间:2023-09-26

一旦值提交到php中的数据库,我就有下面的代码重定向到同一页面。在重定向之前,它必须打印值已提交的js警报。下面的代码只是重定向而不是打印警报。请告诉我代码中的问题在哪里。

if ($runsql)
    {
        do_alert ("Profile for ".$student_pet_name." Saved Successfully");
        header('Location: student_detail_entry.php');   
    }
    else
    {
        echo "Error in Submitted Value ". mysql_error();
    }

我的do_alert函数是

function do_alert($msg) 
    {
        echo '<script type="text/javascript">alert("' . $msg . '"); </script>';
    }

谢谢。。。

如果使用header()函数重定向,浏览器将立即重定向,并且不会为浏览器输出其他数据。

您可以先alert()并使用javascript执行重定向,例如使用

window.location = "http://www.google.com/"

阅读有关header()函数的更多信息:PHP.net header(

您不能同时打印到屏幕和重定向。header()要求没有发送任何输出,并且由于您正在回复JavaScript警报,重定向&JavaScript执行不会像您期望的那样运行。

解决方案?也许可以传递某种消息作为GET参数,并在student_detail_entry.php上执行警报(在页面加载时)。

您可以使用,

location.href="student_detail_entry.php";

而不是

header('Location: student_detail_entry.php');