I'm working on an Online Store project with PHP and MySQL.
Lets say that in details.php
page which contains all the information of a product, I have this:
<button type="button" name="cart_btn" class="formaction btn btn-fefault"><i class="fa fa-shopping-cart "></i>
Add to cart
</button></br>
And at the same page I added this script as well:
$(document).ready(function(){
$('.formaction').click(function(){
var clickBtnValue = $(this).val();
var ajaxurl = 'details.php',
data = {'action': clickBtnValue};
$.post(ajaxurl, data, function (response) {
// Response div goes here.
alert("action performed successfully");
});
});
});
And also this code:
if (isset($_POST['action'])) {
switch ($_POST['action']) {
case 'add_cart':
add_cart();
break;
case 'select':
select();
break;
}
}
function add_cart(){
if(isset($_POST['cart_btn'])){
$pro_id = $_POST['pro_id'];
$ip = getIp();
$add_cart = $pdo->prepare("INSERT INTO cart (pro_id,qty,ip_add) VALUES ('$pro_id','1','$ip')");
if($add_cart->execute()){
echo "<script>alert('adssadsad')</script>";
}else{
echo "<script>alert('Try Again')</script>";
}
}
}
But the problem is, it does not work at all! I mean when you click on the button, it does not run these scripts...
So what is going wrong here guys, if you know please help me with that.. Thanks!