I am developing a eCommerce website in php without any cms. I have done approx all things, but I am facing a problem in add to cart page. I want to display a successfully message after add to cart with session variable. Please suggest me.
Here is my code:
<?php
session_start();
include('dbfunctions.php');
$id = $mysqli->real_escape_string($_GET['id']);
$category_id=$mysqli->real_escape_string($_GET['category_id']);
?>
<?php
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$products=$mysqli->query("select * from product_details where id=$id and category_id='$category_id'");
if(count($products)>0)
{
$obj=$products->fetch_object(); {
echo '<form method="post" action="cart_update.php">';
echo '<img src="../image/product/'.$obj->pic.'"class="img-responsive" style="width:100%;height:300px;">';
echo ucwords($obj->product_name);
echo $obj->material;
echo $obj->product_code;
echo $obj->area;
echo $obj->width;
echo $obj->rolls;
echo $obj->features;
echo '<button id="button-cart">Add to Cart</button>';
echo '<input type="hidden" name="id" value="'.$obj->uid.'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
}
}
?>
mysqli_query Returns FALSE
on failure and for successful queries if will return TRUE
. You can't use it for count
So instead of this
$products=$mysqli->query("select * from product_details where id=$id and category_id='$category_id'");
if(count($products)>0)
you need to count number of rows
$row_cnt = $products->num_rows;
if(count($row_cnt)>0)
if ($success){
$message = "Sent! Thank you";
} else {
$message = "Ops! Try again!";
}
?><script>
prompt(<?php echo json_encode($message); ?>);
</script>
<noscript>
<p><?php echo htmlspecialchars($message); ?></p>
</noscript>
Note: Given a string for input, json_encode will output a JavaScript string literal that is safe for inclusion in an HTML script element. It will not output JSON.
While the strings themselves don't contain any special characters, it is a good habit to run this sort of XSS protection against anything you output that isn't explicitly HTML/JS/etc.
Or try this
<?php
if ($success) {
$message = "Added! Thank you.";
} else {
$message = "Oops...";
}
echo '<div id="message">'.$message.'<div id="close-button"></div></div>';
?>
This way you can style your message like you want to (like positioning it absolutely). But you would have to implement the close button in javascript if the div is positioned absolute. I hope this helps
For display this type message toast is fit to your requirement.
You could use jQuery for this and ajax to your page as such:
jQuery
var itemName = $( "#itemName" ).val(); //Get the value using the ID of the input
var itemPrice = $( "#itemPrice" ).val(); //Get the value using the id of the input
$.ajax( {
url: "myphpscript.php?itemName=" + ietmName + "&itemPrice=" + itemPrice, //Specify your url to go to (an abosulte path to the php script to run)
type: "GET", //Specify the type, can be GET or POST
success: function ( response ) //Set the success function
{
if (response == "true") //Check the response it "true"
{
alert( "Item addded to cart!" ); //Show success message
return;
}
//response == "false" handler
alert( "Failed to add item to the cart" ); //Show fail message
return;
}
} );
PHP CODE
function addToCart()
{
if (!isset($_REQUEST['itemName']) || !isset($_REQUEST['itemPrice']))
{
return "false";
}
//Add to cart code with `return "true";` or `return "false"` checks
return "true";
}
I hope this goes some way to helping you :)