We help IT Professionals succeed at work.

PHP dropdown search

booneball
booneball asked
on
I am trying to create a search so that when you select a name from a drop down list it takes you to another page and displays the data for that particular user (from the table). The reason for this is because I have about 70 seperate users which I display data for and as you can imagine this is quite a long list.

The select box is as follows:
<label>
    <select name="names" id="names" style="width:28%">
    <option value="">Please select...</option>
    <?php
      $names = mysql_query("SELECT * FROM joomla.jos_users ORDER BY id ASC") or die(mysql_error());
      while($row = mysql_fetch_array($names)) {
            echo "<option value=".$row['id'].">".$row['name']."</option>";
      }

OK so the names are displayed in joomla.jos.users. The table I am getting the data from after selecting a name is it.system. I have to get the data to display by using id eg WHERE names = id

I understand I may also need to use javascipt onclick function.

.
Comment
Watch Question

Most Valuable Expert 2011
Top Expert 2016
Commented:
I can't write the code for you because I do not have your data base to test with, but I can tell you the general design pattern

Your form will be using method="get" since it does not modify the data model.  When the client submits the form, the selected value will be sent to the "action" script in the $_GET array.  You can use var_dump($_GET) to see what arrived, or just look in the URL.  From what is posted here, it looks like you will find the "id" number in $_GET["names"];  The name= attribute of the <select> tag is what will get sent in the request.

Since the GET request contains external data, you must filter it before you use it in a query.  PHP function mysql_real_escape_string() is your friend.  Try:
$id = mysql_real_escape_string($_GET['names']);

Next you will construct a query that says something like this.
$sql = "SELECT * FROM joomla.jos_users WHERE id = '$id' LIMIT 1";

Use var_dump() to print out the row that is returned from the query and you will be able to see the information for just one user.
Project Lead
Commented:
try this out... might work... also pls replace nextpage.php with the php page you want to redirect it to ...

   <select name="names" id="names" style="width:28%" onchange='window.location.href="nextpage.php?id="+this.value'>
    <option value="">Please select...</option>
    <?php
      $names = mysql_query("SELECT * FROM joomla.jos_users ORDER BY id ASC") or die(mysql_error());
      while($row = mysql_fetch_array($names)) {
            echo "<option value=".$row['id'].">".$row['name']."</option>";
      }

on nextpage.php

you can get id using $_GET['id']....

Author

Commented:
Already found the solution before post

Explore More ContentExplore courses, solutions, and other research materials related to this topic.