2016-06-22 55 views
0

我試圖選擇每個教師的日程安排,以便在每次選擇下拉列表時顯示在完整日曆上。完整日曆事件的pdo查詢不會觸發?

這裏是我寫的jQuery的:

<script> 
     $(document).ready(function() { 
     $("#teachermale").change(function(){ 
        var str = ""; 
        $("#teachermale option:selected").each(function(){ 
         str += $(this).text() + ""; 
        }); 
       //$("#val").text(str); 
        console.log(str); 
       changeDisplay(str); 

       }).change(); 

     }); 
     function changeDisplay(str){ 
     console.log(str); 
     $.ajax({ 
       url:'find_event.php', 
       type:'POST', 
       data : '&str='+encodeURIComponent(str), 
       dataType:'text', 
       success: function(json) { 
        alert('The string has been passed'); 
       }, 
       error: function(xhr, textStatus, errorThrown) { 
        alert(xhr.responseText); 
       } 
       }); 

     } 
     </script>  

HTML的選擇框:

 <select id="teachermale" name="teachermale"> 
     <option selected disabled>Male Teachers</option> 
     <option value="Mohamed Adil">Mohamed Adil</option> 
     <option value="Sherif Reda">Sherif Reda</option> 
     <option value="Mohamed Shahban">Mohamed Shahban</option> 
     <option value="Abdullah al Haiti">Abdullah al Haiti</option> 
     <option value="Salah">Salah</option> 
     <option value="Ahmed Nabil">Ahmed Nabil</option> 
     <option value="Abdul Tawab">Abdul Tawab</option> 
     <option value="Mahmoud Mahmoud">Mahmoud Mahmoud</option> 
     <option value="Ahmed Ghanim">Ahmed Ghanim</option> 
     </select> 

,這裏是爲find_event.php

 <?php 
      // List of events 
      $json_array["title"]=$_POST["str"]; 
      $title =$json_array["title"]; 

      // connection to the database 
      try{ 

        $pdo = new PDO('mysql:host=localhost;dbname=******', '*****', '*******'); 
       } catch(Exception $e) { 

       exit('Unable to connect to database.'); 
      } 

      // Query that retrieves events 
      $requete = "SELECT * FROM `evenement` WHERE `title`=?"; 
      // Execute the query 
      $resultat = $pdo->query($requete) or die(print_r($pdo->errorInfo())); 
      // sending the encoded result to success page 
      $resultat->execute($title); 

      $events = array(); 
      $dbEvents = $resultat->fetchAll(PDO::FETCH_ASSOC); 

       foreach ($dbEvents as $event) { 
         $event['allDay'] = false; 
         $events[] = $event; 
       } 


      echo json_encode($events); 

      ?> 

的PHP的由於選擇jquery函數中的console.log語句,控制檯會將教師的名稱寫入兩次e函數用於changeDisplay()。但是,似乎查詢不執行。有人可以幫助我嗎?

+0

據我所知'執行()'預計數組作爲參數 –

回答

0

首先,您正在混合功能 - queryexecute這兩者都執行相同的操作 - 執行查詢。在這裏您使用?佔位符,但不要爲其設置值。

所以,你的正確的代碼應該是:

// Query that retrieves events 
$requete = "SELECT * FROM `evenement` WHERE `title`=?"; 
// Prepare your query 
$stmt = $pdo->prepare($requete); 
if ($stmt) { 
    // argument of execute is array, not single value 
    if ($stmt->execute(array($title))) { 
     $events = array(); 
     $dbEvents = $stmt->fetchAll(PDO::FETCH_ASSOC); 
     // more codes 
     // ... 
    } else { 
     // check for error 
     print_r($stmt->errorInfo()); 
    } 
}