0

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!

1

3 Answers 3

1

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.

0

When you pass variable with the ajax method from jQuery, you can pass array like this :

jQuery

    var myArray = newArray();
    myArray.push("data1");
    myString = "data2";


    jQuery.ajax({  
         type: "post",  
         url: ajax_var.url,  
         data: {array:myArray, param2:myString},
                  ^name     ^value
         success: function(count){  
             alert("done"); 
         }  
    }); 

PHP

echo $_POST['array'][0]; // data1
echo $_POST['param2']; // data2
0

Change your addList variable to this:

list[]=Sports&list[]=Cars&list[]=Outdoor&new_list=123123

PHP will parse the items named list[] into an array, and you'll find the values as $_POST['list'][0],$_POST['list'][1],$_POST['list'][2]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.