我有一個簡單的Post/Redirect/Get功能,所以當用戶將產品添加到購物車時,它不會添加另一個他們之後刷新頁面。使用後/重定向/獲取但當頁面被重新定向回來我仍然需要刷新
這裏是我的代碼:
<?php
include("wfcart.php"); ?>
<?php session_start(); ?>
<?php $cart =& $_SESSION['cart'];
if(!is_object($cart)) $cart = new wfCart();
$user_id = get_current_user_id();
$query = mysql_query("SELECT SUM(value) FROM wp_scloyalty WHERE userid = '$user_id'");
$totalpoints = mysql_result($query, 0);
$purchasevalue=array(); ?>
<?php $sqlprize="SELECT prizeid FROM wp_scloyalty_orders WHERE userid = '$user_id'";
$resultprize=mysql_query($sqlprize); ?>
<?php while($rows=mysql_fetch_array($resultprize)){
query_posts('post_type=prizes&showposts=-1&p='.$rows['prizeid'].''); while (have_posts()) : the_post();
$my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
$purchasevalue[] = $my_meta['pointsvalue'];
endwhile;
wp_reset_query();
}
$calctotalnew = $totalpoints - array_sum($purchasevalue);
if($_POST['purchase']) {
foreach($cart->get_contents() as $item) {
$sql="INSERT INTO wp_scloyalty_orders VALUES (".$user_id.", ".$item['id'].")";
$result=mysql_query($sql);
}
$cart->empty_cart();
unset($_SESSION['cart']);
}
$err = array();
if($_POST['add']) {
query_posts('post_type=prizes&showposts=-1&p='.$product_id.''); while (have_posts()) : the_post();
$my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
if($calctotalnew > $my_meta['pointsvalue']){
$cart->add_item(get_the_id(), 1, $my_meta['pointsvalue'], get_the_title());
}else{
//echo 'You do not have sufficient points to redeem this product...';
$err[] = "You do not have sufficient points to redeem this product...";
}
endwhile;
wp_reset_query();
if (!$err) {
// if no errors - saving data
// and then redirect:
header("Location: ".$_SERVER['PHP_SELF']."/my-account");
exit;
} else {
// all field values should be escaped according to HTML standard
foreach ($_POST as $key => $val) {
$form[$key] = htmlspecialchars($val);
}
}
}// else {
// $form['add'] = $form['add'] = '';
//}
?>
所以我的問題是,當我確認從車購買,有一個顯示剩餘funds.In才能看到新的平衡用戶的頁面上的部分他們必須在購買後刷新頁面。
我只是想知道如果我在這裏做的事情真的很錯誤......也許與標題有關:位置的東西?我注意到事情真的取決於不同$ _POSTs的IF語句的順序,但似乎無法實現,所以它在購買時自動更新餘額:/
謝謝!對不起,大量的代碼btw,只是不知道什麼是重要的或不。