I am trying to send different values to php script using jQuery.post
The data I want to send are links like this
http://example.com/image1.jpg
http://example.com/image2.jpg
http://example.com/image3.jpg
http://example.com/image4.jpg
I don't have an exact amount of these links they would vary depending on the user interacting with my script
I am storing these links in a javascript array "fileLinks".
what I want to do is to send these links to php using jQuery.post
Can I format these links in this shape?
jQuery.post(MyAjax.ajaxurl,
{ action : 'ajax-rform',
link1: http://example.com/image1.jpg,
link2: http://example.com/image2.jpg,
link3: http://example.com/image3.jpg,
link4: http://example.com/image4.jpg,
},
function(data){
jQuery("#fileupload").after('<h1>Success</h1><p>Your registration has been recieved.</p>');
}
);
In php I just need to use for loop and replace the link1 and http://example.com/image1.jpg with the arrays and php would iterate it for me but what should I do in javascript?
Thanks
Just pass the array. jQuery will encode it with array notation, and PHP will decide it back into an array:
jQuery.post(MyAjax.ajaxurl,
{ action : 'ajax-rform',
link: fileLinks
},
function(data){
jQuery("#fileupload").after('<h1>Success</h1><p>Your registration has been recieved.</p>');
});
In PHP, you can access the array as $_POST['link']
.
Why not just create a json array and then use php's json_decode
method?
Perhaps you should just make a data object to send, populate it from your array like this
var postData = { action : 'ajax-rform' };
for(var i = 0; i < fileLinks.length; i++){
postData["link"+(i+1)] = fileLinks[i];
}
And then use that in your ajax
jQuery.post(MyAjax.ajaxurl,
postData,
function(data){
jQuery("#fileupload").after('<h1>Success</h1><p>Your registration has been recieved.</p>');
}
);