Question

for some strange reason I'm unable to get GET value from URL

$URL=localhost/supplies/cartridges/?Brand%5B%5D=HP

Here's I'm going to check get but this return an empty array

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

var_dump returns next:

array(0) { }

I cannot understand why as this

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

returns

Brand%5B%5D=HP

Unfortunatelly I need to use get Parameters so I've stuck a bit :(

Code that generating 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>

I'd like simply to replace Brand[] to Brand but later I will add some more brands and I need to maintain those simultaneously, so this is not solution

As I was asked to paste whole class here's it is. PS: Sorry for russian/ukrainian characters :)

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>";
}
}

And this how it is used:

<?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();
?>

UPDATES: I have single script in /setua/catalogues.php everything from /setua/supplies/* is redirected to catalogues.php. Path after supplies defines MySQL table to connect with. In case i specify form action to

 $_SERVER['PHP_SELF']

it redirects to URL /setua/catalogues.php so connection to required DB is lost. But then GET parameters are recognized OK. But as soon as URL /setua/supplies/* is specified in form action - GET parameters are lost

ERROR FOUND If you are using $_GET it will only work from the initial page to the next page UNLESS you manually add the GET variables back into any redirects. Sounds like you are submitting your form to a page that redirects elsewhere which looses the $_GET variables.

Was it helpful?

Solution

I would think this would work: (Changed name="Brand[]" to 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>

Then:

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

UPDATE: If you have to stick with name="Brand[]"

Make sure your form is going somewhere, your action tag being blank is odd.

<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>

Then, on 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
            )
    )
    */
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top