我收到通知:未定义索引:select_dept使用ajax调用时

时间:2014-01-25 08:38:37

标签: php 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>

2 个答案:

答案 0 :(得分:0)

您要将$country分配给select_dept,您可能想要更改它。

$country = $_REQUEST['country'];

答案 1 :(得分:0)

你的

$country=$_REQUEST['select_dept'];

应该是

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

请注意,您的代码对SQL注入是开放的,应该使用mysqli和mysqli_real_escape_string代替。