I'm trying to send the clicked id of an element to my PHP file, using AJAX, so that my SQL statement can select the appropriate data. Everything seems to be working accept that my $_POST
variables remain undefined.
Javascript
$(document).ready(function(){
$('.productsForm').submit(createEntry);
$('.clickProducts').bind('click touchend', refreshEntries);
});
function refreshEntries() {
//Get the id number of the clicked element.
var clicked_id = (this.id);
var cigar_id = clicked_id.toString();
var display = "";
$.ajax({
url: 'php/mobile-select.php',
type: 'POST',
contentType: "application/json",
dataType: 'json',
data: { product_id : cigar_id
},
success: function(json){
//I'm attempting to loop through the JSON object and create product arrays for each cigar.
$.each(json, function(key, value){
$.each(value, function(x, y){
display += '<tr><td>'+ x + '</td><td>' + y +'</td></tr>';
});
});
console.log(display);
}
})
}
PHP
<?php
include("connect-db.php");
// Post remains undefined for some reason. ????
$product_id = $_POST['product_id'];
$select_sql = "SELECT product_name, current_inventory, price FROM products WHERE product_id = '$product_id' ";
$result = $conn->query($select_sql);
$data = array();
while($r = mysqli_fetch_assoc($result)) {
$data[] = $r;
}
$conn->close();
echo json_encode($data);
?>