Hi all, I have looked everywhere (obviously) to find the snipet of code required to show results from a dropdown list. I just need something simple that works. Here is my code so far:


    $sql="SELECT * FROM licensing ORDER BY id DESC"; 


    while ($row=mysql_fetch_array($result)) { 

    $options.="<option value=\"$id\">".$state;

    <select name="listing"><option value=0>Choose<?=$options?></option></select>
    <input type="submit" value="Search"/>

If someone could complete the echo results from the selected state, showing the variable results.

I appreciate any help.

4 Years
Discussion Span
Last Post by EvolutionFallen

Not really sure what you're looking for here. Could you elaborate?
Every <option> tag needs a corresponding closing </option> tag, but you only have one.
Change line 15 to $options.="<option value=\"$id\">".$state."</option>;
Then change line 19 to

<select name="listing">
    <option value="0">Choose</option>
    <?php echo $options; ?>

OK, so... I have a table with state, url, etc.

The dropdown menu consists of the state abbreviations NM, CA.

I need the "search" button to then print the corresponding state information from the database onto the screen.




Do you need this to go to the server every time you chose state or can the data be held in a javascript variable (json format) on page load and the output updated via js on searchbutton click?


Where are your form tags? Not present in the code supplied.
Are you sending to the same page or are you sending to a form handler and redirecting to the form page?
Do you want to send by get or post?


You're going to want to use AJAX to call a php script from your page and then use the php script to query your database and to echo the results back to the page.

        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
        <script type="text/javascript">
            function searchOccupation () {
                    url: "searchOccupation.php?search=" + $('#searchTxt').attr('value'),
                    success: function (data) {
        <input type="text" id="searchTxt">
        <input type="button" value="Search" id="searchBtn" onclick="searchOccupation()">

Then your php script (whose name should match that in the "url" field of the ajax call (in this case it should be named "searchOccupation.php") will look like this:

    $searchTxt = $_GET['search'];
    $con = new mysqli('server', 'user', 'password', 'database');
    if (!$con) {die("failed to connect: " . $con->connect_error;)}
    $sql = "SELECT * FROM tableName WHERE occupation = '" . $searchTxt . "'";
    $result = $con->query($sql);
    if (!$result) {die("No result set");}
    while($row = $result->fetch_assoc()) {
        echo $row['firstName'];  //This sends data back to the page 

Sorry Jack, but as requested I just need php help. I have a dropdown menu, which searches my database for the state (this works just fine). I just need the data result to be shown on the page, after hitting search. Just something simple. I already have a connection include file to my database in my .php page.


So what's the problem? Jack's solution should work fine for you, just drop the JavaScript part, it's not necessary, just prevents a page reload.
The only thing that's missing is your <form> tag from what I see.

<form id="[your form id here]" action="[page filename here]" method="get">
    <!-- your SELECT element goes here -->
if(!empty($_GET['listing'])) {
    $searchTxt = mysql_real_escape_string($_GET['listing']);
    $con = new mysqli('server', 'user', 'password', 'database');
    if (!$con) {die("failed to connect: " . $con->connect_error;)}
    $sql = "SELECT * FROM tableName WHERE fieldname = '" . $searchTxt . "'"; // edit tablename and fieldname as needed
    $result = $con->query($sql);
    if (!$result) {die("No result set");}
    while($row = $result->fetch_assoc()) {
        echo $row['firstName'];  // change firstName as needed, use more echo statements for all data you need to output 

Ok, never mind... I have a drop down list, I just needed a little extra code added on to show results on the same page, I don't have a search box, I already have an include file, was just looking for print/echo results from a drop down list selection.

Thanks for the somewhat off the subject help.


Your terminology is really confusing. It seems we've covered all that. My last script (which is just a modification of jacksparrow0109's) along with the <select> element I showed in my first post shows exactly how to use a "dropdown" (aka SELECT tag/element) which print results to the same page based on the user's selection.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.