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:

<?php

    $sql="SELECT * FROM licensing ORDER BY id DESC"; 
    $result=mysql_query($sql); 

    $options=""; 

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

    $id=$row["id"]; 
    $url=$row["l_url"]; 
    $phone=$row["l_phone"]; 
    $state=$row["l_state"];
    $sullstate=$row["l_statefull"];  
    $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.

Recommended Answers

All 11 Replies

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; ?>
</select>

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.

<dropdownlist><searchbutton>

Results:
URL
Statefull
etc....

Member Avatar for diafol

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?

Member Avatar for diafol

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?

Same page as stated in a reply, forgot the form tags, 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.

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

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:

<?php
    $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 -->
</form>
<?php
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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.