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 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏