Retrieve value from database using drop down list

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lala
Forum Commoner
Posts: 27
Joined: Mon Jul 26, 2010 7:56 pm

Retrieve value from database using drop down list

Post by lala »

I want to do search function using 2 drop down list, 1 to list down criteria, another one to prompt the value from database.

I can do search by using text field but still have some problem include file for drop down list.

This is code for searchform.php

Code: Select all

<script language="JavaScript">

function setmode(m)
{
 var w = m.selectedIndex;
 var selected_text = m.options[w].text;
 if (m.options[m.selectedIndex].value == '#') return;
 if (m.options[m.selectedIndex].value == "a") mode = "a";
 if (m.options[m.selectedIndex].value == "b") mode = "b";

}

function showSelected(val)
{

 if (val == "a")
 {
  document.getElementById('dv').style.display = 'none';
  document.getElementById('image').style.display = 'none';
  var newDiv = "<input type='text' name='keyword'>";
  document.getElementById('selectedResult').innerHTML = "  Staff Name : " +  newDiv;
 }
  if (val == "b")
  {
  
  }

}

function app(f)
{
 url = "search.php?mode="+mode+"&key="+document.getElementById('keyword').value;
 url = encodeURI(url);
 f.action = url;
 f.submit();
 return true;
}



function validateForm()
{
 var okSoFar=true;
 var f=document.track;
 if (f.criteria.value == "#" && okSoFar)
 {
  ok=false
  alert("Please Select A Search Option.")
  f.criteria.focus()
 }

 if (ok==true)
 {
  f.submit();
 }
}
</script>

<head><title>Search </title>

</head>



<div align="center">

     <form action="search.php"  method="post" name="search" onsubmit="">
     <table>
	 <tr><b>Search for Translation Task</b></tr>
            <tr>

                 <td>Search By: </td>
                 <td><select name="criteria" onchange="javascript:showSelected(this.value)" id="theValue">
                             <option value="#">[Select Search Option]</option>
                             <option value="a">Staff Name</option>
                             <option value="b">Guest</option>

                    </select></td>


                 </tr>
            <tr>
                 <td></td>
                 <td align="right"><input type="button" value="SEARCH" onclick="validateForm();"></td>
            </tr>
     </table>
     </form>


?>
if I wan change (val == "a") from text box to drop down list by including name.php, Anyone can help?

name.php

Code: Select all

<?php
include "../opendb.php";


$sql="select name from staff";
$res=mysql_query($sql);
$num=mysql_num_rows($res);

echo "<select name=\"staff\">";
echo "<option value=''>--- Please Select an staff  ---</option>";
for($i=0; $i<$num; $i++)
{
 $row=mysql_fetch_array($res);
 echo "<option value='".ucwords($row['staff'])."'>".ucwords($row['staff'])."</option>";
}
echo "</select>";

include "../closedb.php";
?>

I appreciate your help. Thanks
User avatar
Technocrat
Forum Contributor
Posts: 127
Joined: Thu Oct 20, 2005 7:01 pm

Re: Retrieve value from database using drop down list

Post by Technocrat »

The prettiest way would be to use AJAX. You can write on your own if you wanted, however my suggestion would be to use a library like JQuery.

Read the value selected from the box, pass it to the name.php (REMEMBER to validate your input!!), pass it back as a JSON, and write it to the screen.

http://api.jquery.com/category/ajax/

http://us2.php.net/manual/en/ref.json.php
lala
Forum Commoner
Posts: 27
Joined: Mon Jul 26, 2010 7:56 pm

Re: Retrieve value from database using drop down list

Post by lala »

Technocrat wrote:The prettiest way would be to use AJAX. You can write on your own if you wanted, however my suggestion would be to use a library like JQuery.

Read the value selected from the box, pass it to the name.php (REMEMBER to validate your input!!), pass it back as a JSON, and write it to the screen.

http://api.jquery.com/category/ajax/

http://us2.php.net/manual/en/ref.json.php
Thanks. I try to understand it first.
Post Reply