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
followercount for various social networks. They can be slow in retrieving the data so I thought doing it this way might be the way.fblikeandfbsharefrom the values in the string.