通过PHP帮助的jQuery /阿贾克斯SQL实时更新实时、jQuery、PHP、SQL

2023-09-10 19:03:19 作者:、我输不起

我有一个通过从我的数据库while循环输出所有我接触的表。

我的语法是这样的:

  SELECT * FROM接触WHERE ID = $ _SESSION ['USER_ID'] ORDER BY名ASC LIMIT 5
 

这翻出我的所有数据,只给我5个结果。

现在我的目标是有一个小按钮,打开了jQuery的模型中(这个我可以管理我自己)与窗体要求用户输入一个号码,然后这个数字会通过邮寄发送或到达$ PHP_SELF并更新与用户inputed数量的局部变量,则该变量将被用于更新数据库,以增加或减少LIMIT值

我已经看过所有网站上(与谷歌)寻找提交使用AJAX的一种形式,但所有我已经找到了例子不为我工作。

快递物流管理系统 SSM,JQUERY EASYUI,MYSQL

当用户提交数目和SQL查询被执行和更新为所输出的表,以根据所述新LIMIT值全部而没有在网页清爽向用户动态更新

我jQuery的code是:

 (文件)。就绪(函数(){
   $(#表单形式)。递交(函数(){
   //我们要存储的表单输入框中的值,然后发送通过ajax以下
      VAR VAL = $('输入[名称= NEW_VALUE]')ATTR('值')。
      $阿贾克斯({
          类型:后,
          网址:process.php
          数据:VAL =+ VAL,
          缓存:假的,
          成功:函数(){
             $('#形式的形式)隐藏(函数(){$('成功。')淡入();});
          }
      });
      返回false;
   });
});

$(文件)。就绪(函数(){$(#表单形式)。递交(函数(){
   //我们要存储的表单输入框中的值,然后发送通过ajax以下
   VAR VAL = $('输入[名称= NEW_VALUE]')ATTR('值')。
   $阿贾克斯({类型:后,网址:process.php数据:VAL =+ VAL,缓存:假的,成功的:
   功能(){
      $('#形式的形式)隐藏(函数(){$('成功。')淡入();});
   }});返回false; }); });
 

然后我的PHP code是:

  $ new_val = $ _ POST ['new_val'];
$ _val =更新`settings` SET`display_limit` = {$ new_val}其中`user_id` = {$ _SESSION ['USER_ID]};
的mysql_query($ _ VAL)或死亡(mysql_error());
 

和我的表格很简单:

          

有什么建议?我还没来怎么有我所输出的表动态更新又那么如果任何人都可以点我在正确的方向,或提供一些帮助,这将是真棒。

感谢

编辑:

下面是一个更新的jQuery脚本我工作,我能够成功提交表单!但我唯一的问题是,我无法看到的变化,直到页面刷新着失败的AJAX使用的目的... 叹息的

怎么现在有我#results格更新,并刷新与表单提交的内容?

  $(文件)。就绪(函数(){
    VAR的选择= {
        网址:process.php,
        类型:'后',
        //数据类型:JSON,
        目标:#last_five_sellers,
        成功:成功
    };

    //绑定到表单的提交事件
    $('#形式)。递交(函数(){
        //里面的事件回调'这'是DOM元素,所以我们第一
        //包装在一个jQuery对象,然后调用ajaxSubmit方法
        $(本).ajaxSubmit(选件);
        //!重要 !!!
        //总是返回false以prevent标准的浏览器提交和页面导航
        返回false;
    });
    功能成功(responseText的,$形式){
        $(#的形式表)隐藏();
        $(成功)淡入()。
    }
});
 

解决方案

在你的PHP code,你做了更新,你可以回响在HTML格式的联系人。这将然后返回到你的成功功能的jQuery。

 成功:函数(){
    $('#形式的形式)隐藏(函数(){$('成功。')淡入();});
}
 

该函数有一个参数的数据,这是HTML格式,你的PHP呼应。 示例

 成功:功能(数据){
    $('#形式的形式)隐藏(函数(){$('成功。')淡入();});
    $(数据).appendTo('#结果');
}
 

I have a table that outputs all my contacts via a while loop from my database.

my syntax is like this:

SELECT * FROM contacts WHERE id = $_SESSION['user_id'] ORDER BY name ASC LIMIT 5

that pulls out all my data and only gives me 5 results.

Now my goal is to have a little button that opens up a model box with jquery (this I can manage on my own) with a form asking the user to input a number then that number will be sent via post or get to $PHP_SELF and update a local variable with the number the user inputed, then that variable will be used to update the database to increase or decrease the LIMIT value.

I have looked all over the web (with google) to look for submitting a form using AJAX but all the examples i've found don't work for me.

When the user submits the number and the sql query is executed and updated for the outputed table to dynamically update according to the new LIMIT value all without ever refreshing the page to the user.

my jquery code is:

(document).ready(function(){
   $("form#form").submit(function() {
   // we want to store the values from the form input box, then send via ajax below
      var val = $('input[name=new_value]').attr('value');
      $.ajax({
          type: "post",
          url: "process.php",
          data: "val="+ val,
          cache: false,
          success: function(){
             $('form#form').hide(function(){$('.success').fadeIn();});
          }
      });
      return false;
   });
});

$(document).ready(function(){ $("form#form").submit(function() {
   // we want to store the values from the form input box, then send via ajax below
   var val =    $('input[name=new_value]').attr('value');
   $.ajax({ type: "post", url: "process.php", data: "val="+ val, cache: false, success:   
   function(){
      $('form#form').hide(function(){$('.success').fadeIn();});
   } }); return false; }); });

then my php code is:

$new_val = $_POST['new_val'];
$_val = "UPDATE `settings` SET `display_limit` = {$new_val} WHERE `user_id` = {$_SESSION['user_id']}";
mysql_query($_val) or die(mysql_error());

and my form is simple:

any suggestions? I haven't come to how to have my outputed table dynamically update yet so if anyone can point me in the right direction or provide some help that would be awesome.

thanks

EDIT:

Here is an updated jquery script I was working on, I'm able to submit the form successfully! but my only problem is that I can't see the changes until the page is refreshed with defeats the purpose of the AJAX usage... sigh

how can I now have my #results div updated and refreshed with the form submission content?

$(document).ready(function() { 
    var options = {
        url: 'process.php',
        type: 'post',
        //dataType:  'json',
        target: '#last_five_sellers',
        success: success
    };

    // bind to the form's submit event 
    $('#form').submit(function() { 
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        $(this).ajaxSubmit(options); 
        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false; 
    });
    function success(responseText, $form) {
        $("form#form").hide();
        $(".success").fadeIn();
    }
});

解决方案

In your php code where you do the update, You could echo your contacts in html-format. That would then return to your success function in jquery.

success: function(){
    $('form#form').hide(function(){$('.success').fadeIn();});
}

The function have a parameter data, which is the html-format you echoed in php. Example

success: function(data){
    $('form#form').hide(function(){$('.success').fadeIn();});
    $(data).appendTo('#result');
}