0

I'm trying to check to see if isset is empty and if it is then I would like to do something else.

I have a few checkboxes in my form and ONE input field (hidden field and its name is keyword2). if the users check any of the checkboxes and submit the form, they will be presented with the requested information from mysql database. this works fine and as it should.

on the page load, none of the checkboxes is checked BUT the input field always will have a value in it and it will never be empty.

So, I am trying to load some Other data/information on page load if the users submit the Form without checking any checkbox!

This is what I have done so far:

<?php
$searchList = "";
$clause = " WHERE ";//Initial clause
$sql="SELECT *
FROM `yt`
INNER JOIN `ATTRIBUTES` ON yt.id=ATTRIBUTES.id";//Query stub
    if(isset($_POST['keyword']) && !empty($_POST['keyword'])){

        foreach($_POST['keyword'] as $c){
            if(!empty($c)){
                $keyword2 = $_POST['keyword2'];
                ##NOPE##$sql .= $clause."`".$c."` LIKE '%{$c}%'";
                $sql .= $clause . " (ATTRIBUTES.sizes LIKE BINARY '$c' OR ATTRIBUTES.colors LIKE BINARY '$c') AND ATTRIBUTES.type='$keyword2'";
                $clause = " OR ";//Change  to OR after 1st WHERE
            }
        }
        $sql .= " GROUP BY yt.id";
        $query = mysqli_query($db_conx, $sql);
            $productCount = mysqli_num_rows($query);
            if ($productCount > 0) {
                while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
             $id = $row["id"];
             $product_name = $row["product_name"];
                   $searchList .= '<a class="overlay" href="product.php?id='.$id.'"></a>';
                }
            }
        }

        ///////////////IF EMPTY////////////////
if(isset($_POST['keyword']) && empty($_POST['keyword'])){
   $keyword2 = $_POST['keyword2'];
   $sql = "SELECT * FROM ATTRIBUTES WHERE type='$keyword2'";
   $query = mysqli_query($db_conx, $sql);
   $productCount = mysqli_num_rows($query );
            $productCount = mysqli_num_rows($query);
            if ($productCount > 0) {
                while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
             $id = $row["id"];
             $product_name = $row["product_name"];
                   $searchList .= '<a class="overlay" href="product.php?id='.$id.'"></a>';
                }
            }
        }
?>

As you can see in the code above, I first check if the checkboxes ($keyword) is not empty and if not empty, I will do something and this works fine.

and in the second part of the code, I am checking to see if IT IS EMPTY and if it is then I want to do something else but this doesn't really work and as if it is not checking for it being empty and I will not get any result after I submit the form.

I hope I explained it properly so someone can help me.

any help would be appreciated.

Thank in advance.

TERO
  • 159
  • 2
  • 16
  • BTW: `isset && !empty` is redundant: http://stackoverflow.com/a/4559976/476 – deceze Mar 11 '15 at 09:41
  • You can learn how [`empty()`](http://php.net/manual/en/function.empty.php) and [`isset()`](http://php.net/manual/en/function.isset.php) functions work from the [PHP type comparison tables](http://php.net/manual/en/types.comparisons.php) – axiac Mar 11 '15 at 09:58

1 Answers1

1

When a checkbox is not ticked in HTML, PHP won't receive the variable at all.

You'll need to just check if the key exists using array_key_exists():

if( !array_key_exists('keyword', $_POST) )
{
    // The checkbox was not checked 
    // or at least there is no 'keyword' key in the $_POST array.
}
BenM
  • 52,573
  • 26
  • 113
  • 168