2

How can I make the variable $Category in the mysql query below equal the value chosen in the dropdown list below that?

It seems like a simple concept that can't seem to figure out. It would also be great if the page automatically runs the new query when the dropdown list option changes. Furthermore, it should default to showing all records when the page first loads.

MYSQL PHP QUERY

               $query = mysql_query("SELECT * FROM tblClients  WHERE tblclients.package =  'standard' and tblclients.category = '$Category' LIMIT 0, 9", $connection);

DROPDOWN LIST

    <section class="main">
        <div class="wrapper">
            <div id="dd" class="wrapper-dropdown-3" tabindex="1">
                <span>View By Category</span>
                <ul class="dropdown">
            <?php while ($rows = mysql_fetch_array($query_category)) { ?>
                    <li><a class="<?php echo $rows['category']; ?>"><?php echo $rows['category']; ?></a></li>
            <?php } ?>  
            </ul>
            </div>
        ​</div>
    </section>

1 Answer 1

4

If you do not want to use a form and pass the value of an option from a select, and instead want the user to click a link in a list, you can append the value to the URL and pass it as a get parameter.

Change:

<li><a class="<?php echo $rows['category']; ?>"><?php echo $rows['category']; ?></a></li>

To:

<li><a href="?category=<?php echo $rows['category']; ?>" class="<?php echo $rows['category']; ?>"><?php echo $rows['category']; ?></a></li>

You can then access that variable as a $_GET variable. Like:

$selectedCategory = $_GET['category'];
$query = mysql_query("SELECT * FROM tblClients  WHERE tblclients.package =  'standard' and tblclients.category = " . $selectedCategory . " LIMIT 0, 9", $connection);

Be sure to sanitize your user input data, as well.

Response to your comment:

If you want to use a select and options, you can use a form. The form's action will be the URL of the PHP script that will process the selected values of the form elements. The form's method can be either GET (and you can process the variable similarly to how I outlined previously), or it can be POST, and you can access the variable with the $_POST array rather than the $_GET array.

<form name="form1" action="" method="GET">
    <select name="category">
     <option value="home">Home</option>
     <option value="work">Work</option>
     <option value="school">School</option>
    </select>
    <input type="submit" value="Submit"/>
</form>

Or, you can use javascript to redirect to your processing code onchange of the select:

<select name="category" onchange="location = window.location.href + "?category="+this.selectedIndex.value;">
 <option value="home">Home</option>
 <option value="work">Work</option>
 <option value="school">School</option>
</select>

And process the $_GET variable in the same way that I outlined previously.

Sign up to request clarification or add additional context in comments.

2 Comments

What if I were to make it a SELECT OPTION dropdown list instead. For Example... <select name="category"> <option value="entertainment">Entertainment</option> <option value="catering">Catering</option>
I added to the original answer in response to this comment.

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.