Sorry I am posting again so soon but I really have tried a lot of different option hoping it would work.
I have an jQuery/AJAX form that will create the array based on the users input. The AJAX will then send the values over to my PHP script to pull and place the data from my db.
The problem I am getting is when I check only one box, then the AJAX creates a blank result
It works 100% okay if you select 2 or more values. I was thinking maybe its caused by an array starting with 0 or some other odd factor. I also checked in my console and it is submitting the right values to my PHP page.
Single Value
XHR finished loading: "http://healthbenefitsohio.com/expertise.php?expertise%5B%5D=Ancillary".
Two and up Values
XHR finished loading: "http://healthbenefitsohio.com/expertise.php?expertise%5B%5D=Ancillary&expertise%5B%5D=SmallGroup".
I then decided maybe I can achieve this by using an if statement and this didn't seem to work as well. I tried using the statement in the data
section of the AJAX call, an if/else statement that each had their own AJAX calls in it, and I tried to make the if statement into an variable to pass into the data.
If statement
if(myArray.size() > 0) {
//Function goes here
else {
//Function goes here
}
AJAX call that currently works for two and up values
// Agent search ajax/jQuery
function agentAJAX() {
$('input').on('click', function() { //Pulls data based on radial input
var myArray = [];
var value = $(this).val();
$(".formC input:checkbox:checked").each(function(){
myArray.push($(this).val());
});
$.ajax({
type: "POST",
datatype: "html",
data: {
expertise: myArray
},
url: "expertise.php",
success: function (data) {
$("#bodyA").html(data);
}
});
});
}
agentAJAX();
IF/else AJAX call
// Agent search ajax/jQuery
function agentAJAX() {
$('input').on('click', function() { //Pulls data based on radial input
var myArray = [];
var value = $(this).val();
$(".formC input:checkbox:checked").each(function(){
myArray.push($(this).val());
});
if(myArray.size() > 1) {
$.ajax({
type: "POST",
datatype: "html",
data: {
expertise: myArray
},
url: "expertise.php",
success: function (data) {
$("#bodyA").html(data);
}
});
}
else {
$.ajax({
type: "POST",
datatype: "html",
data: {
expertise: value
},
url: "expertise.php",
success: function (data) {
$("#bodyA").html(data);
}
});
}
});
}
agentAJAX();
If you need more info or code then just let me know and I'll upload it after lunch.
ANSWER
// Agent search ajax/jQuery
function agentAJAX() {
$('input').on('click', function() { //Pulls data based on radial input
var myArray = [];
var value = $(this).val();
$(".formC input:checkbox:checked").each(function(){
myArray.push($(this).val());
});
if(myArray.length === 1) {
$.ajax({
type: "POST",
datatype: "html",
data: {
expertise: value
},
url: "expertise.php",
success: function (data) {
$("#bodyA").html(data);
}
});
}
else {
$.ajax({
type: "POST",
datatype: "html",
data: {
expertise: myArray
},
url: "expertise.php",
success: function (data) {
$("#bodyA").html(data);
}
});
}
});
}
agentAJAX();