如何将PHP值传递给Javascript并重定向到其他页面?
为什么我不能将PHP代码中的值传递给按钮上的OnClick函数和javascript函数depCheck2()?我想重定向到2个不同的页面取决于它携带的价值。谁能帮帮我吗如何将PHP值传递给Javascript并重定向到其他页面?,javascript,php,html,mysql,Javascript,Php,Html,Mysql,为什么我不能将PHP代码中的值传递给按钮上的OnClick函数和javascript函数depCheck2()?我想重定向到2个不同的页面取决于它携带的价值。谁能帮帮我吗 <html> <head> <?php $company_id = $_GET['cont']; $query = "SELECT count(Department_ID) as countDep FROM department WHERE Company_ID=$company_id"; $r
<html>
<head>
<?php
$company_id = $_GET['cont'];
$query = "SELECT count(Department_ID) as countDep FROM department WHERE Company_ID=$company_id";
$result = mysql_query($query, $db) or die(mysql_error($db));
$row = mysql_fetch_array($result);
extract($row);
?>
<script>
function depCheck2(x){
var CountRow = '<?php echo "$countDep";?>';
if (CountRow>0){
window.location.assign("editEvent.php?id="+x"");
}
else{
window.location.assign("editEvent.php2?id="+x"");
}
}
</script>
</head>
<body>
<?php
include("config.php");
$company_id = $_GET['cont'];
$query = "select * from event_details where Company_ID=".$company_id." ORDER BY EventDetails_ID DESC";
$result=mysql_query($query, $db) or die(mysql_error($db));
echo "<table border=1 width='1000'>";
echo "<tr><th>Serial Number</th><th>Status</th><th>Event Type</th><th>Date of Event</th><th>End Date</th><th>Details</th></tr>";
while ($row = mysql_fetch_array($result))
{
extract($row);
echo "
<tr>
<td align='center'>$EventDetails_ID</td>
<td align='center'>$Status</td>
<td align='center'>$EventType</td>
<td align='center'>$StartDate</td>
<td align='center'>$EndDate</td>
<td align='center'>
<input type='button' value='Details' class='btn btn-large btn-primary' onClick='depCheck2(\''.$EventDetails_ID.'\')'>
</td>
</tr>
";
}
?>
</table>
</body>
</html>
函数depCheck2(x){
var CountRow='';
如果(CountRow>0){
window.location.assign(“editEvent.php?id=“+x”);
}
否则{
window.location.assign(“editEvent.php2?id=“+x”);
}
}
首先:
包含PHP中未定义的$countDep
它仅出现在DB查询中,但您尚未将其分配给$result
然后window.location.assign(“editEvent.php?id=“+x”)代码>语法错误
你在结尾有额外的引号
它应该是window.location.assign(“editEvent.php?id=“+x”)代码>试试这个:
window.location.href ="editEvent.php2?id="+x;
您的PHP中没有$countDep
变量?它来自选择count(Department_ID)作为countDep我相信您的'onClick'应该是'onClick',您在window.location.assign(“editEvent.PHP?ID=“+x”)中有错误代码>将其替换为window.location.assign(“editEvent.php?id=“+x”)代码>哈哈,谢谢兄弟。成功了。在这方面我真是个傻瓜:)甚至可以使用window.location.assign(“”)。只是用户在结尾有额外的引号。此外,变量$countDep没有在任何地方定义