2015-08-28 62 views
0

我正在開發一個電子商務網站在PHP中沒有任何cms。我已經完成了所有事情,但我在添加購物車頁面時遇到了問題。我想在將會話變量添加到購物車後顯示成功的消息。請建議我。加入購物車後顯示成功消息

這裏是我的代碼:

<?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.'" />'; 
} 
} 
?> 
+0

你能不能展示'cart_update.php'代碼和你試過的東西? – 2015-08-28 09:39:34

+0

對不起,長碼 –

+0

您可以使用ajax請求來處理'添加到購物車'功能。因此,點擊添加到購物車按鈕,例如使用jQuery向tro'cart_update.php'發送請求。成功時,顯示一個對話框,並顯示錯誤信息(彈出) – sanderbee

回答

0

mysqli_query返回FALSE失敗和成功查詢是否將返回TRUE。你不能用它來計數

所以不是這個

$products=$mysqli->query("select * from product_details where id=$id and category_id='$category_id'"); 

if(count($products)>0) 

你需要計算行

$row_cnt = $products->num_rows; 
if(count($row_cnt)>0) 
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> 

注:鑑於輸入字符串,json_encode會輸出一個可安全包含在HTML腳本元素中的JavaScript字符串文字。它不會輸出JSON。

雖然字符串本身不包含任何特殊字符,但對於不輸出任何明確爲HTML/JS /等的輸出的任何內容,運行此類XSS保護是一種好習慣。

或者像你想(像絕對定位吧)試試這個

<?php 
    if ($success) { 
     $message = "Added! Thank you."; 
    } else { 
     $message = "Oops..."; 
    } 
    echo '<div id="message">'.$message.'<div id="close-button"></div></div>'; 
?> 

這樣你就可以風格你的消息。但是,如果div絕對定位,則必須在JavaScript中實現關閉按鈕。我希望這可以幫助

0

用於顯示此類型的消息吐司適合您的要求。

http://codeseven.github.io/toastr/demo.html

+0

你可以在你的答案中添加一個應用於OP代碼的例子嗎? – 2015-08-28 10:00:58

+0

你可以在這裏找到示例https://github.com/CodeSeven/toastr –

0

您可以使用jQuery,這和AJAX到你的頁面,例如:

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代碼

function addToCart() 
{ 
    if (!isset($_REQUEST['itemName']) || !isset($_REQUEST['itemPrice'])) 
    { 
     return "false"; 
    } 
    //Add to cart code with `return "true";` or `return "false"` checks 

    return "true"; 
} 

我希望這可以幫助你:)