【问题标题】:issue regarding PHP, Jquery and Ajax.关于 PHP、Jquery 和 Ajax 的问题。
【发布时间】:2012-10-18 19:57:30
【问题描述】:

我目前有一个关于 PHP、Jquery 和 Ajax 的问题。

我的页面底部有一个 div,其中包含来自数据库的数据,现在对于数据的每次迭代都会形成一个新的 div,该 div 的 id 为“pagestatus”,并且下一个具有自动增量到它,所以每个 div 的 id 都会改变,如下所示:'pagestatus0','pagestatus1'等。

现在我对 Jquery 并不完全陌生,因为我使用它来使页面更具交互性,并且我使用 Ajax 来更新 MySQL 数据库。

我的代码有问题,我希望它是这样的:

  • 在 div 中单击按钮
  • 按钮淡入(或 div,哪个更容易)
  • 下面会出现一个带有加载 gif 的隐藏 div
  • Ajax 调用 php 文件对数据库进行更改
  • jquery 然后将加载的 gif 淡出并淡入按钮

我已经为它编写了一些代码,但我认为我在某个地方出错了,有人可以看到我做错了什么并告诉我如何解决它。

这是我的 Jquery:

$(document).ready(function(){
    for(i=0;i<$('#changestatusoff').val();i++){
        (function(x){
            $('#changestatusoff'+x).click(function(){
                $('#changestatusoff'+x).fadeOut('slow',function(){
                    $('#loadingstatus').fadeIn('slow',function(){
                        $.ajax
                        ({
                            type: "POST",
                            url: '.php',
                            data: {'changestatusoff': changestatusoff}, 
                            success: function(data) {
                                return data;
                            },
                            error: function() {
                                alert('Error occured');
                            }
                            $('#loadingstatus').fadeOut('slow',function(){
                                $('#changestatusoff'+x).fadeIn('slow',function();
                            });
                        });
                    });
                });
            });
        });         
    }
})(i);
});

这是 div 中的按钮:

<input type='button' value='Offline' id='changestatusoff".$count."' style='background: none repeat scroll 0% 0% rgb(0, 118, 188); color: rgb(255, 255, 255); border: 1px solid rgb(0, 0, 0); font-weight: bold; cursor: pointer; margin:5px;'/>

感谢任何帮助

【问题讨论】:

  • 您能粘贴一个 HTML 示例吗? ;-)
  • 你在哪里设置changestatusoff 变量?与其循环遍历所有的 ID,不如使用一个类?
  • "alert('Error发生');}后出现语法错误我认为应该有一个");"来结束ajax函数调用。

标签: php jquery mysql ajax


【解决方案1】:

正如其他人提到的,我们不知道您提交的是什么;-)

使用一个类,这意味着它不必为每个项目创建一个新的绑定,它可以一次完成。

你可以这样做:

$(function() {
    //set loading
    var $loading = $('#loadingstatus');

    //on changeStatus click
    $('.changeStatus').click( function(e) {
        //since we dont have a form, disable default behaviour
        e.preventDefault();
        //set $this as the item clicked
        var $this = $(this);
        //fadeIn loading, fadeout the item
        $this.fadeOut();
        $loading.fadeIn();
        //make ajax request
        $.ajax
            ({
            type: "POST",
            url: 'yourPhpFile.php',
            data: {'changestatusoff': $(this).val()}, 
            success: function(data) {
                //Do something with data?
                $loading.fadeOut();
                $this.fadeIn();
            },
            error: function() {
                //Do nothing, and tell an error happened
                alert('An error occured');
                $loading.fadeOut();
                $this.fadeIn();
            }
        });
    });

});

【讨论】:

    【解决方案2】:

    我认为您需要检查 data: {'changestatusoff': changestatusoff} 并尝试将其更改为 data: {changestatusoff: 'changestatusoff'}

    【讨论】:

      【解决方案3】:

      如果你想把loading gif改回来,那么你应该把最后两行:

      $('#loadingstatus').fadeOut('slow',function(){
      $('#changestatusoff'+x).fadeIn('slow',function();
      

      在完成回调中,而不是在 ajax 调用之后。

      $.ajax
      ({
      type: "POST",
      url: '.php',
      data: {'changestatusoff': changestatusoff}, 
      success: function(data) {
          return data;
      },
      error: function() {
          alert('Error occured');
      },
      completed: function() {
          $('#loadingstatus').fadeOut('slow',function(){
          $.('#changestatusoff'+x).fadeIn('slow',function();
      }
      

      【讨论】:

        【解决方案4】:

        试试下面的

        $(function(){
           $('#Your_Event_Source').click(function(){
              $(this).fadeOut(600);
              $('#loadingstatus').fadeIn(600);
              $.post('you_file.php',{changestatusoff: 'yourValue'},function(data){
                 if(data.success == 'yes'){
                     alert(data.message);
                     $('#Your_Event_Source').fadeIn(600);
                     $('#loadingstatus').fadeOut(600);
                 }else{
                     alert('failed'+ data.message);
                     $('#Your_Event_Source').fadeIn(600);
                     $('#loadingstatus').fadeOut(600);
                 }
              },'json');
           });
        });
        

        我建议在 php 中使用“成功”:

        $data = array('success'=>'yes','message' => 'your meaasage',...);
        echo json_encode($data);
        

        【讨论】:

          猜你喜欢
          • 2011-08-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-19
          • 2016-08-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多