使用 php 单击编辑按钮时出现以下错误。
Error:
Uncaught ReferenceError: edt_id is not defined
我在下面解释我的代码。
index.php:
<?php
include_once 'dbconfig.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CRUD Operations With PHP and MySql - By Cleartuts</title>
<link rel="stylesheet" href="css/style.css" type="text/css" />
</head>
<body>
<center>
<div id="body">
<div id="content">
<table align="center">
<tr>
<th colspan="5"><a href="add_data.html">add data here.</a></th>
</tr>
<th>First Name</th>
<th>Last Name</th>
<th>City Name</th>
<th colspan="2">Operations</th>
</tr>
<?php
$sql_query="SELECT * FROM users";
$result_set=mysql_query($sql_query);
while($row=mysql_fetch_row($result_set))
{
?>
<tr>
<td><?php echo $row[1]; ?></td>
<td><?php echo $row[2]; ?></td>
<td><?php echo $row[3]; ?></td>
<td align="center"><a href="javascript:edt_id('<?php echo $row[0]; ?>')"><img src="images/pencil_small.png" align="EDIT" /></a></td>
<td align="center"><a href="javascript:delete_id('<?php echo $row[0]; ?>')"><img src="images/cross-small-icon.png" align="DELETE" /></a></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</center>
</body>
</html>
请帮我解决此错误。当用户单击编辑按钮时,用户将重定向到 edit_data.php
页面,其中包含 edt_id
和 URL。请帮助我。
最佳答案
您的代码中存在一些问题:
- 您收到的错误是 javascript 错误,而不是 PHP 错误(事实上,您正在浏览器开发控制台中阅读错误)
- 代码中的链接执行 JavaScript 方法,因此语法为
javascript:methodname()
如果您只想创建指向另一个 PHP 文件的链接,您可以使用不带 JavaScript 代码的纯 HTML 链接:
<td align="center"><a href="edit_data.php?edt_id=<?php echo urlencode($row[0]) ?>"><img src="images/pencil_small.png" align="EDIT" /></a></td>
<td align="center"><a href="edit_data.php?delete_id=<?php echo urlencode($row[0]) ?>"><img src="images/cross-small-icon.png" align="DELETE" /></a></td>
根据需要在 href
属性中更改路径,在不知道您的应用程序的情况下,我只能猜测。需要 PHP 调用 urlencode()
来转义数据并避免注入(inject) ( docs )。
关于php - 获取 Uncaught ReferenceError : using PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32666670/