duan32342 2015-03-19 07:14
浏览 157
已采纳

为JavaScript中的表单元素创建键值对

I have a module where the forms created are dynamic. So the number of inputs can defer always. Also, the array key can also defer.

My current method of posting form is this:

name = form_options[option_1] value = 1

On submitting the form using POST, I get the form as array in $_POST, which looks like this.

form_options(
    option_1 => 1
)

But, now I am trying to implement the same thing using AJAX. So, I would need a common module to get all form values. I found a way to do it.

var objectResult = $('#options_form').serializeArray();
console.log(objectResult);

This gives me a result like this:

0: Object
  name: "form_options[option_1]"
  value: "1"

How can parse this result to get an array like $_POST array, which I can send as data in AJAX.

P.S: All the form elements have name field as form_options[key]

  • 写回答

3条回答 默认 最新

  • doumanni3501 2015-03-19 07:25
    关注

    You should use this for get post data in PHP file.

    
        // You can use like this    
        var objectResult = $('#options_form').serializeArray();
        $.ajax({
            type: "POST", // Enter Request type GET/POST
            url: 'action.php', // Enter your ajax file URL here,
            dataType: 'json', // If you are using dataType JSON then in php file use die( json_encode($resultArray) );
            data: objectResult, // Put your object here
            beforeSend: function(){
                alert('before');
            },
            error: function(data) {
                console.log(data);
            },
            success: function(response){
                console.log(response);
            }
         });
    
        // In php file get values like this way
        $_POST['form_options']
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果