2014-03-24 31 views
0

這是情況...在php頁面中創建了兩個結果..結果顯示爲json_encode。結果顯示完美。但是,當我插入兩個PHP代碼塊中的JavaScript代碼,然後一個結果顯示,而其他不..我真的不知道爲什麼會這樣。我的代碼PHP代碼不能在javascript之間執行

$action = isset($_GET['action']); 
if($action == "get_requests"){ 

include("../connect.php"); 


    $sql_song_req = "SELECT COUNT(*) FROM `song_requests`"; 
    $sql_select_song = "SELECT * FROM `song_requests` ORDER BY id ASC"; 

    $sql_count = $rad->prepare($sql_song_req); 
    $sql_count->execute(); 

    $count = $sql_count->fetchColumn(); 



    $select_song_prep = $rad->prepare($sql_select_song); 
    $select_song_prep->execute(); 

    while($row = $select_song_prep->fetch(PDO::FETCH_ASSOC)){ 

     $id = $row['id']; 
     $name = $row['name']; 
     $song = $row['songname']; 
     $dedicatedto = $row['dedicatedto']; 
     ?> 
     <script> 
      function delete_req(id){ 
     alert("hello"); 
      } 
     </script> 
     <?php 

     $data .= ' <tr cellpadding="5" cellspacing="6" align="center" width="60%"> 
       <td>'.$id.'</td> 
       <td>'.$name.'</td> 
       <td>'.$song.'</td> 
       <td>'.$dedicatedto.'</td> 
       <td><a href="javascript:;" onclick="delete_req('.$id.');" style="background:black; color:white; padding:8px;">Delete</a></td> 
       </tr>'; 

    } 


    $display = ' <table "cellspacing="4" align="center"> 
      <tr> 
      <th>ID</th> 
      <th>Name</th> 
      <th>Song</th> 
      <th>Dedicated to</th> 
      <th>Delete</th> 

      '.$data.'   

      </tr> 
      </table>'; 


     $response = array(); 
      $response['data_from_db'] = $display; 
     $response['count'] = $count; 
     echo json_encode($response); 

} 

這裏response['count']正顯示出在我的PHP頁面上,但不是$response['data_from_db']。 當我刪除JavaScript代碼,然後他們都顯示..需要幫助。

我應該指出,使用NGINX和PHP5-FPM

回答

0

是你有一個括號不匹配。

加了一個大括號}$dedicatedto = $row['dedicatedto'];您的while循環未正確關閉。

$action = isset($_GET['action']); 
if($action == "get_requests"){ 

include("../connect.php"); 

    $sql_song_req = "SELECT COUNT(*) FROM `song_requests`"; 
    $sql_select_song = "SELECT * FROM `song_requests` ORDER BY id ASC"; 

    $sql_count = $rad->prepare($sql_song_req); 
    $sql_count->execute(); 

    $count = $sql_count->fetchColumn(); 

    $select_song_prep = $rad->prepare($sql_select_song); 
    $select_song_prep->execute(); 

    while($row = $select_song_prep->fetch(PDO::FETCH_ASSOC)){ 

     $id = $row['id']; 
     $name = $row['name']; 
     $song = $row['songname']; 
     $dedicatedto = $row['dedicatedto']; 
     } // <- added. Brace for while loop 
     ?> 
     <script> 
      function delete_req(id){ 
     alert("hello"); 
      } 
     </script> 
     <?php 

     $data .= ' <tr cellpadding="5" cellspacing="6" align="center" width="60%"> 
       <td>'.$id.'</td> 
       <td>'.$name.'</td> 
       <td>'.$song.'</td> 
       <td>'.$dedicatedto.'</td> 
       <td><a href="javascript:;" onclick="delete_req('.$id.');" style="background:black; color:white; padding:8px;">Delete</a></td> 
       </tr>'; 

    $display = ' <table "cellspacing="4" align="center"> 
      <tr> 
      <th>ID</th> 
      <th>Name</th> 
      <th>Song</th> 
      <th>Dedicated to</th> 
      <th>Delete</th> 

      '.$data.'   

      </tr> 
      </table>'; 


     $response = array(); 
      $response['data_from_db'] = $display; 
     $response['count'] = $count; 
     echo json_encode($response); 

} 
+0

我做到了..同樣的事情,一個反應是所有我得到.. – nick

+0

JS的功能是否必須在其目前的地方?爲什麼不把它放在PHP的頂部?我認爲這就是現在的問題。這是因爲條件語句包含在所有內容中。如果條件滿足,你是否希望一切都執行,因爲現在,if($ action ==「get_requests」){'和'echo echo json_encode($ response);'被執行。 @nick你也可以迴應JS,然後完全擺脫'<?php'標籤。 –

+0

好吧,我把它放在頂部..並猜測它仍然是同樣的問題..究竟是什麼錯誤?是因爲json嗎? – nick