0

I have this jQuery function that reads the values from wp_localize_script declaration.

<script type='text/javascript'>
/* <![CDATA[ */
var assjs = {"action":"get_ass_counts","path":"http:\/\/localhost\/wordpress-dev\/wp-content\/plugins\/advanced-social-status","_wpnonce":"2f3686b7ca","networks":"fblike,fbshare","ajax_url":"http:\/\/localhost\/wordpress-dev\/wp-admin\/admin-ajax.php"};
/* ]]> */
</script>

The parts of the query that I am most interested in is "networks":"fblike,fbshare"

In a separate JavaScript file I have this function:

AJAX Script

$('.advanced-social-status form').on('submit', function(e) {

    e.preventDefault();

    // Array of social networks
    var social_networks = [assjs.networks];

    // cancel previous requests
    if (asswxhr) asswxhr.abort();

    $.each(social_networks, function(key){
        asswxhr = $.ajax({
            type: "POST",
            url: assjs.ajax_url,
            data:{
                action: assjs.action,
                _wpnonce: assjs._wpnonce,
                network: social_networks,
            },
            success: function(data) {
                console.log(data);
                $results.html(data);
            },
            beforeSend: function() {

                $(".ass-fbshare").html('<img style="margin-left:50px;" src="'+assjs.path+'/images/ajax-loader.gif" />');
            }
        });

    });

    return false;
});

What I am trying to achieve is to get each individual social network function for the "networks":"fblike,fbshare" which I reference in jQuery as assjs.networks and loop through and process it once.

Currently it posts both social network objects to my PHP script which processes the ajax request.

PHP Script

$socialnetwork = trim(stripslashes($_POST['network']));

If I var_dump($_POST); in my PHP processing script I get these results:

array(4) {
  ["action"]=>;
  string(14) "get_ass_counts"
  ["_wpnonce"]=>;
  string(10) "2f3686b7ca"
  ["network"]=>;
  array(1) {
    [0]=>;
    string(14) "fblike,fbshare"
  }
  ["s"]=>;
  string(0) ""
}

Is there a way I can separate or break up the social network names in the jQuery file so it only passes EACH social network name once?

Thanks

4
  • so in your array social_network you have duplicates and you want to remove them?
    – StateLess
    Feb 9, 2015 at 8:49
  • Is there a reason you do multiple calls in Ajax? Why not do a single request with the networks to the php code and do all of the breakdowns in the php that way you will always have all of them in your php code at the same time
    – Asheliahut
    Feb 9, 2015 at 8:49
  • @Geohut, I am probably going about it the wrong way but the aim of the script is to call the follower count for various social networks. They can be slow in retrieving the data so I thought doing it this way might be the way.
    – Jason
    Feb 9, 2015 at 8:52
  • @aktiv-coder, don't want to remove them. I want to pull each word out of the array like fblike and fbshare from the values in the string.
    – Jason
    Feb 9, 2015 at 8:54

1 Answer 1

2

you can use split for it

   var social_networks = [assjs.networks];
   social_networks = social_networks.split(',');

MDN ref

it is not the best way to make multiple ajax call's do consider Geohut suggestion.

8
  • 1
    Also side note on this after split and making one call you can use CURL calls or file_get_contents or multiple other options to call the services you need in PHP and gather data and pass back to client.
    – Asheliahut
    Feb 9, 2015 at 9:01
  • Thanks guys. Taking advise from @Geohut if I just pass the array of social networks name to the PHP processing script, would I just need a foreach($_POST['network'] as $network) to process each social network name individually.
    – Jason
    Feb 9, 2015 at 9:06
  • once split and on send I'd recommend JSON encoding it because it is an array and easiest to send it across as such, and then on PHP do check if value isset then do json_decode($_POST['network'])
    – Asheliahut
    Feb 9, 2015 at 9:07
  • @Jason Yes it seems ok :)
    – StateLess
    Feb 9, 2015 at 9:08
  • Awesome, thanks again guys. I will take your advise and only run 1 ajax query. I will post the array of names directly to the PHP script and process them this way. Thank you @aktiv-coder. Your answer is totally correct for the initial question I was asking. Just love Stackoverflow.
    – Jason
    Feb 9, 2015 at 9:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.