douyoupingji7238 2015-03-26 12:37
浏览 58
已采纳

jQuery创建并将数组发布到PHP脚本

My experience with jQuery is very limited, and im on a very steep learning cure.

I have a dynamic form which is generated based on main and sub menu, placing marker points in each row which has an inputfield, I have managed to create a loop which looks for all the marker points and return the field_name and the field_value of any input field.

$('td[edit="1"]').each(function(i, el) {
    field_name = $(el).attr('fieldname');
    field_val = $('#'+field_name).val();

    alert(field_name + " = " + field_val);
});

What I am having trouble with now, is converting this in to an array or JSON and pushing it to a PHP file which can then pick up the results.

Here is an example of how I currently submit a forms data to a PHP file, however is not dynamic as I have to specify which fields and values I want to send.

$.ajax({
    type: "POST",
    url: "form.php",
    data: {
        title : title,
        age : age

    }).done(function(data) {
        results = $(data).find('data').html();

    }).fail(function() {
        alert("error");
    }
);
  • 写回答

2条回答 默认 最新

  • dragon87836215 2015-03-26 12:42
    关注

    As data you can user Jquery on the form to create an array of all the data: https://api.jquery.com/serializeArray/

        $.ajax({
            type: "POST",
            url: "form.php",
                data: $('#idOfForm').serializeArray()
        }).done(function(data) {
            results = $(data).find('data').html();
        }).fail(function() {
            alert("error");
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)