2014-01-25 36 views
-3

時select_dept我想顯示的醫生的名字對應的,所以我必須使用AJAX 專業在選擇專業相應的醫生的名字也將顯示 我得到錯誤像這樣和醫生列表顯示不出來我得到通知:未定義指數:使用Ajax調用

  Notice: Undefined index: select_dept in C:\wamp\www\xx\get_specialist.php 
      on line 2 
       Call Stack 
       # Time Memory Function Location 
      1 0.0012 140616 {main}() ..\get_specialist.php:0 

我的表單代碼

    <select name="select_dept" id="select_dept" 
       class="textBox" style="width:180px" onChange="getSpecialist(this.value)"> 
     <option value="">Select Department</option> 
     <?php 
     include("db_conexn.php"); 
     $res=mysql_query("SELECT * FROM imh_departments"); 
     while($rowCat2 = mysql_fetch_array($res)){?> 
     <option value="<?php echo $rowCat2['depart_id']?>"> 
       <?php echo $rowCat2['department_name'];?></option> 
     <?php }?> 
     </select> 

      <div id="statediv"> 
     <select name="doct" > 
     <option>Select Doctor</option> 
     </select> 
     </div> 

我的Ajax代碼

  <script language="javascript" type="text/javascript"> 

      function getXMLHTTP() { //function to return the xml http object 
     var xmlhttp=false; 
     try{ 
      xmlhttp=new XMLHttpRequest(); 
     } 
     catch(e) {  
      try{    
       xmlhttp= new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
      catch(e){ 
       try{ 
       xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
       } 
       catch(e1){ 
        xmlhttp=false; 
       } 
      } 
     } 

     return xmlhttp; 
    } 

    function getSpecialist(countryId) 
{  
    var strURL="get_specialist.php?country="+countryId; 
    var req = getXMLHTTP(); 

    if (req){ 

     req.onreadystatechange = function() { 
      if (req.readyState == 4) { 
       // only if "OK" 
       if (req.status == 200) 
      {       
        document.getElementById('statediv').innerHTML=req.responseText; 

       } else { 
       alert("Problem while using XMLHTTP:\n" + req.statusText); 
       } 
      }    
     }   
     req.open("GET", strURL, true); 
     req.send(null); 
    }  
} 

get_specialist.php代碼

 <?php 
     $country=$_REQUEST['select_dept']; 
     include("db_conexn.php"); 
     $query="SELECT doct_id,doct_name FROM imh_doctors WHERE depart_id='$country'"; 
     $result=mysql_query($query); 
     ?> 
<select name="doct" > 
<option>Select Doctor</option> 
<?php while ($row=mysql_fetch_array($result)){?> 
<option value=<?php echo $row['doct_id'];?>><?php echo $row['doct_name'];?></option> 
<?php } ?> 
</select> 

回答

0

$country=$_REQUEST['select_dept']; 

應該

if(isset($_REQUEST['country'])) { 
    $country=$_REQUEST['country']; 
// rest of db code 
} 

請注意,你的代碼是開放的SQL注入和使用mysqli和mysqli_real_escape_string代替。

+0

謝謝@Tims它工作... –

0

您分配$countryselect_dept,你可能想改變這種狀況。

$country = $_REQUEST['country']; 
相關問題