how to put values from database into combobox

Discussion in 'Programming/Scripts' started by abc, Nov 24, 2010.

  1. abc

    abc New Member

    I am using php and i have made two combo boxes.Actually i want that whenever user select any value from combo box1 named subject the values from database are automatically displayed in the combobox2 named supervisor.But whenever i run the code it gives error as "undefined index subject" and values are not retrieved in the second combobox.here is the code
    Code:
    <form method="post" action="" name="form1">
    
    <table width="60%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="150"></td>
        <td  width="150">
    	  <p>
    	    <select name="subject" >
    	        <option value="">Select Subject</option>
    	        <option value="1">Networking</option>
    	        <option value="2">Databases</option>
    	        <option value="3">Web development</option>
    	        <option value="4">digital signal processing</option>
    	      </select>
    		  
    		  
    	  </p>
    	</td>
      </tr>
    
      </table>
      
      <?php
    include("conpage.php");
    $s1= intval($_POST['subject']); //here it gives error as subject to be        undefined
    mysql_select_db('thesis');
    $query="SELECT id,supervisorname FROM supervisors WHERE subjectid='$s1'";   //supervisors table from where values in the second combo box have to be displayed
    $result=mysql_query($query);
    
    ?>
    
    
    <select name="state">
    <option>Select Supervisor</option>
    <? while($row=mysql_fetch_array($result)) { ?>
    <option value=<?=$row['id']?>><?=$row['supervisorname']?></option>
    <? } ?>
    </select>
    </form>
    
    i donot know how to solve this?
     
  2. falko

    falko Super Moderator Howtoforge Staff

    Please add
    Code:
    print_r($_POST);
    to your code to see how the $_POST array looks:

    Code:
    <form method="post" action="" name="form1">
    
    <table width="60%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="150"></td>
        <td  width="150">
    	  <p>
    	    <select name="subject" >
    	        <option value="">Select Subject</option>
    	        <option value="1">Networking</option>
    	        <option value="2">Databases</option>
    	        <option value="3">Web development</option>
    	        <option value="4">digital signal processing</option>
    	      </select>
    		  
    		  
    	  </p>
    	</td>
      </tr>
    
      </table>
      
      <?php
    include("conpage.php");
    [COLOR="Red"]print_r($_POST);[/COLOR]
    $s1= intval($_POST['subject']); //here it gives error as subject to be        undefined
    mysql_select_db('thesis');
    $query="SELECT id,supervisorname FROM supervisors WHERE subjectid='$s1'";   //supervisors table from where values in the second combo box have to be displayed
    $result=mysql_query($query);
    
    ?>
     

Share This Page