2013-04-24 104 views
0

我觉得这很简单。我有一个$ _GET这样的:

$cameFromCat = $_GET['cat']; 
echo $cameFromCat; 

这会读出“牌”不如预期,所以后来我问它:

if ($_POST['submission'] == "Continue Shopping") { 

    Header("location: index.php?cat=" . $cameFromCat); 

} 

链接发送我index.php?cat=。 它不会读出'卡片'。但是,如果我硬编码的变种,如:

$cameFromCat = "Cards"; 

的链接发给我的index.php猫=卡

它严重快把我逼疯了?我究竟做错了什么?

UPDATE:周围代码:

$cart = $_SESSION['cart']; 
$cameFromCat = $_GET['cat']; 
$cameFromPage = $_GET['pagenum']; 
$action = $_GET['action']; 
$cardqty2 = $_POST['var']; 

switch ($action) { 
case 'add': 
    if ($cart) { 
     for ($i = 1; $i <= $cardqty2; $i++) { 
      $cart .= ','.$_GET['id']; 
     } 
    } else { 
     $cart = $_GET['id']; 
     for ($i = 2; $i <= $cardqty2; $i++) { 
      $cart .= ','.$_GET['id']; 
     } 
    } 
    break; 
case 'delete': 
    if ($cart) { 
     $items = explode(',',$cart); 
     $newcart = ''; 
     foreach ($items as $item) { 
      if ($_GET['id'] != $item) { 
       if ($newcart != '') { 
        $newcart .= ','.$item; 
       } else { 
        $newcart = $item; 
       } 
      } 
     } 
     $cart = $newcart; 
    } 
    break; 
case 'update': 

    if ($_POST['submission'] == "Update") { 

    if ($cart) { 
     $newcart = ''; 
     foreach ($_POST as $key=>$value) { 
      if (stristr($key,'qty')) { 
       $id = str_replace('qty','',$key); 
       $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart); 
       $newcart = ''; 
       foreach ($items as $item) { 
        if ($id != $item) { 
         if ($newcart != '') { 
          $newcart .= ','.$item; 
         } else { 
          $newcart = $item; 
         } 
        } 
       } 
       for ($i=1;$i<=$value;$i++) { 
        if ($newcart != '') { 
         $newcart .= ','.$id; 
        } else { 
         $newcart = $id; 
        } 
       } 
      } 
     } 
    } 
    $cart = $newcart; 
    break; 

} 

if ($_POST['submission'] == "Checkout") { 

Header("Location: address.php"); 

} 

if ($_POST['submission'] == "Continue Shopping") { 

Header("location: index.php?cat=" . $cameFromCat); 

} 

}

+9

显示更多的代码,因为这是没有太大的去。您必须在某处重置变量。 – 2013-04-24 21:39:35

+0

你的回声和你的if语句之间发生了什么? – Dan 2013-04-24 21:40:23

+1

你是否在POST过程中包含GET变量的URL? – showdev 2013-04-24 21:41:37

回答

1

确保所有的POST和GET值已被正确地发送到您的脚本:

// check if GET['cat'] isset 
if(!isset($_GET['cat'])) { 
    die('GET.cat not set'); 
} 

// check if POST['submissuion'] isset  
if(!isset($_POST['submission'])) { 
    die('POST.submission not set'); 
} 

$cameFromCat = $_GET['cat']; 
echo $cameFromCat; 

if ($_POST['submission'] == "Continue Shopping") { 
    Header("location: index.php?cat=" . $cameFromCat); 
} 
0

我认为这个问题是与开关条件有关。

看看你的这部分代码:

case 'update': 

    if ($_POST['submission'] == "Update") { 

    if ($cart) { 
     $newcart = ''; 
     foreach ($_POST as $key=>$value) { 
      if (stristr($key,'qty')) { 
       $id = str_replace('qty','',$key); 
       $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart); 
       $newcart = ''; 
       foreach ($items as $item) { 
        if ($id != $item) { 
         if ($newcart != '') { 
          $newcart .= ','.$item; 
         } else { 
          $newcart = $item; 
         } 
        } 
       } 
       for ($i=1;$i<=$value;$i++) { 
        if ($newcart != '') { 
         $newcart .= ','.$id; 
        } else { 
         $newcart = $id; 
        } 
       } 
      } 
     } 
    } 
    $cart = $newcart; 
    break; 

} 

突破;是if ($_POST['submission'] == "Update")

另一个问题里面是一个}缺少包围switch语句。

我合格的猜测是,你想要的代码更改为:

case 'update': 
    if ($_POST['submission'] == "Update") { 

    if ($cart) { 
     $newcart = ''; 
     foreach ($_POST as $key=>$value) { 
      if (stristr($key,'qty')) { 
       $id = str_replace('qty','',$key); 
       $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart); 
       $newcart = ''; 
       foreach ($items as $item) { 
        if ($id != $item) { 
         if ($newcart != '') { 
          $newcart .= ','.$item; 
         } else { 
          $newcart = $item; 
         } 
        } 
       } 
       for ($i=1;$i<=$value;$i++) { 
        if ($newcart != '') { 
         $newcart .= ','.$id; 
        } else { 
         $newcart = $id; 
        } 
       } 
      } 
     } 
    } 
    $cart = $newcart; 

    } 
    break; 
} 
+0

我还在为狗生根... – raidenace 2013-04-24 22:13:05

+0

@Raidenace - 这取决于你:-) – bestprogrammerintheworld 2013-04-24 22:14:54