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

时间:2012-03-26 19:33:53

标签: php jquery ajax load

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

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

每当有人达到两小时的时间限制时,我就会尝试显示提醒信息。注册div是所有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; });
            }); 

           }

      });

2 个答案:

答案 0 :(得分:1)

大卫在他的回答中是正确的,但我想再详细说明一下。

这里有一个基于当前用户房间数量的可变输出的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。在您的脚本rooms.php中,您需要建立一个标准数据格式来响应并使用

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

或出现错误时:     echo json_encode(array('status'=&gt;'ERROR','errtxt'=&gt;'对不起,但你已预订了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方面的其他建议结合起来,我认为你会非常友好。

答案 1 :(得分:0)

您似乎将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()来调用服务器端资源,获取值{{ 1}},将其设置为JavaScript变量,然后使用它(全部在rooms_per_user调用的success中。)

基本上,您的JavaScript代码无法直接与您的PHP代码对话。如果存在仅存在于服务器上的值,则需要调用该服务器以获取该值。