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


How to display PHP message after jQuery loading?

我是PHP新手。我想建立一个房间预订的小项目,并通过使用AJAX来改进它。

http://img851.imageshack.us/img851/6172/88303815.png

每当有人达到两小时的房间时间限制时,我都会尝试显示一条警告信息。registrationdiv是所有PHP处理"Get-Room"操作的地方。我的问题是,除非我刷新页面,否则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');
          }

非常感谢,如果我犯了一些致命的错误,请原谅;-)

对不起,我在PHP部分复制了错误的代码(我试图缩短它,但忘记了PHP标记)

编辑2:我试着使用json编码,但我不知道为什么它对我不起作用…它被困在提交按钮禁用阶段。当我删除数据类型行时,它工作正常。。。帮助任何人?非常感谢你的回答!

    $("#formReg").ajaxForm({
        url: "room.php",
        dataType: 'json',
        type: "POST",
        beforeSubmit: function() {
            $('form').find('input[type="submit"]').attr('disabled', 'disabled');
        },
        success: function(data) {
            alert(data.status);
            $("#registration").load("room.php #registration", function() { 
                $( "input:submit, a, button", ".registration" ).button();
                $( "a", ".registration" ).click(function() { return false; });
            });

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

David的回答有点正确,但我想详细说明一下。

这里有一个javascript函数,它具有基于用户当前房间数量的可变输出。

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

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

代码块将不会输出到页面。这就是为什么您的用户在尝试添加其他房间时看不到警报消息的原因。输出到页面的代码不会随每个ajax请求重新加载,而是静态内容。因此,当用户注册超过2个房间时,JS代码如下:

$("#dialog-message").find('.error').show();

从未被输出或使用。

通常,当您进行这样的验证时,"正确"的方法是在服务器端进行验证。因此,当用户试图注册一个房间,但已经预订了其中的两个房间时,AJAX请求会触发到room.php,它会从服务器接收"数据"响应。我的建议是在你的ajaxForm参数中添加一个dataType,比如这个

   url: "room.php",
   type: "POST",
   beforeSubmit: disableButtons,
   dataType: 'json',
   success: function(data) {

这将告诉jQuery期望来自服务器的响应为JSON。然后,在您的script rooms.php中,您需要建立一个标准的数据格式,以便使用进行响应并返回

echo json_encode( array( 'status' => 'OK' ) ); //tell the JS code that the status was OK

或者在出现错误的情况下:echo json_encode(array('status'=>'ERROR','errtxt'=>'对不起,您已经预订了2个房间。')//告诉JS代码状态为OK

因此,我简化了您的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方面的其他建议,我想你会很满意的。

您似乎在将JavaScript与PHP:混合

if(rooms_per_user($_SESSION['user_id'])>=2)

rooms_per_user是PHP函数还是JavaScript函数?$_SESSION绝对是一个PHP数组。PHP中的任何内容对JaveScript都将毫无意义,反之亦然。这段代码很可能会导致浏览器JavaScript控制台出现错误。

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

考虑到您现在JavaScript代码中的结构,实现所需功能的最简单方法可能是向.ajax()添加另一个调用以调用服务器端资源,获取rooms_per_user的值,将其设置为JavaScript变量,然后使用它(全部在.ajax()调用的success中)。

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