【问题标题】:Sending POST data from one form with 2 buttons使用 2 个按钮从一个表单发送 POST 数据
【发布时间】:2013-05-03 17:52:03
【问题描述】:

如何根据点击的发布按钮将表单中的 POST 数据发送到两个不同的页面?

我的“导出”按钮将 POST 数据发送到“export.php”,但“Graphique”按钮必须将 POST 数据发送到“graphique.php”页面。

这是我的代码:

<form name="TCAgregat" class="TCAgregat" action="export.php" method="post">

    <input type="hidden" name="daco" value="<?php echo $DConso; ?>"></input>
    <input type="hidden" name="CNS" value="<?php echo $CNewCentres; ?>"></input>

    <input type=submit border="0" class="EXP" name="exp" value="EXPORT" />  
    <input type=submit border="0" class="DCB" name="dcb" value="GRAPHIQUE" />
</form>

我怎样才能做到这一点?

【问题讨论】:

  • 把它们放在不同的表格里?
  • 或者使用带有查询字符串的javascript或acnhor标签
  • 点击按钮时更改表单操作。
  • 在提交之前,您可以使用 JavaScript 来评估按下了哪个按钮并相应地更改“操作”页面。

标签: php javascript jquery html


【解决方案1】:

action="handler.php",然后写handler.php类似的东西:

<?php
    if (isset($_POST['exp'])) {
        include('export.php');
    } elseif (isset($_POST['dcb'])) {
        include('graphique.php');
    } else {
        // Default state / error handling
    }
?>

【讨论】:

  • 只是一点。如果他想将数据发送到两个文件而不是一个文件怎么办?
  • 然后他可以将它们都包含在if 语句的同一个分支中。问题中没有任何内容表明他可能想要这样做。
  • 他谷歌可能翻译了它。但在我看来,第一行How can send the same POST data totwo different pages 似乎他正试图同时在两个不同的页面上发帖......只是说。
【解决方案2】:

点击按钮改变表单动作:

$("form").on("click", ":submit", function(e) {
    $(e.delegateTarget).attr('action', $(this).data('action'));
});

HTML:

<input type=submit data-action="export.php" value="EXPORT" />
<input type=submit data-action="graphics.php" value="GRAPHIQUE" />

http://jsfiddle.net/FAnq9/

【讨论】:

  • 就是这样,非常感谢
【解决方案3】:

使用 JavaScript 重写表单的action,然后改写submit()

【讨论】:

  • 如果你打算这样做(我不建议这样做,因为它引入了对 JS 的不必要的依赖),那么调用 submit() 是多余的。只是不要一开始就取消表单的默认操作。
  • 更多的是评论而不是答案。至少应该举个例子。
【解决方案4】:

既然你已经用 jQuery 标记了它,这里有一个解决方案:

$(input[type="submit"]).click(function(){
 var but = $(this).val();
 var action = '';
 if(but == 'EXPORT')
 {
    action = 'export.php';
 }
 else if(but == 'GRAPHIQUE')
 {
    action = 'graphique.php';
 }

 $('form').attr('action', action);

})

【讨论】:

    猜你喜欢
    • 2016-03-29
    • 2013-02-11
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多