当javascript介于两者之间时,PHP代码不执行


PHP code not executing when javascript comes between

情况如下。。。在php页面中创建了两个结果。。结果作为json_encode进行响应。结果显示得很完美。但是,当我在两个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-

大括号不匹配。

$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);
}