使用带有添加变量的 Javascript 提交表单
我有一个 HTML 表单,它通过两个触发 JavaScript 代码的不同链接提交到 PHP 脚本。我需要的是能够通过两个 JavaScript 链接以不同的方式提交表单,以便我可以检查 PHP 端的差异。我意识到我可以使用提交按钮来做到这一点,但我试图避免这种情况。我希望这样的东西能够工作:
foo.html
<form name="fooform" action="fooscript.php" method="POST">
<input type="text" name="footext">
</form>
<a onclick='document.fooform.submit();' href='#'>Do something</a>
<a onclick='document.fooform.submit();' href='#'>Do something else</a>
<!--
<a onclick='document.fooform.submit(0);' href='#'>Do something</a>
<a onclick='document.fooform.submit(1);' href='#'>Do something else</a>
-->
fooscript.php
<?php
if(submit(0)){
//Do something
}
if(submit(1)){
//Do something else
}
?>
I have an HTML form that submits to a PHP script through two different links that fire JavaScript code. What I need is to be able to submit the form differently through the two JavaScript links so that I can check for that difference on the PHP side. I realize I could do this with submit buttons, but I'm trying to avoid that. I was hoping something like this would work:
foo.html
<form name="fooform" action="fooscript.php" method="POST">
<input type="text" name="footext">
</form>
<a onclick='document.fooform.submit();' href='#'>Do something</a>
<a onclick='document.fooform.submit();' href='#'>Do something else</a>
<!--
<a onclick='document.fooform.submit(0);' href='#'>Do something</a>
<a onclick='document.fooform.submit(1);' href='#'>Do something else</a>
-->
fooscript.php
<?php
if(submit(0)){
//Do something
}
if(submit(1)){
//Do something else
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会在表单中创建一个隐藏字段。
在脚本标记中提取提交表单的详细信息,
然后在提交表单之前添加值。
I would create a hidden field in the form.
extract the details of the submitting the form in a script tag
then add the value before submitting the form.
PHP:
PHP: