PHP无法从URL获取参数

时间:2014-04-27 20:16:21

标签: php get

由于某些奇怪的原因,我无法从URL

获取GET值
$URL=localhost/supplies/cartridges/?Brand%5B%5D=HP

这是我要检查get但是这会返回一个空数组

 public function getGetparameters(){
    $get=$_GET;
    var_dump($get);
}

var_dump返回下一个:

array(0) { }

我无法理解为什么这个

$URL=$_SERVER['REQUEST_URI'];
    $parsedURL=parse_url($URL);
    $URLQ=$parsedURL['query'];
    echo $URLQ;

返回

Brand%5B%5D=HP

不幸的是,我需要使用get参数,所以我有点卡住了:(

生成get的代码:

<form action="" method="get"><h4>Brand:</h4><div class="input-control checkbox">
              <label>
              <input name="Brand[]" type="checkbox" value="HP">
                <span class="check"></span>HP</label>
              </div>
               <button type="submit" class="bg-darkCobalt" style="box-shadow: 0 0 3px 0 #000000; color: #ffffff"><i class="icon-list on-left fg-white"></i>Filter</button>
    </form>

我想简单地将Brand []替换为Brand,但稍后我会添加更多品牌,我需要同时维护这些品牌​​,所以这不是解决方案

当我被要求粘贴全班时,它就是。 PS:对不起俄罗斯/乌克兰人物:)

class CatalogHandler extends generalRenderer{
public function getCatalogName(){
    $URL=$_SERVER['REQUEST_URI'];
    $parsedURL=parse_url($URL);
    $URLPath=$parsedURL['path'];
    $array = explode('/',$URLPath);
    $rev = array_reverse($array);
    $catalog = $rev[1];
    return $catalog;
}
public function getGetparameters(){
    var_dump($_GET);
}



public function renderTitleAndMetaAndSetHeader($newSQL){
    while($row=$this->fetchArray($newSQL)){
        echo "<title>".$row['Title']." ".$this->setSuffix."</title>
             <meta name='description' content='".$row['Desc']."'/>
             <meta name='keywords' content='".$row['KeyWord']."'/>
        ";
        $this->setHeader($row['Title']);
    }
}
public function getCatalogJS($path,$catalogname){
    echo "<script type='text/javascript' src='".$path."js/".$catalogname.".js'></script>";
}
public function getFilters($newSQL,$SQL){
    echo "<div class='grid'> <div class='row'><div class='span3 padding10 border'>
                    <form action='' method='get'>";
    $this->getBrands($newSQL);
    $this->getTypes($SQL);
    echo "<div class='clearfix'></div>
    <button type='submit' class='bg-darkCobalt' style='box-shadow: 0 0 3px 0 #000000; color: #ffffff'><i class='icon-list on-left fg-white'></i>Фильтровать</button>
    </form>
    </div>";
}
public function checkedBrandAndFilter($row){
    if(!empty($_GET['Brand'])&&in_array('HP',$_GET['Brand'])){
        echo "checked='checked'";
    }
    else{
        echo "";
    }
}
public function checkedTypeAndFilter($row){
    if(!empty($_GET['Type'])&&in_array($row['Type'],$_GET['Type'])){
        echo "checked='checked'";
    }
    else{
        echo "";
    }
}
public function getBrands($newSQL){
    echo "<h4>Brand:</h4>";
    while($row=$this->fetchArray($newSQL)){
        echo "<div class='input-control checkbox'>
              <label>
              <input name='Brand[]' type='checkbox' value='".$row['Brand']."'";
               if(!empty($_GET['Brand'])&&in_array('HP',$_GET['Brand'])){
                    echo "checked='checked'";
               }
                else{
                    echo "checked=''";
                    echo $_GET['Brand'];
                }
              echo "/>
                <span class='check'></span>".$row['Brand']."</label>
              </div>
        ";
    }
}
public function getTypes($SQL){
    echo "<h4>Тип:</h4>";
    while($row=$this->fetchArray($SQL)){
        echo "<div class='input-control checkbox'>
              <label>
              <input name='Type[]' type='checkbox' value='".$row['Type']."'
               ".$this->checkedTypeAndFilter($row)."/>
                <span class='check'></span>".$row['Type']."</label>
              </div>
        ";
    }
}
public function getProductNameModelAndLink($row){
    echo "<a href='".$row['Brand']."-".$row['Model']."'><h4>".$row['Brand']." ".$row['Model']."</h4></a>";
}
public function getProductIMG($row){
    echo "<a href='".$row['Brand']."-".$row['Model']."'>
                <img class='span2 shadow' src='".$row['IMGPath']."' alt='".$row['Brand']." ".$row['Model']." купить и провести сервисное обслуживание в Житомире и области'/>
          </a>";
}
public function getBrand4Tech($row){
    if(isset($row['Brand'])){
    echo "Производитель: ".$row['Brand']." / ";
    }
    else{
    }
}
public function getModel4Tech($row){
    if(isset($row['Model'])){
        echo "Модель: ".$row['Model']." / ";
    }
    else{
    }
}
public function getType4Tech($row){
    if(isset($row['Type'])){
        echo "Тип: ".$row['Type']." / ";
    }
    else{
    }
}
public function getCode4Tech($row){
    if(isset($row['Code'])){
        echo "Код: ".$row['Code']." / ";
    }
    else{
    }
}
public function getProductTechInfo($row){
    $this->getBrand4Tech($row);
    $this->getModel4Tech($row);
    $this->getType4Tech($row);
    $this->getCode4Tech($row);
}
public function renderProducts($SQL,$catalogname){
    echo "<div class='span9'>";
    while($row=$this->fetchArray($SQL)){
        $this->getProductNameModelAndLink($row);
        $this->getProductIMG($row);
        echo "<div class='span7'>";
        $this->getProductTechInfo($row);
        $this->renderPrice($row);
        $this->getBuyButton($catalogname,$row);
        $this->renderStock($row);
        echo "</div>";

                    echo "<div class='clearfix'></div>";
    }
    echo "</div></div></div>";
}
}

以及如何使用它:

<?php
include('CatalogHandler.php');
include('DBconfig.php');

$config = new DBconfig('localhost','setuser','a11235813b','setua');
$con=mysqli_connect('localhost','setuser','a11235813b','setua');
$catalog = new CatalogHandler($config);
$catalogname=$catalog->getCatalogName();

$catalog->openConnection();
$query="SELECT * FROM metaandtitlecatalogs WHERE Cat='".$catalogname."'";
$query4brands="SELECT DISTINCT(Brand) AS Brand FROM ".$catalogname;
$query4types="SELECT DISTINCT(Type) AS Type FROM ".$catalogname;
$query4catalog="SELECT * FROM ".$catalogname;

$catalog->startHead('../../');
$catalog->getGetparameters();
$result = mysqli_query($con,$query);
$result4brands=mysqli_query($con,$query4brands);
$result4types=mysqli_query($con,$query4types);
$result4catalog=mysqli_query($con,$query4catalog);

$catalog->renderTitleAndMetaAndSetHeader($result);
$catalog->getGenCSS('../../');
$catalog->getGenJS('../../');
$catalog->getJS4Filters('../../');
$catalog->getCatalogJS('../../',$catalogname);
$catalog->closeHead();
$catalog->getTopBarAndStartBody('../../');
$catalog->getHeader();
$catalog->getFilters($result4brands,$result4types);
$catalog->renderProducts($result4catalog,$catalogname);
$catalog->getGetparameters();
?>

更新 我在/setua/catalogues.php中有单个脚本 来自/ setua / supplies / *的所有内容都被重定向到catalogues.php。 耗材后的路径定义要连接的MySQL表。 如果我指定表格行动

 $_SERVER['PHP_SELF']

它重定向到URL /setua/catalogues.php,因此丢失了与所需数据库的连接。但是GET参数被识别出来了。但是只要在表单操作中指定了URL / setua / supplies / *,GET参数就会丢失

发现错误 如果您使用$ _GET,它只能从初始页面到下一页面工作,除非您手动将GET变量添加回任何重定向。听起来您正在将表单提交到重定向到其他地方的页面,这些页面会丢失$ _GET变量。

1 个答案:

答案 0 :(得分:1)

我认为这样可行:(将name="Brand[]"更改为name="Brand"

<form action="" method="get">
    <h4>Brand:</h4>
    <div class="input-control checkbox">
        <label>
            <input name="Brand" type="checkbox" value="HP">
            <span class="check"></span>HP
        </label>
    </div>
    <button type="submit" class="bg-darkCobalt" style="box-shadow: 0 0 3px 0 #000000; color: #ffffff"><i class="icon-list on-left fg-white"></i>Filter</button>
</form>

然后:

public function getGetparameters() {
    $get = $_GET;
    var_dump($get);  //Should get you brand => HP
};

更新:如果您必须坚持使用name="Brand[]"

确保您的表单在某处,action标记为空白是奇数。

<form action="catalogs.php" method="get">
    <h4>Brand:</h4>
    <div class="input-control checkbox">
        <label>
            <input name="Brand[]" type="checkbox" value="Apple">
            <span class="check"></span>Apple
        </label>
        <label>
            <input name="Brand[]" type="checkbox" value="HP">
            <span class="check"></span>HP
        </label>
    </div>
    <button type="submit" class="bg-darkCobalt" style="box-shadow: 0 0 3px 0 #000000; color: #ffffff"><i class="icon-list on-left fg-white"></i>Filter</button>
</form>

然后,在catalogs.php上:

public function getGetparameters() {
    $get = $_GET;
    print_r($get); 
    /* Will result with the following if only HP was checked off
    Array
    (
        [Brand] => Array
            (
                [0] => HP
            )
    )
    */
    /* Will result with the following if both checked off
    Array
    (
        [Brand] => Array
            (
                [0] => Apple
                [1] => HP
            )
    )
    */
};