简体   繁体   中英

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 . 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

$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'] . And when I delete the javascript code then both of them are showing.. Help needed.

I should mention that am using NGINX and php5-fpm

You have a brace mismatch.

Add a brace } after $dedicatedto = $row['dedicatedto']; Your while loop wasn't properly closed.

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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