I’m trying to build a very simple Admin area where the administrator can approve or reject image / link ads before they’re shown on the site. They will use radio buttons that contain an “accepted” or “rejected” ID, and the value is the unique ID number for the ad.
I thought I would give the very handy .each() function a try to put all the accepted and rejected ads into their own arrays, and then have PHP process each of them accordingly.
However, on submitting the form, I get a Callback is undefined error from the FireBug console.
Sample HTML (output by PHP):
<li><input type="radio" name="' . $a['adID'] . '" id="accept" value="' . $a['adID'] . '" /> Approve</li>
<li><input type="radio" name="' . $a['adID'] . '" id="reject" value="' . $a['adID'] . '" /> Reject</li>
… and this is the jQuery code:
$(document).ready(function() {
$("#save").click(function() {
var arrayOfAccepted = new Array();
var arrayOfRejected = new Array();
$.each("input:radio[id='accept']:checked"), function() {
arrayOfAccepted.push($(this).val());
}
$.each("input:radio[id='reject']:checked"), function() {
arrayOfRejected.push($(this).val());
}
$.ajax({
type: 'POST',
url: 'exe/fn.moderateAds.php',
data: {acceptedAds : arrayOfAccepted, rejectedAds : arrayOfRejected},
success: function() {
alert("Changes saved successfully.");
}
});
});
});
acceptedAds / rejectedAds in the data parameter would be the variables that I want PHP to identify the arrays by when it looks for them.
Syntax error, or is it a problem with how I’m passing the data through the AJAX function?
Your closing parenthesis is in the wrong place and you’re not selecting elements, but rather iterating a string. Also, your selector is a little too specific.
should be:
And the same for the other one. Right now, it’s being treated as one comma operator with a left-hand operand of
$.each("input:radio[id='accept']:checked")(hence the error you see) and a right-hand operand of an anonymous function that is never used.Code tip: use
[]instead ofnew Array().