php $ _GET会回显,但在此之后显示为空白

时间:2013-04-24 21:38:23

标签: php get

我觉得这很直截了当。我有这样的$ _GET:

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

这将按预期读出“卡片”,所以稍后我会这样问:

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

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

}

该链接将我发送至index.php?cat=。 它没有读出'卡'。但是,如果我硬编码var:

 $cameFromCat = "Cards";

链接将我发送到index.php?cat = Cards

这让我很生气。我做错了什么?

更新:周围的代码:

$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);

}

}

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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;
}