【问题标题】:Process form data using PHP_SELF second button to post form data to another page使用 PHP_SELF 第二个按钮处理表单数据以将表单数据发布到另一个页面
【发布时间】:2013-09-16 00:37:36
【问题描述】:

我有一个表单,它使用以下方式发布数据: " method="post" enctype="multipart/form-data">

已发布的数据执行了一些计算,并将结果反馈到表单字段。提交按钮称为计算。 if(isset($_POST['Calculate']))

这很好用。但是,我添加了另一个按钮,称为打印,我想用它来发布数据以在另一个页面上使用。 if(isset($_POST['Print']))

有没有一种方法可以做到这一点?单击打印按钮时,基本上将表单操作从 PHP_SELF 更改为 newpage.php。

谢谢。

【问题讨论】:

  • 顺便说一下,enctype="multipart/form-data" 主要用于文件附件/上传,所以我怀疑你是否需要这个,除非这是你在表单中提供的另一件事。
  • 我会使用 jQuery 和 AJAX 而不是严格的 PHP。
  • 你不能在服务器端做,改用javascript

标签: javascript php forms


【解决方案1】:

有一个使用 JQuery 发布的解决方案:

Jquery / Javascript change form action based on input field

除了使用纯 Javascript 之外,我可能会做一些非常相似的事情:

<script type="text/javascript">
<!--
function changeformaction(newact)
{
  document.myformname.action=newact;
}
//-->
</script>

然后使用这样的东西:

<form name="myformname" action="Calculate.php" method="POST">

<input type="submit" name="Calculate" onclick="changeformaction('Calculate.php');">
<input type="submit" name="Print" onclick="changeformaction('Print.php');">
</form>

或者你可以使用这样的隐藏字段:

<form name="myformname" action="Calculate.php" method="POST" onsubmit="this.action=document.myformname.hiddenaction.value;">

<input type="submit" name="Calculate" onclick="document.myformname.hiddenaction.value='Calculate.php';">
<input type="submit" name="Print" onclick="document.myformname.hiddenaction.value='Print.php';">
<input type="hidden" name="hiddenaction" value="">
</form>

试试。

【讨论】:

  • 效果很好!感谢您的帮助!
【解决方案2】:

您能够做到这一点的唯一方法是使用 javascript(很可能是 jQuery)在单击按钮时更改表单的操作。

如果是我,我会将表单操作设置为您想要的打印按钮。然后使用 jquery/ajax 调用您的脚本(或者如果它们可以使用 javascript 完成,则在客户端执行它们并将帖子保存到服务器)以在单击该按钮时进行计算。

【讨论】:

    猜你喜欢
    • 2012-06-24
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    • 2014-10-16
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多