2013-09-22 36 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