douqian8238 2017-03-30 13:11
浏览 48

jquery ajax提交表单+额外数据,在php中检索结果

I have a pretty complex html form that consist of 10+ normal input fields and also a dynamic row entry that creates an array of things. Imagine having a form with Personal details and the user can also add extra rows with family member details. I need to be able to send both 'blocks' of data to a php. A simple $('#myForm').serialize(); wont do, because the 2nd block has to be sent as an array of things. The problem that i am facing is this...

When i send the form values this way:

var frm_data = $('#myForm').serialize();
$.ajax({
    "url": "request.php?action=save",
    "data": frm_data,
    "type": "POST",
    "success": function(data) {
            var response = JSON.parse(data);
        if(response.data == "error"){
            //
        }else{
            //
        }
    }
});

I get each input field from the form as an associate php array. Because the 2nd block with the dynamic rows has no unique name= values...(the only distinction is their <tr id=>), i get only the last row's data.

Array
(
    [name] => George Blah
    [address] => Str. 25
    [zip] => 35510
    ...
    [extra_name] => Maria
    [extra_Tele] => 21214454
    [extra_Mobil] => 111111
    [extra_Email] => 
)

But i managed to create a js array of all the 'extra' rows, before i send the form, and i have it stored in a variable, which i have available. The problem is, when i am sending BOTH (the form data and the 2nd block array) to the php file, the form's data is not an associated array anymore.

var extra = [];
//After a pretty complex function, 
//i have all the extra dynamic rows stored in this array

var frm_data = $('#myForm').serialize();
$.ajax({
    "url": "request.php?action=save",
    "data": {"form": frm_data, "extras": JSON.stringify(extra)} ,
    "type": "POST",
    "success": function(data) {
            var response = JSON.parse(data);
        if(response.data == "error"){
                //
        }else{
            //
        }
    }
}); 

And the result is like this:

Array
(
    [form] => name=George+Blah&address=Str+25&zip=35510&=email=&extra_name=Maria&extra_Tele=21214454&extra_Mobil=111111&extra_Email=
    [extras] => [{"extra_name":"Maria","extra_Tele":"21214454","extra_Mobil":"111111","extra_Email":""}]
)

I need the 'form' value to look like the 'extras' value, before it reaches the php file. I dont want to break the string sent to the php based on the '&' sign in case the user adds something like: 'George & Marry' for name, and all hell breaks lose after that.

So basically i want this:

[form] => name=George+Blah&address=Str+25&zip=35510&=email=&extra_name=Maria&extra_Tele=21214454&extra_Mobil=111111&extra_Email=

to be like this:

[form] => [{"name":"George Blah","address":"Str 25","zip":"35510","email":"".....}]

I hope i am understood. :/

Thanks

  • 写回答

1条回答 默认 最新

  • douzhangli9563 2017-03-30 13:19
    关注

    Nevermind, i am an idiot.

    In the php file, all i had to do is:

    parse_str($post["form"], $data);
    

    Now $data holds all the $_POST results in the format i want.

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器