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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?