1

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.

8
  • %5B%5D is actually [] why its encoded that way? But it should work that way. Commented Apr 27, 2014 at 20:19
  • show th atcode which is generating that url Commented Apr 27, 2014 at 20:21
  • ask yourself. what would you expect var_dump to dump? Commented Apr 27, 2014 at 20:21
  • Added code that generating Get Commented Apr 27, 2014 at 20:27
  • Why $get=$_GET; before var_dump($get); ? What happens if you do directly var_dump($_GET); ? Commented Apr 27, 2014 at 20:33

1 Answer 1

1

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
            )
    )
    */
};
Sign up to request clarification or add additional context in comments.

7 Comments

I'd like to use this but later I will add some more brands and I need to maintain those simultaneously, so replacing Brand[] to Brand is not applicable. But I've tried and this either returned nothing. I'm totally confused
@Hunkeone but in that case, you would create a whole new checkbox, right (or radio buttons)? Because you cannot use one checkbox for multiple brands. So just rename it to BrandHP. And if you later add more brands, name those BrandOtherBrand.
rly, I didn't think of such behavior, thanks. But anyway currently this doesn't works :(
I guess you could set it to to name"brand[0]" and then check for $_GET['brand[0]'] ? And then get rid of the zero when you want to add more.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.