0

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

3 Answers 3

2

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'].

Sign up to request clarification or add additional context in comments.

3 Comments

When I use this, it sends fileLinks values in simultaneous connections and because of that php script runs more than one time I want PHP to be executed only once. Is there any to do it?
If that's happening, it's for some other reason. This code won't cause multiple connections.
Thanks Barmer - yes you were right I was using .each method and it was causing this problem.
0

Why not just create a json array and then use php's json_decode method?

Comments

0

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>');
    }
 );

Comments

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.