jQuery的AJAX张贴到动态表数据的MySQL表 - 我是否以正确的方式做这件事? [英] jQuery AJAX POST to mysql table for dynamic table data- Am I doing this in anything like the right way?

查看:87
本文介绍了jQuery的AJAX张贴到动态表数据的MySQL表 - 我是否以正确的方式做这件事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,我可以动态地添加和删除任意数量的行。一旦数据全部由用户输入,我使用jQuery AJAX功能将其发布到mysql数据库,因此不存在页面重定向或刷新。

我能想到让它工作的唯一方法就是在我的jQuery中使用这个for循环,分别有效地将每行记录到数据库中。这是多么愚蠢?我应该获取所有表格数据并发布一次吗?用户可能有不同数量的行,用户可以在提交之前尽可能多地添加和删除行。

奇怪的我变量计数是由于表格顶部有两行。我无法制定出一个明智的做法。



我对数据总是与正确的行关联有点偏执。



谢谢你的时间。

  jQuery(function(){
jQuery(。button1)。click(function(){
//在此处理表单
var rowCount = jQuery('#dataTable tr')。length;

for(var i = 2; i< rowCount; i ++){
//我将每行的四个元素存储到mysql
var txtRow1 = jQuery('#txtRow'+(i-1))。val();
var tickerRow1 = jQuery( '#tickerRow'+(i-1)).val();
var quantRow1 = jQuery('#quantRow'+(i-1)).val();
var valueRow1 = jQuery( '#valueRow'+(i-1)).val();

var dataString ='txtRow1 ='+ txtRow1 +'& tickerRow1 ='+ tickerRow1 +'& quantRow1 ='+ (
类型:POST,
url: http://rccl.co.uk/form_action1.php,
data:dataString
});
};
return false;
});
});


解决方案

使用您的变量 i ,可以确定所涉及的行的索引。大多数表单处理程序服务器端会将表单的重复键解压缩到一个列表中,像许多具有相同名称的复选框。你可以在这里利用它。

  datastring =''; (var i = 2; i  var txtRow1 = jQuery('#txtRow'+(i-1))。val(); 

var tickerRow1 = jQuery('#tickerRow'+(i-1))。val();
var quantRow1 = jQuery('#quantRow'+(i-1))。val();
var valueRow1 = jQuery('#valueRow'+(i-1))。val();
dataString = datastring +'index [] ='+(i-1)+'txtRow1 [] ='+ txtRow1 +'& tickerRow1 [] ='+ tickerRow1 +'& quantRow1 [] ='+ quantRow1 +'& valueRow1 [] ='+ valueRow1;
}

然后用整个字符串进行ajax调用。



在服务器端,您应该为每个数组获取数组,每个数组的第一个对应第一行,第二个对应第二行,所以上。



自从我使用PHP以来已经很长时间了,但我相信每个关键项目的 [] 符号都是这是PHP $ $ _ POST 的必要条件,它应该将各个键的内容转换为数组。


I have a table which I can dynamically add and delete any number of rows to. Once the data is all entered by the user I am using the jQuery AJAX functionality to POST it to a mysql database so there is no page redirect or refresh.

The only way I could think of getting it to work was using this for loop in my jQuery, effectively posting each row to the database separately. How dumb is this? Should I be getting all the table data and posting it once? There could be any number of rows varying on user and the user could add and delete rows as much as he wants before submitting.

The strange i variable counting is due to there being two th rows at the top of the table. I couldn't work out a smart way of doing this.

I was a bit paranoid about the data always being associated with the correct row.

Thankyou for your time.

jQuery(function() {  
  jQuery(".button1").click(function() {  
    // process form here
    var rowCount = jQuery('#dataTable tr').length;

    for (var i = 2; i < rowCount; i++){
    // the four elements of each row I am storing to the mysql
    var txtRow1 = jQuery('#txtRow' + (i-1)).val();
    var tickerRow1 = jQuery('#tickerRow' + (i-1)).val();
    var quantRow1 = jQuery('#quantRow' + (i-1)).val();
    var valueRow1 = jQuery('#valueRow' + (i-1)).val();

    var dataString = 'txtRow1='+ txtRow1 + '&tickerRow1=' + tickerRow1 + '&quantRow1=' + quantRow1 + '&valueRow1=' + valueRow1;  
    //alert (dataString);return false;  
    jQuery.ajax({  
    type: "POST",  
    url: "http://rccl.co.uk/form_action1.php",  
    data: dataString
    });  
    };
return false;  
});  
});  

解决方案

It looks very clearly to me as if you have a well-established index of the row in question, using your variable i. Most form handlers server-side will unpack repeated keys of the form into a list, for stuff like many checkboxes with the same name. You could exploit that here.

datastring = '';
for(var i=2; i<rowCount; i++) {
    var txtRow1 = jQuery('#txtRow' + (i-1)).val();
    var tickerRow1 = jQuery('#tickerRow' + (i-1)).val();
    var quantRow1 = jQuery('#quantRow' + (i-1)).val();
    var valueRow1 = jQuery('#valueRow' + (i-1)).val();
    dataString = datastring + 'index[]=' + (i-1) + 'txtRow1[]='+ txtRow1 + '&tickerRow1[]=' + tickerRow1 + '&quantRow1[]=' + quantRow1 + '&valueRow1[]=' + valueRow1;
}

Then make the ajax call with the whole string.

On the server-side, you should get arrays for each of these, the first of each of which corresponds to the first row, the second of each of which corresponds to the second row, and so on.

It's been a long time since I used PHP, but I believe the [] symbols for each key item are necessary to clue PHP's $_POST that it should convert the various keys' contents into arrays.

这篇关于jQuery的AJAX张贴到动态表数据的MySQL表 - 我是否以正确的方式做这件事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆