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

时间:2014-03-24 14:19:38

标签: javascript php jquery json

情况就是这样......在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

1 个答案:

答案 0 :(得分: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);

}