简体   繁体   English

当javascript之间出现PHP代码未执行

[英]PHP code not executing when javascript comes between

Here is the situation... Two results are created in the php page.. The results are echoed as json_encode . 这是一种情况...在php页面中创建了两个结果。.结果以json_encode The results are showing perfectly. 结果显示完美。 But when i insert a javascript code within two php code blocks, then one result is shown while the other is not.. I really have no idea why this is happening.. My code 但是,当我在两个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);

}   

Here the response['count'] is showing on my php page but not $response['data_from_db'] . 这里的response['count']显示在我的php页面上,但是没有显示$response['data_from_db'] And when I delete the javascript code then both of them are showing.. Help needed. 当我删除JavaScript代码时,它们都将显示出来。

I should mention that am using NGINX and php5-fpm 我应该提到正在使用NGINX和php5-fpm

You have a brace mismatch. 您的支架不匹配。

Add a brace } after $dedicatedto = $row['dedicatedto']; $dedicatedto = $row['dedicatedto'];之后添加大括号} $dedicatedto = $row['dedicatedto']; Your while loop wasn't properly closed. 您的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);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM