关于单个表单上多个提交按钮的 PHP 代码


PHP code regarding multiple submit button on single form

>我的索引页上有两个提交按钮,即国际和国内。 我希望两个不同的按钮指向不同的页面,即int.php和dom.php当我单击按钮时。 你能帮帮我吗? 谢谢

而只允许为表单元素定义单个action = ""。 但是如果我必须这样做,我会这样做。

<form action ="somepage.php" method="post">
    <!--all your input elements-->
    <input type="submit" name ="international" value="international"/>
    <input type="submit" name ="domestic" value="domestic"/>
</form>

确定单击了哪个按钮并采取相应措施。

if(isset($_POST['domestic']) {
    //include dom.php
}
else if(isset($_POST['international']) {
    //include  int.php
}

然后,您可以包含必要的文件。

或者另一种方式是顺AJAX/jQuery

你可以在php中使用switch来区分或使用JavaScript

用 jquery 来做!首先,不要创建提交按钮,只是创建

<input type="button" />

然后给他们一个id,比如:

<input type="button" id="InternationalBTN" />
<input type="button" id="DomesticBTN" />

并与 jquery 绑定操作

$(document).ready(function(){
    $("#InternationalBTN").bind('click',function(){
        $('#idOfYourForm').attr('action','setTheDestinationPageHere');
        document.forms['nameOfYourForm'].submit();
    });
});

这是不可能的,因为表单的操作属性一次只能指向一个位置,并且两个按钮都位于同一表单中(但如果使用 AJAX,则可能)。

如果你想使用纯PHP(即不涉及Javascript),你必须为不同的按钮点击编写两个不同的处理程序,如下所示:

if(isset($_POST["name_of_international_button"])){
    //actions to perform for this -- 
}
if(isset($_POST["name_of_domestic_button"])){
    //action to perform for this
}

然后,在每个处理程序的操作部分中,您可以执行重定向,其中 URL 包含要在int.phpdom.php脚本中处理的数据

你可以这样做:

在表单标签中,请留下空的操作action=""

2个按钮发送:

<input class="btnSend" type="submit" name="International" value="International" id="International"/>
<input class="btnSend" type="submit" name="Domestic" value="Domestic" id="Domestic"/>

并使用 ajax:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
  $(document).ready(function(){
    $('#International ').click(function() {
      // send to file 1 using ajax
    });
    $('#Domestic').click(function() {
      // send to file 2 using ajax
    });
  });
</script>

以下是使用 ajax 发送数据的方法:
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/

您的表单操作必须包含某种条件语句,以根据单击哪个提交按钮重定向用户

<form action="<?php if(isset($_POST['international'])){echo 'international.php';}else{echo 'domestic.php';}?>" method="post">
  <input type="text" name="field1" />
  <input type="text" name="field2"/>
  <input type="submit" value="international "name="international"/>
  <input type="submit" value="domestic "name="domestic"/>
</form>

或者,您可以在表单操作指定的页面上设置条件,并根据单击的按钮重定向它们,

只需放置一个表单标签,然后将操作设置为页面。然后提交按钮将导航到操作标记指向的页面...

就这么简单:D