I am developing a e-commerce site of pizza shop. i Have a database from where i am getting data about the pizza.I show data in table from database table.
Here is the table:
$sql = "SELECT id, name, price, qty FROM appetisers";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$quantity = $row["qty"];
if ($quantity == '' || $quantity == '0') {
$quantity = '1';
}
?>
<tr class="eachrow">
<td><?php echo $row["name"] ?></td>
<td class="price-amount">£ <?php echo $row["price"] ?></td>
<td><input type="text" name="amount" class="amount-type" value="<?php echo $quantity; ?>"/></td>
<td><a href="" class="add-cart" onclick="add_to_cart('<?php echo $row["name"] ?>', '<?php echo $row["price"] ?>')">Add to cart</a></td>
</tr>
<?php }
}
?>
i want the item-name,item-price and item-quantity to a javascript fite from where i can send to a php file for processing through ajax.My javascript file is:
$(function(){
$('.add-cart').click(function(e){
var val = $(e.target).closest('.eachrow').find('[ name="amount"]').val();
e.preventDefault();
})
function add_to_cart(product_name,product_price){
var dataString = "item_name=" + product_name + "&item_price=" + product_price + "&item_quantity=" + val + "&page=add_to_cart";
$.ajax({
type: "POST",
url: "php/processor/processor.php",
data: dataString,
success: function(response){
console.log(response);
}
})
}
});
and my php file is :
<?php
if(isset($_POST['item_name']) && !empty($_POST['item_name']) && isset($_POST['item_price']) && !empty($_POST['item_price']) && isset($_POST['item_quantity']) && !empty($_POST['item_quantity'])){
echo product_name;
}
?>
But is shows "ReferenceError: add_to_cart is not defined" in console