我可以用href格式(ahref ='...'
)制作此链接吗?
<form action='add_to_card.php' method="POST">
<input type='hidden' name='id' value='<?php echo $id?>' />
<input type='submit' value='Add' />
</form>
我试过
echo "<a href='add_to_card.php?id=$id'>Add</a>";
但它不起作用。
答案 0 :(得分:3)
<a href="add_to_cart.php?id=<?php echo $id ?>" > Add </a>
确保您的表单使用GET来解析变量。
作为POST ......
<form name="form1" action='add_to_card.php' method="POST">
<input type='hidden' name='id' value='<?php echo $id?>' />
<a href="##" onClick="fn_submit();" >Submit</a>
</form>
<script>
function fn_submit()
{
document.form1.submit();
}
</script>
通过这种方式,您可以将锚点放在任何位置并提交form1
答案 1 :(得分:1)
<input type="button" id="<?php echo $id; ?>" class='add2cart' value='Add to Cart' />
<script>
$('.add2cart').click(function(e) {
$.ajax({type: "POST", url: "/add_to_cart.php?id=" + e.currentTarget.id}).done(function( html ) {
$("body").html(html);
});
});
</script>
This is through jQuery though. The advantage is that not the whole page gets reloaded.
答案 2 :(得分:1)
您可以点击链接使用javascript提交表单:
<form name="myform" action='add_to_card.php' method="POST">
<input type='hidden' name='id' value='<?php echo $id?>' />
<a href="#" onclick="submitform()">Add</a>
</form>
<script type="text/javascript">
function submitform()
{
document.myform.submit();
}
</script>