【问题标题】:PHP unable to get parameter from URLPHP 无法从 URL 获取参数
【发布时间】:2014-06-13 06:06:30
【问题描述】:

由于某些奇怪的原因,我无法从 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 参数被识别为 OK。但是一旦在表单操作中指定了 URL /setua/supplies/* - GET 参数就会丢失

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

【问题讨论】:

  • %5B%5D 实际上是 [] 为什么要这样编码?但它应该这样工作。
  • 显示生成该网址的代码
  • 问问自己。你希望var_dump 转储什么?
  • 添加生成Get的代码
  • 为什么 $get=$_GET;var_dump($get); 之前?如果你直接var_dump($_GET); 会发生什么?

标签: php get


【解决方案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
            )
    )
    */
};

【讨论】:

  • 我想用这个,但稍后我会添加更多品牌,我需要同时维护它们,因此将 Brand[] 替换为 Brand 不适用。但我试过了,这要么什么也没返回。我完全糊涂了
  • @Hunkeone 但在这种情况下,您会创建一个全新的复选框,对吧(或单选按钮)?因为您不能为多个品牌使用一个复选框。所以只需将其重命名为BrandHP。如果您以后添加更多品牌,请将其命名为 BrandOtherBrand
  • rly,我没有想到这种行为,谢谢。但无论如何,目前这不起作用:(
  • 我想您可以将其设置为 name"brand[0]" 然后检查 $_GET['brand[0]'] 吗?然后在你想添加更多时去掉零。
  • 如果您使用 $_GET,它只会从初始页面到下一页有效,除非您手动将 GET 变量添加回任何重定向。听起来您正在将表单提交到一个页面,该页面重定向到其他地方,这会丢失 $_GET 变量。
猜你喜欢
  • 2017-10-06
  • 1970-01-01
  • 2017-10-15
  • 2021-05-18
  • 1970-01-01
  • 2014-06-13
  • 1970-01-01
  • 2021-02-27
  • 1970-01-01
相关资源
最近更新 更多