dongxifu5009 2013-09-29 11:41
浏览 51
已采纳

使用AJAX将数组和字符串从Javascript传递到PHP

I have the following ajax call.

My addList variable holds a string: list=Sports&list=Cars&list=Outdoor&new_list=123123

I want to grab the addList in my PHP file as

$_POST['list'] is an array with values Sports, Cars, Outdoor
$_POST['new_list'] is a string 123123

But I couldnt convert the POST string into right forms.

I can create arrays/loops in both sides but it didnt feel right.

Whats the convenient way of doing it?

jQuery.ajax({  
                    type: "post",  
                    url: ajax_var.url,  
                    data: "action=post-list&nonce="+ajax_var.nonce+"&post_list=&post_id="+post_id+"&" + addList,  
                    success: function(count){  
                        alert("done"); 
                    }  
                }); 

Any help will be appreciated. thanks!

  • 写回答

3条回答 默认 最新

  • dongtan2603 2013-09-29 11:57
    关注

    try using followig code.

    you just neeed to locate your form if and url to pass values to :

    var form = new FormData($('#form_id')[0]);
    form.append('view_type','addtemplate');
    $.ajax({
    type: "POST",
    url: "savedata.php",
    data: form,
    cache: false,
    contentType: false,
    processData: false,
    success:  function(data){
    //alert("---"+data);
    alert("Settings has been updated successfully.");
    window.location.reload(true);
    }
    });
    

    this will pass all form element automatically.

    Working and tested code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器