1

I am trying to create an advanced search system for a simple auction site I am building. I want to have a 'Mozilla Thunderbird' style search system where upon a certain option being selected in a form causes another form to appear with specific select options itself to allow a more narrow search.

For those unfamiliar with the search system I have attatched a picture which should hopefully help provide some context:

Mozilla Thunderbird Advanced Search

This is the code I have so far for this:

<html>
<body>

<?php {

$servername = "";
$username = "";
$password = "";
$dbname ="";

$conn = new mysqli($servername, $username, $password, $dbname);

if($conn->connect_error) {
    die("connection failed: " . $conn->connect_error);
}

$sql = "SELECT DISTINCT `category` FROM `iBayItems`";
$Category = $conn->query($sql);

if($Category->num_rows > 0) {


echo "<h2>Advanced Search</h2>
          <p>Searching for items where: </p>";

    /*First parameters*/
    echo "<form action='TestAdvanced.php' method='POST'>
        <select name='advancedSearch'>
            <option value=''>Select Search...</option>
            <option value='TitleSearch'>Title</option>
            <option value='CategorySearch'>Category</option>
            <option value='PriceSearch'>Price</option>
            <option value='AddressSearch'>Address</option>
            <option value='TimeLeftSearch'>Time Left</option>
        </select>
    </form>";

    (isset($_POST["advancedSearch"])) ? $searchOption = $_POST["advancedSearch"] : $searchOption="";

    /*Operators for first parameters*/
    /*Title Operators*/
    if ($searchOption == '') {
    } else if ($searchOption == 'TitleSearch') {
    echo "<form>
        <select name='TitleOperators'>
            <option value=''>Select Operator...</option>
            <option value='LIKE'>is</option>
            <option value='NOT LIKE'>is not</option>
        </select>
    </form>";

    echo "<form>
        <input type='text' required pattern='[A-Za-z0-9]{0,42}' name='Title'>
    </form>"; 
    } else if($searchOption == 'CategorySearch') {

    /*Category Operators*/
    echo "<form>
        <select name='CategoryOperators'>
            <option value=''>Select Operator...</option>
            <option value='LIKE'>is</option>
            <option value='NOT LIKE'>is not</option>
        </select>
    </form>";

    echo "<form>
        <select name='Category'>
            <option value=''>Select Category...</option>";
                while($row = $Category->fetch_assoc()) {
                    echo "<option value='";
                        echo $row["category"];
                    echo "' required>";
                    echo $row["category"];
        echo "</option>";
                }
    echo "</select>
    </form>";
    } else if($searchOption == 'PriceSearch') {

    /*Price Operators*/
    echo "<form>
        <select name='PriceOperators'>
            <option value=''>Select Operator...</option>
            <option value='='>equals</option>
            <option value='<='>less than or equal to</option>
            <option value='>='>greater than or equal to</option>
            <option value='<'>less than</option>
            <option value='>'>greater than</option>
        </select>
    </form>";

    echo "<form>
        <input type='number' required name='price' min='0' step='0.01'>
    </form>";
    } else if($searchOption == 'AddressSearch') {

    /*Address Operators*/
    echo "<form>
        <select name='AddressOperators'>
            <option value=''>Select Operator...</option>
            <option value='LIKE'>near to</option>
            <option value='NOT LIKE'>not near</option>
        </select>
    </form>";

    echo "<form>
        <input type='text' placeholder='E.g. NR18 9JJ' required name='postcode' pattern='[A-Za-z0-9]{0,42}'>
    </form>";
    } else if($searchOption == 'TimeLeftSearch') {

    /*Time Operators*/
    echo "<form>
        <select name='TimeOperators'>
            <option value=''>Select Operator...</option>
            <option value='='>equals</option>
            <option value='<='>less than or equal to</option>
            <option value='>='>greater than or equal to</option>
            <option value='<'>less than</option>
            <option value='>'>greater than</option>
        </select>
    </form>";

    echo "<form>
        <input type='number' required name='days' min='0' step='1'>
        <p> days</p>
    </form>";
    } ?>
</body>
</html>

The problem I am having is getting the secondary forms to appear, I have tried using "noscript" with a submit form but this isn't really what I need as I don't want the page to reload everytime an option is selected.

I am quite new to PHP so have tried to do as much as I can through research, an understanding of HTML and coding practices and blind luck.

I have used these links so far to help me:

Using $_POST to get select option value from HTML

http://www.vasiljevski.com/blog/2012/01/17/how-to-set-the-selected-option-of-select-tag-from-the-php/

But alas these only got me so far.

As you can see from the picture Thunderbird allows you to have multiple possible searches, this is something I would like to implement myself after I have resolved the current issue I am having.

Any help that you can provide would be absolutely fantastic!

Alex

Community
  • 1
  • 1

0 Answers0