Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
页面刷新后从PHP调用jQuery消息通知_Php_Jquery - Fatal编程技术网

页面刷新后从PHP调用jQuery消息通知

页面刷新后从PHP调用jQuery消息通知,php,jquery,Php,Jquery,我正在一个PHP项目中使用这个toast消息插件。 我需要的是从PHP验证/查询结果触发消息。比如: if ($data === FALSE) { // No product found with the called id. Return to the catalog page throwing error message // Trigger Message from here ... header('location: products.php'); } 如果我只是

我正在一个PHP项目中使用这个toast消息插件。

我需要的是从PHP验证/查询结果触发消息。比如:

if ($data === FALSE) { // No product found with the called id. Return to the catalog page throwing error message

    // Trigger Message from here ...

    header('location: products.php');
}
如果我只是回显脚本调用代码或包含在模板中,如下所示:

$message = "
    <script>
        $().toastmessage('showToast', {
        text     : 'No product found with the specified criteria',
        sticky   : 1,
        position : 'top-right',
        type     : 'Error',
        closeText: '',
        close    : function () {
            }
        });
    </script>";

echo $message;
$message=”
$().toastmessage('showtoos'{
文本:“未找到符合指定条件的产品”,
粘性:1,,
位置:'右上',
键入:“错误”,
closeText:“”,
关闭:函数(){
}
});
";
回声$信息;
它可以工作,但是当页面被刷新(如示例中所述)以确保表单没有重新提交时,问题就会出现,然后在刷新时回显会丢失,因为它在验证过程中用完,并且消息不会出现


有什么办法可以解决这个问题吗?

如果有人能提出更好的解决方案或改进方案,我会将我目前的想法()发布给其他人参考:

我制作了一个脚本init.php,每个页面都会加载该脚本,在该页面中我包括:

        // Alert message display
        if(!isset($_SESSION['msgdisp'])){
            $_SESSION['msgdisp'] = 0;
        }else if($_SESSION['msgdisp'] > 0){
            echo $_SESSION['msg'];
            --$_SESSION['msgdisp'];
        }

// Toast message handler
// -----------------------------------------------------------------------
function message($message='Message',$msg_type=3,$msg_trigger=1,$sticky=0)
{
    $msg_type_array = array('Error','Success','Warning','Message');
    $msg_type = $msg_type_array[$msg_type];
    $_SESSION['msgdisp'] = $msg_trigger;
    $_SESSION['msg'] = "
        <script>
            $().toastmessage('showToast', {
            text     : '".$message."',
            sticky   : ".$sticky.",
            position : 'top-right',
            type     : '".strtolower($msg_type)."',
            closeText: '',
            close    : function () {
                }
            });
        </script>";
}
我没有考虑到CBroe在评论中强调的另一点,即用户可能已经打开了多个窗口,原因是页面刷新是即时的,当页面刷新并显示消息时,用户几乎无法对其他窗口执行任何操作


它现在运行良好(非常感谢CBroe),如果有人对此有任何想法,请分享。

然后在重定向到的URL中传递一个参数,以便下次运行PHP脚本时,您可以确定是否输出JS代码。我认为不可能通过$\u GET转发所有这些标记和符号,它将在两者之间中断。我并没有说你应该把JS代码本身作为参数传递,而只是一个让你在下一页决定是否输出JS代码的值
products.php?showMessage=1
或类似的内容。您可以看到,有多个参数,如消息类型(有4个)、粘性与否、定位以及整个消息(如果我想经常使用该方法,有些消息非常长,即使使用html,例如完整的验证错误报告在无序列表中包含14条错误消息等。这确实不是我想要显示消息的方式。你认为让url中的参数都可见是好的吗?你什么都没有r原始问题说明了有关参数或消息可变部分的任何内容–因此下次请立即提及相关事实//您只需将所有这些参数放入会话,然后在下一页显示带有这些值的对话框,然后将其从会话中删除即可。(用户可能会打开多个选项卡,共享同一会话–因此,如果这是一个问题,那么您可以在会话中为特定消息提供一个id,然后通过URL传递该id。)
if ($data === FALSE) { // No product found with the called id. Return to the catalog page throwing error message

    // Trigger Message from here ...
    message('No product found with the specified criteria',0,2,1); // 2 is the display trigger, set 1 for no page refresh
    header('location: products.php');
}