当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);
}
相关文章:
- php代码在textbox更改事件上显示null
- 无法运行php代码,有角度路由问题
- jquery中的PHP代码不起作用
- 获取PHP代码中日期选择器的值
- PHP代码古埃及乘法javascript
- PHP代码中实现的JavaScript |if语句不起作用
- 如何将javascript变量传递到Php代码中.
- 在按钮'上运行PHP代码;s onclick事件,或者使用Ajax
- 停止直接执行php代码而不点击按钮
- php代码或脚本接受自动完成列表中文本框中的值
- 无法使autocompletion与bootstrap和php代码点火器一起工作
- 添加 PHP 代码的 Javascript 下拉框
- 在 php 代码中实现倒计时 js 倒计时
- AJAX 中的 PHP 代码到 POST
- 用于修改文档元素的 PHP 代码
- 链接适用于所有浏览器,除了 safari(php 代码)
- PHP 代码中的 CSS 弄乱了 IE9 中的布局
- Javascript的“document.write()”中的PHP代码
- JavaScript 函数在 php 代码中的调用中不起作用
- JavaScript 只在 php 代码中执行一次