I am new to Ajax and PHP and hope someone can help me with this.
I need to generate an array with unique IDs in jQuery, pass it to a PHP page and return the corresponding values from the PHP page.
So far I have the following code which works on the jQuery side so the first console.log logs the input array correctly, e.g. ["1", "2", "3", "4", "5"].
However, I am not getting any results from the PHP page and the second console.log logs nothing for the output array so I guess I am missing one or more things there.
The result should be an associative array where each item is listed with its tID and the corresponding value from the MySQL query, like "tID" => "1", "value" => "value1" ... .
Can someone help me with this ?
JS side:
function getValues(languageFrm){
var values = [],
tID;
$('.valuesDynamic').find('.values').each(function(){
tID = $(this).attr('name').replace('tID', '');
if($.inArray(tID, values) === -1){
values.push(tID);
}
});
values.sort();
console.log(values);
$.ajax({
type: "post",
url: "ajax.php",
cache: "false",
data: {
node: 'fetchValues',
values: values,
languageFrm: languageFrm
},
success: function(data){
console.log(data);
}
});
}
PHP side:
case "fetchValues":
$values = $_POST["values"];
$languageFrm = $_POST["languageFrm"];
$stmt = $conn->prepare("SELECT tID, " . $languageFrm . " FROM main WHERE tID IN(?) ORDER BY tID");
$stmt->bind_param("s", implode(",", $values));
$stmt->execute();
$result = $stmt->get_result();
while($arrValues = $result->fetch_assoc()){
$result[] = array("tID" => $arrValues["tID"], "value" => $arrValues[$languageFrm]);
}
echo $result;
break;
Many thanks for any help.