Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.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/2/jquery/86.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
jQuery加载后如何显示PHP消息?_Php_Jquery_Ajax_Load - Fatal编程技术网

jQuery加载后如何显示PHP消息?

jQuery加载后如何显示PHP消息?,php,jquery,ajax,load,Php,Jquery,Ajax,Load,我是PHP的新手。我想构建一个房间预订的小项目,并使用AJAX对其进行改进 我试图在每次有人达到两小时房间时间限制时显示一条警报消息。registration div是所有PHP处理“getroom”操作的地方。我的问题是,除非刷新页面,否则room_per_user函数不会显示每个用户的正确房间数 如何更正代码以显示错误消息 $("#formReg").ajaxForm({ url: "room.php", type: "POST", be

我是PHP的新手。我想构建一个房间预订的小项目,并使用AJAX对其进行改进

我试图在每次有人达到两小时房间时间限制时显示一条警报消息。registration div是所有PHP处理“getroom”操作的地方。我的问题是,除非刷新页面,否则room_per_user函数不会显示每个用户的正确房间数

如何更正代码以显示错误消息

     $("#formReg").ajaxForm({
       url: "room.php",
       type: "POST",
       beforeSubmit: disableButtons,
       success: function(data) {

          $("#registration").load("room.php #registration", function() { 
                $(function() {

          <?php 
          if(rooms_per_user($_SESSION['user_id'])>=2)
          {
            $string = "\$(\"#dialog-message\").find('.error').show();";
            echo $string;
          } 
          ?>

                    $( "input:submit, a, button", ".registration" ).button();
                    $( "a", ".registration" ).click(function() { return false; });
                });                 
          });


         $("#cancellation").load("room.php #cancellation", function() {     
            $(function() {
                 $( "input:submit, a, button", ".cancellation" ).button();
                $( "a", ".cancellation" ).click(function() { return false; });
              });                     
          });

        }); 

          function disableButtons(){
           $('form').find('input[type="submit"]').attr('disabled', 'disabled');
          }

您似乎将JavaScript与PHP混合使用:

if(rooms_per_user($_SESSION['user_id'])>=2)
rooms\u per\u user
是PHP函数还是JavaScript函数
$\u SESSION
绝对是一个PHP数组。PHP中的任何内容对JaveScript来说都是毫无意义的,反之亦然。这段代码很可能导致浏览器JavaScript控制台出错

PHP在服务器端执行。执行完成后,页面将发送到浏览器。然后JavaScript在浏览器中执行。PHP代码和JavaScript代码存在于两个完全不同的上下文中,不能像这样混合

考虑到JavaScript代码目前的结构,实现所需功能的最简单方法可能是添加另一个调用
.ajax()
来调用服务器端资源,获取
rooms\u per\u user
的值,将其设置为JavaScript变量,然后使用它(所有这些都在
调用的
成功
.ajax()
过程中)


基本上,JavaScript代码不能直接与PHP代码对话。如果有一个值仅存在于服务器上,则需要调用该服务器以获取该值。

David的回答有点正确,但我想进一步说明

这里有一个javascript函数,该函数根据用户当前拥有的房间数具有可变输出

您在上面发布的脚本是在用户最初访问您的页面时加载的。这意味着当用户最初加载页面并且没有保留房间时

<?php 
if(rooms_per_user($_SESSION['user_id'])>=2)
{
$string = "\$(\"#dialog-message\").find('.error').show();";
echo $string;
} 
?>
从未被输出或使用

通常,当您这样做验证时,“正确”的方法是在服务器端进行验证。 因此,当用户试图注册一个房间,但已经预订了其中两个房间时,AJAX请求会触发room.php,它会从服务器接收“数据”响应

   url: "room.php",
   type: "POST",
   beforeSubmit: disableButtons,
   dataType: 'json',
   success: function(data) {
这将告诉jQuery期望从服务器得到JSON响应

echo json_encode( array( 'status' => 'OK' ) ); //tell the JS code that the status was OK
或在发生错误时: echo json_encode(数组('status'=>'ERROR','errtxt'=>'抱歉,您已经预订了2个房间);//告诉JS代码状态正常

因此,我简化了您的JS代码,并删除了许多您不需要的函数中的jQuery包装,最终结果如下:

$("#formReg").ajaxForm({
    url: "room.php",
    type: "POST",
    beforeSubmit: function() {
        $('form').find('input[type="submit"]').attr('disabled', 'disabled');
    },
    success: function(data) {

        if( data.status != 'OK' ) {
            $("#dialog-message").find('.error').html(data.errtxt).show();
            return;
        }

        $("#registration").load("room.php #registration", function() { 

            $( "input:submit, a, button", ".registration" ).button();
            $( "a", ".registration" ).click( function(e) {
                e.preventDefault();
            });

        });

        $("#cancellation").load("room.php #cancellation", function() {     
            $( "input:submit, a, button", ".cancellation" ).button();
            $( "a", ".cancellation" ).click(function() { return false; });
        }); 
    }
});

将此与PHP方面的其他建议结合起来,我认为您的处境会很好。

您不能以这种方式将PHP和JS混为一谈。
rooms\u per\u user
PHP函数?看起来像
$\u SESSION['user\u id']
被称为javascript。但这就是PHP使用的数组。room.PHP返回什么?room.PHP与此脚本的页面相同
echo json_encode( array( 'status' => 'OK' ) ); //tell the JS code that the status was OK
$("#formReg").ajaxForm({
    url: "room.php",
    type: "POST",
    beforeSubmit: function() {
        $('form').find('input[type="submit"]').attr('disabled', 'disabled');
    },
    success: function(data) {

        if( data.status != 'OK' ) {
            $("#dialog-message").find('.error').html(data.errtxt).show();
            return;
        }

        $("#registration").load("room.php #registration", function() { 

            $( "input:submit, a, button", ".registration" ).button();
            $( "a", ".registration" ).click( function(e) {
                e.preventDefault();
            });

        });

        $("#cancellation").load("room.php #cancellation", function() {     
            $( "input:submit, a, button", ".cancellation" ).button();
            $( "a", ".cancellation" ).click(function() { return false; });
        }); 
    }
});