2013-09-22 40 views
0

我想从两个下拉列表组成的页面传递两个变量做一些计算并将第三个列表检索到div。我怎样才能使这个工作。? 这是我的代码。使用ajax和php页检索列表

<HTML> 
    <HEAD> 

     <script src="jquery-1.10.2.js"></script> 

     <script type="text/javascript"> 
      $(document).ready(function(){ 
       $("#day").change(function(){ 

         var day=$("#day").val(); 
         var doctor=$("#doctor").val(); 

         $.ajax({ 
          type:"post", 
          url:"time.php", 
          data:"day="+day+"&doctor="+doctor, 
          success:function(data){ 
          $("#testing").html(data); 
          } 

         }); 

       }); 
      }); 
     </script> 
     </HEAD> 

<BODY> 
    <FORM action="post"> 

    <SELECT id="doctor">//some options</SELECT>  
    <SELECT id="day">//some option </select> 


    <div id="testing"> 
    BLA BLA BLA 


     </div> 

    </BODY> 



</HTML> 

在time.php页我做了一些计算,以检索与位值“1”列名,结果存储到一个下降downlist

  <? 
    $con=mysqli_connect("localhost","clinic","myclinic","myclinic"); 
    // Check connection 

    if (mysqli_connect_errno()) 
    { 
     echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

    $doctor = $_POST['doctor']; 

    $day = $_POST['day']; 

    $query="SELECT * FROM schedule WHERE doctor='" .$doctor."'AND day='" .$day. "'"; 
      //Some calculations and store the result into a list 


    $result = mysqli_query($con, $query); 

    if(!$result) 
    { 
     echo "Failed to execute the query"; 
    } 

    echo" 
    <table><tr><td>&nbsp;Time&nbsp;</td> 
    <td>&nbsp;<select name='time'>"; 
    $i = 0;         //Initialize the variable which passes over the array key values 

    $row = mysqli_fetch_assoc($result); //Fetches an associative array of the row 
    $index = array_keys($row);    // Fetches an array of keys for the row. 

    while($row[$index[$i]] != NULL) 
    { 

     if($row[$index[$i]] == 1) {    
      echo $index[$i]; 
      echo "<option value='" . $index[$i]."'>" . $index[$i] . "</option>"; 
     } 
     $i++; 
    }  

    echo "</select>"; 

     ?> 
+0

你不说什么是不工作 –

+0

道歉:)。我想从日期列表中选择一个值,以便医生和日期的相应值进入time.php脚本。从数据库中检索一些数据并将其放入列表中,并将此列表'时间'显示在主页。 – Ajit

+0

你的php很容易被mysql注入 – DGS

回答

0

在这里你有三个丢弃的主页下拉列表中,您应该有一个js代码,它接收前两个列表的值并使用这些值向php服务器发送查询。

服务器上的php脚本应该将结果输出到XML文件中,这就是AJAX名称的来源。

然后,主页上的js应该检索xml文件,解析它并将结果填充到第三个列表中。

没有帖子/在这个过程中获得!

这里有一个例子

function updatehours() 
{ 
    document.getElementById('minute').disabled=true; 
    document.getElementById('hour').disabled=false; 
    // update xml at server side. 
    var head = document.getElementsByTagName('body').item(0); 
    var script = document.createElement('script'); 
    script.setAttribute('type', 'text/javascript'); 
    script.setAttribute('src', 'updatehours.php?file=92007bb48c.xml&date='+document.getElementById('datepicker').value); 
    script.deleteFromDocument; 
    head.insertBefore(script, head.firstChild); 
    setTimeout('processhours()',500); 
    setTimeout('processminutes()',1000); 
} 
function processhours() 
{ 
    var xmlhttp; 
    var txt,xx,x,i; 
    if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } else {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
     txt=""; 
     x=xmlhttp.responseXML.documentElement.getElementsByTagName("H"); 
     for (i=0;i<x.length;i++) 
     { 
     try 
     { 
      txt=txt + "<option value=\"" + x[i].firstChild.nodeValue + "\">" + x[i].firstChild.nodeValue + "</option>"; 
     } 
     catch (er) 
     { 
     } 
     } 
    } 
    document.getElementById('hour').innerHTML=txt; 
    } 
    xmlhttp.open("GET","hours/92007bb48c.xml",true); 
    xmlhttp.send(); 
} 

注意,updatehours.php是在计算并把结果在XML文件中的服务器脚本。

+0

的发布请求,他正在使用jquery,不需要直接使用xhr,因为他使用的是jquery ajax方法,他也不需要将响应放在xml中,他只需输出html和将它直接添加到dom中,不需要进行解析或任何操作。你也提到_“没有帖子/在这个过程中获得!!”但是所有的xhr请求都是获取或发布,例如你正在做一个GET –

+0

aha,不知道关于jquery,好的提示! – Aus