2014-03-24 83 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