2015-10-11 28 views
0

PHP重定向錯誤我已經在服務器的PHP購物車 錯誤重定向是我迄今所做的是jQuery中我要送項目ID和單價店cart.php如何糾正服務器

我的js代碼是

var id = getUrlParameter('id'); 
var unitprice=$(".couponPrice").text(); 
window.location = 'shop-cart.php?id=' + id +'&unitprice='+ unitprice; 

當我點擊「加入購物車」按鈕將其重定向到

www.domainname/shop-cart.php?id=sp_gt1&unitprice=1 

和商店購物車我要生成一個訂單號,我的店鋪cart.php碼是

<?php 
ob_start(); 
session_start(); 
//error reporting 
include "script/db.php"; 
error_reporting(E_ALL); 
ini_set('display_errors','1'); 
error_reporting(E_ERROR | E_PARSE); 
?> 


<?php 
////////order id generate///////// 
date_default_timezone_set('Asia/Kolkata'); 
     $stamp = date("ymdhis"); 

$orderid = $stamp; 


if(isset($_GET['OrderId'])) 
{ 
    $_SESSION['OrderId']=$_GET['OrderId']; 
} 


///////////////////////////////////////////////////////////////////////////// 
// SECTION-1  (if you want to add items to your shopping cart) 
///////////////////////////////////////////////////////////////////////////// 
if (isset($_GET['id'])) 
{ 

    $pid = $_GET['id']; 
    $quantity=1; 
    $unitprice=$_GET['unitprice']; 

    $wasFound = false; 
    $i = 0; 

    // If the cart session variable is not set or cart array is empty 
    if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1)     { 

     // RUN IF THE CART IS EMPTY OR NOT SET 
     $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => $quantity,"unit_price"=>$unitprice)); 
    } 
    else 
    { 
     // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT 
     foreach ($_SESSION["cart_array"] as $each_item) { 
       $i++; 
       while (list($key, $value) = each($each_item)) { 
        if ($key == "item_id" && $value == $pid) { 
         // That item is in cart already so let's adjust its quantity using array_splice() 
         array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1,"unit_price"=>$unitprice))); 
         $wasFound = true; 
        } // close if condition 
       } // close while loop 
      } // close foreach loop 
      if ($wasFound == false) 
      { 
       array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => $quantity,"unit_price"=>$unitprice)); 
      } 

    }//else 
    header("location:shop-cart.php?OrderId=".$orderid); 
      exit(); 

}//if 
?> 

我的問題是,當我點擊添加到購物車按鈕,在本地主機它顯示

http://localhost/mydomainname/shop-cart.php?OrderId=151011020441 

但在網上的服務器就說明

http://domainname/shop-cart.php?id=sp_gt1&unitprice=1 

和身體的其餘部分顯示空白。 我希望它重定向以顯示本地主機中顯示的orderId,任何幫助將不勝感激。

+0

可以顯示javascript函數'getUrlParameter'好嗎? – RamRaider

+0

好吧,JavaScript函數'getUrlParameter'似乎從URL返回正確的參數(在我的測試中),所以'sp_gt1'是PID?我在「header」(「location:」)中注意到的一點是,冒號後沒有空格 - 我認爲冒號後需要一個空格,即:header(「location:shop-cart.php? OrderId =「。$ orderid);''也許嘗試在這個'exit($ orderid)'之前放置一個exit語句來查看$ orderid是否生成了 – RamRaider

+0

不,它只顯示了body中的order id而不是在url中重定向,我在js中使用它echo「」;它的工作原因是什麼?@RamRaider – priyabrata

回答

0
 $("#add-to-cart").click(function(e) { 
    e.preventDefault(); 
    error(); 

}); 
function error(){ 
     function getUrlParameter(sParam) 
{ 
    var sPageURL = window.location.search.substring(1); 
    var sURLVariables = sPageURL.split('&'); 
    for (var i = 0; i < sURLVariables.length; i++) 
    { 
     var sParameterName = sURLVariables[i].split('='); 
     if (sParameterName[0] == sParam) 
     { 
      return sParameterName[1]; 

     } 
    } 
} 
var id = getUrlParameter('id'); 
var unitprice=$(".couponPrice").text(); 


     window.location = 'shop-cart.php?id=' + id +'&unitprice='+ unitprice; 




    };//add-to-cart//// 

@RamRaider

0
/* For testing only - comment out if the orderid is set correctly */ 
exit('The value of $orderid is: '.$orderid); 

/* Notice the space after the colon. Also, you can combine with exit() */ 
exit(header("location: shop-cart.php?OrderId=".$orderid));