当javascript介于
情况是这样的。。。在php页面中创建了两个结果。。结果以当javascript介于,javascript,php,jquery,json,Javascript,Php,Jquery,Json,情况是这样的。。。在php页面中创建了两个结果。。结果以json\u encode的形式显示。结果显示得很好。但是,当我在两个php代码块中插入一个javascript代码时,会显示一个结果,而另一个则不会。。我真的不知道为什么会这样。。我的代码 $action = isset($_GET['action']); if($action == "get_requests"){ include("../connect.php"); $sql_song_req = "SELECT COU
json\u 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);
}
$action=isset($\u GET['action']);
如果($action==“获取请求”){
包括(“../connect.php”);
$sql_song_req=“从'song_requests'”中选择COUNT(*);
$sql\u select\u song=“从`song\u requests`按id订购ASC中选择*;
$sql\u count=$rad->prepare($sql\u song\u req);
$sql_count->execute();
$count=$sql\u count->fetchColumn();
$select\u song\u prep=$rad->prepare($sql\u select\u song);
$select_song_prep->execute();
而($row=$select_song_prep->fetch(PDO::fetch_ASSOC)){
$id=$row['id'];
$name=$row['name'];
$song=$row['songname'];
$dicatedto=$row['dicatedto'];
?>
功能删除请求(id){
警惕(“你好”);
}
你有一个括号不匹配
在$indivatedto=$row['indivatedto'];
循环未正确关闭时,在之后添加一个大括号
$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);
}
$action=isset($\u GET['action']);
如果($action==“获取请求”){
包括(“../connect.php”);
$sql_song_req=“从'song_requests'”中选择COUNT(*);
$sql\u select\u song=“从`song\u requests`按id订购ASC中选择*;
$sql\u count=$rad->prepare($sql\u song\u req);
$sql_count->execute();
$count=$sql\u count->fetchColumn();
$select\u song\u prep=$rad->prepare($sql\u select\u song);
$select_song_prep->execute();
而($row=$select_song_prep->fetch(PDO::fetch_ASSOC)){
$id=$row['id'];
$name=$row['name'];
$song=$row['songname'];
$dicatedto=$row['dicatedto'];
} //
功能删除请求(id){
警惕(“你好”);
}
JS函数必须在它现在的位置吗?为什么不把它放在PHP上面呢?我想这就是问题所在。这是因为条件语句包装在所有内容中。如果条件满足,你希望所有内容都执行吗?因为现在,IF($action==“get\u requests”)中的所有内容{
和右下至echo json_encode($response)“Nek你也可以对JS进行回音,然后去掉一个<代码> OK,我把它放在上面……猜猜还有什么问题?什么是错的?是因为JSON吗?不,最有可能是因为你在PHP中进出,JS正好在PHP之外,而条件语句是包装所有内容,使其无效并造成严重破坏/问题。@nick已经给出了这样的信息。@nickI不明白为什么它会这样做,因为JS所做的一切都是在单击时调用函数delete_req()
,我觉得这确实很奇怪。@nick