如何在没有javascript的情况下根据单击的按钮将表单提交到两个不同的页面 [英] How to submit a form to two different pages depending on the button clicked, without javascript

查看:145
本文介绍了如何在没有javascript的情况下根据单击的按钮将表单提交到两个不同的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有文本区域和2个按钮的表单,我需要其中一个提交到同一页面,另一个需要提交到另一个php文件.两个按钮都需要允许帖子引用文本区域.我该怎么做.

I have a form with a text area and 2 buttons, i need one of them to submit to the same page its on and the other to submit to another php file. Both buttons need to allow the text areas to be referenced by post. How can i do this.

例如:

<form action="" method="post">

   <textarea></textarea>

   <input type='submit' value='Preview'> //I want this to submit to the same page

   <input type='submit' value='Save'> // I want this to submit to save.php

</form>

注意:我所有的html都是由php通过不同的脚本生成的,这些脚本会根据用户的先前操作进行更改.

Note: All my html is generated by php through different scripts that change depending on users previous actions.

推荐答案

<?php

if (isset($_POST['action1']) || isset($_POST['action2'])) {
    // handle textarea

    if (isset($_POST['action1'])) {
        header('Location: /action1.php');
        exit();
    }

    header('Location: /action2.php');
    exit();
}

?>

<form>
  <fieldset>
    <textarea name="text"></textarea>
    <input type="submit" name="action1" value="Action1">
    <input type="submit" name="action2" value="Action2">
  </fieldset>
</form>

您只需要谨慎,当用户按下Enter键提交表单时会发生什么. IE.什么提交将被触发.

You only have to be cautious about what happens when the user presses the enter key to submit the form. I.e. what submit will be triggered.

这篇关于如何在没有javascript的情况下根据单击的按钮将表单提交到两个不同的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆