使用php点击编辑按钮时出现以下错误。
错误:
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
的{{1}}页面重定向到edt_id
页面。请帮帮我。
答案 0 :(得分:0)
您的代码中存在一些问题:
javascript:methodname()
如果您只想创建指向另一个PHP文件的链接,您可以使用纯HTML链接而无需JavaScript代码:
<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()
来转义数据并避免注入(docs)。