[英]using a href (html)tag along with PHP
i have tried: 我努力了:
<?php include("delete.php") ?>
<?php
....
....
....
if($result=mysql_query($sql))
{
echo "<table><th>Id</th><th>Name</th><th>Description</th><th>Unit Price</th>";
while($row = mysql_fetch_array($result))
{
echo "<tr><td>".$row['Id']."</td><td>".$row['Name']."</td><td>".$row['Description']."</td><td>".$row['UnitPrice']."</td>
<td><a href='delproduct($row[Id])' onclick = 'return MsgOkCancel()'>Delete</a></td></tr>";
echo "<br/>";
}
}
?>
following javascript is in the same page: 以下javascript在同一页面中:
<script type="text/javascript" language="javascript">
function MsgOkCancel() {
if (confirm("Are You Sure You Want to Delete?"))
{ return true }
else
{return false}
}
</script>
where delproduct is a javascript function in delete.php written like: 其中delproduct是delete.php中的javascript函数,写为:
<script type="javascript">
function delproduct(Id)
{
alert('Id '+ Id);
}
<script>
** after ** clicking Delete a okcancel message-box appear asking conformation ** 在 **单击删除okcancel消息框后出现**询问构象
** but ** after clicking 'ok' it should execute statements inside delproduct function but it doesn't **, 但 **单击“确定”后,它应在delproduct函数中执行语句,但不会
it gives error like: 它给出如下错误:
Object Not Found :The requested URL was not found on this server. 找不到对象:在此服务器上找不到请求的URL。
what would be the problem? 有什么问题吗?
pls help, 请帮助,
thanks 谢谢
A URI without a scheme (such as http:
) is treated as a relative URI. 没有方案的URI(例如
http:
被视为相对URI。
You appear to be looking for javascript:
(which should never be used for anything other than creating bookmarklets). 您似乎正在寻找
javascript:
除创建书签外,请勿将其用于其他任何用途)。
What you should be doing is something along the lines of: 您应该做的事情大致如下:
onclick="if (MsgOkCancel()) { delproduct($row[Id]); return false; } else { return false; }"
However, you should have something that works in the href, but since this appears to be making a significant change on the server, you should be using POST not GET, so a link is the wrong tool. 但是,您应该有一些可以在href 中起作用的东西 ,但是由于这似乎对服务器进行了重大更改,因此您应该使用POST而不是GET,因此链接是错误的工具。
What you probably should have is: 您可能应该拥有的是:
<form action="/delete" method="post" onsubmit="return delete(this);">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($row[Id]); ?>">
<input type="submit" value="Delete">
</form>
Combined with: 结合:
function delete(form) {
if (confirm("Are You Sure You Want to Delete?")) {
delproduct(form.elements.id.value);
}
return false;
}
Better yet, get rid of the onsubmit
attribute and assign the event using JavaScript. 更好的是,摆脱
onsubmit
属性,并使用JavaScript分配事件。
I think you need a different setup. 我认为您需要其他设置。
First of all, if you are going to call javascript functions in an href attribute, you need to prepend it with javascript:
like so href="javascript:delproduct(...)"
. 首先,如果要在href属性中调用javascript函数,则需要在其前面加上
javascript:
如so href="javascript:delproduct(...)"
。 But calling javascript from an href attribute is not recommended. 但是不建议从href属性调用javascript。 That attribute is intended for urls.
该属性用于URL。
I would advice you to create a function that displays the messagebox and based on the action of the user, calls the delproduct function. 我建议您创建一个显示消息框的函数,并根据用户的操作调用delproduct函数。 Something like:
就像是:
function confirmDelProduct( id )
{
if( msgOkCancel() )
{
delproduct( id );
}
// return false is meant to stop the href url from being called
return false;
}
And in your html: 并在您的html中:
<a href="#" onclick="return confirmDelProduct(' . $row[ 'id' ] . ')"> ... etc
What about this one: PHP: 那这个呢:PHP:
<a href="javascript:void(0);" onclick=\"delproduct({$row[Id]})\">
JS: JS:
function delproduct(Id){
if(MsgOkCancel()) alert('Id '+ Id);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.