Javascript 如何更正服务器中的php重定向错误
我在php购物车服务器重定向时出错 到目前为止,我所做的是在jquery中,我必须将商品id和单价发送到shop-cart.php 我在js中的代码是Javascript 如何更正服务器中的php重定向错误,javascript,php,jquery,mysql,redirect,Javascript,Php,Jquery,Mysql,Redirect,我在php购物车服务器重定向时出错 到目前为止,我所做的是在jquery中,我必须将商品id和单价发送到shop-cart.php 我在js中的代码是 var id = getUrlParameter('id'); var unitprice=$(".couponPrice").text(); window.location = 'shop-cart.php?id=' + id +'&unitprice='+ unitprice; 当我点击“添加到购物车”按钮时,它会重定向到 www
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
在shop cart中,我必须生成一个订单id,我的shop-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://domainname/shop-cart.php?id=sp_gt1&unitprice=1
身体的其他部分显示空白。
我希望它重定向到显示orderId,如localhost中所示,任何帮助都将不胜感激。$(“#添加到购物车”)。单击(函数(e){
$("#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////
e、 预防默认值();
错误();
});
函数错误(){
函数getUrlParameter(sParam)
{
var sPageURL=window.location.search.substring(1);
var sURLVariables=sPageURL.split('&');
对于(变量i=0;i
@RamRaider您能显示javascript函数
getUrlParameter
吗?好的,javascript函数getUrlParameter
似乎从url返回了正确的参数(在我的测试中),sp_gt1
PID也是这样吗?我在标题(“位置:
中注意到,冒号后面没有空格-我认为冒号后面需要有一个空格,即:标题(“位置:shop cart.php?OrderId=“.$OrderId”);
-也许可以尝试在退出($OrderId)之前放一个exit语句
若要查看$orderid是否生成否,它只在正文中显示订单id,不在url中重定向,但当我在js ie echo“window.location='shop cart.php?orderid='+$orderid”中使用它时,它会起作用,为什么?@RamRaider
/* 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 ) );
/* 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 ) );