<form>
	<table border="0" width="100%">
	<tr>
		
	<td align="right" width="60%"><select name="select" size="1"  >
	<option>Select Category</option>
	<? $sql1=mysql_query("select name from categories") or die(mysql_error());
	$num=mysql_num_rows($sql1);
	while($row=mysql_fetch_array($sql1))
		{
			$category = $row['name'];
	
       ?>
	<option><?php echo $category; ?></option>
		 
      <?     
	    }
      ?>
	</select>
     </td>		
	</tr>
	</table>
</form>

Here i got i.e. it display
in list student, teacher ,professor etc.
i want to do like when click on any option without press button
only click on option in list
they fire query

select * from table where name='student'; // i.e. selected value from list

my problem is how to check that option is select or not
or get the selected value ??

<form>
	<table border="0" width="100%">
	<tr>		
	<td align="right" width="60%"><select name="select" size="1"  onChange='submit()'>
	<option value='0'>Select Category</option>
	<? $sql1=mysql_query("select name from categories") or die(mysql_error());
	$num=mysql_num_rows($sql1);
	while($row=mysql_fetch_array($sql1))
		{
			$category = $row['name'];
	
       ?>
	<option value='<?php echo $category; ?>'><?php echo $category; ?></option>
		 
      <?     
	    }
      ?>
	</select>
     </td>		
	</tr>
	</table>
</form>

i think you can either submit your form for processing php script on change event of select or you can call javascript function which will call php page for processing. 2nd option is known as Ajax with php.
If you wanna see ajax and php then visit given url
http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=333
hope this will help you.
Dilip Kumar Vishwakarma

Here i am modified code...

<form>
<table border="0" width="100%">
<tr>
<td align="right" width="60%">
<select name="cmbcategory" size="1" onChange="submit();">
<option value="0">Select Category</option>
<? $sql1=mysql_query("select name from categories") or die(mysql_error());
  while($row=mysql_fetch_array($sql1))
 {
	$category = $row['name'];
?>
 <option value="<?php echo $category; ?>"><?php echo  $category; ?></option>

 <?     
     }
  ?>
  </select>
   <?     
	    echo "<p>You are selected: '".$_GET['cmbcategory']."'</p>\n"; 
  ?>
 </td>		
</tr>
</table>
</form>

Here i am choosing option from list...

echo "<p>You are selected: '".$_GET['cmbcategory']."'</p>\n";

I get correct echo value of selected option
But in list view it shows Select Category

e.g suppose i chose student...
in echo it gives You are selected: student
but in option list it shows Select Category not student

where is problem in code??

Try:

<form>
<table border="0" width="100%">
<tr>
<td align="right" width="60%">
<select name="cmbcategory" size="1" onChange="submit();">
<option value="0">Select <?php if (isset($_GET['cmbcategory'])) {echo $_GET['cmbcategory'];} else echo "category"; ?></option>
<? $sql1=mysql_query("select name from categories") or die(mysql_error());
  while($row=mysql_fetch_array($sql1))
 {
	$category = $row['name'];
?>
 <option value="<?php echo $category; ?>"><?php echo  $category; ?></option>

 <?     
     }
  ?>
  </select>
   <?     
	    echo "<p>You are selected: '".$_GET['cmbcategory']."'</p>\n"; 
  ?>
 </td>		
</tr>
</table>
</form>
Member Avatar for diafol

If I get you right, you want to display the selection as additional text below the same form? This doesn't seem to make much sense.

(i) Forms should be passed to a form handler where data can be validated and appropriate action taken, e.g.

<form id=myform" name="myform" method = "post" action="myhandler.php">
...
</form>

(ii) Also, form data should be passed as POST and not GET, otherwise an user could pass data using the url in the address bar, e.g.
http://mypage.php?cmbcategory=professor

If on the other hand, your form just has the select widget and you don't really any other data validated - use AJAX. This will not change the widget because the page is not reloaded. The easiest way to do this would be:

1. Download and link to the Prototype library. Also create a new js file for your ajax/js calls.

<script src="prototype.js"></script>
<script src="myAjax.js"></script>

2. Place this in the widget:

<select id="namelevel" name="namelevel" onchange="alterName();return false;">
...
</select>

3. Under your form, place this "placeholder":

<div id="nametext"></div>

3. In your myAjax.js file:

function alterName(){
  var nameLevel = $F('namelevel'); //this retrieves the value of the option selected
  var url = changelevelname.php; //the new php file holding the sql
  var oOptions = {method:post,parameters: 'id=' + nameLevel};
  var oAjax = new Ajax.Updater('nametext',url,oOptions); //this replaces the div called 'nametext' with the output from changelevelname.php
}

4. In the new changelevelname.php file:

$id = addslashes(htmlentities($_POST['id']));
$sql = "SELECT name FROM categories WHERE id = '{$id}'";  
$rs = mysql_query($sql);

echo "You are selected: {$rs['name']}";

Oh Darn! Just realised, do you just want to show the name as separate text? If so, forget Ajax, just do this:

<select id="changename" onchange="repeatMe();return false;">
...
</select>
...
<p id="output"></p>

In javascript (in a file or as part of a script tag):

function repeatMe(){
  var x=document.getElementById("changename");
  document.getElementById('output').innerHTML = "You selected: " + x.options[x.selectedIndex].text;
}

hi kanaku, i tried your code but no category is displayed ....

Try this (I removed the size="1" attribute in your select tag).

<form>
<table border="0" width="100%">
<tr>
<td align="right" width="60%">
<select name="cmbcategory" onChange="submit();">
<option value="0">Select <?php if (isset($_GET['cmbcategory'])) {echo $_GET['cmbcategory'];} else echo "category"; ?></option>
<? $sql1=mysql_query("select name from categories") or die(mysql_error());
  while($row=mysql_fetch_array($sql1))
 {
	$category = $row['name'];
?>
 <option value="<?php echo $category; ?>"><?php echo  $category; ?></option>

 <?     
     }
  ?>
  </select>
   <?     
	    echo "<p>You are selected: '".$_GET['cmbcategory']."'</p>\n"; 
  ?>
 </td>		
</tr>
</table>
</form>

OR if that doesn't work: Can you post the whole code instead?

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.