将表单元素分配给多个表单

时间:2013-07-13 20:13:22

标签: html forms

我有这个带有两个提交按钮的小表单,点击每个按钮我想将表单数据发送到不同的PHP文件。但是,两个提交按钮都将表单数据发送到表单标记的action属性中指定的默认PHP文件。有没有办法将表单数据发送到具有不同按钮的不同文件。我通过在第二个提交按钮

中重新定义action属性来尝试这样做是不成功的
                <div class="dropdown dropdown-dark">
                    <select name="promoteyearselect1" id="promoteyearselect1" class="dropdown-select"  onfocus="showhidephdmenu()" form="promotionform" required>
                        <option value="">Select an option</option>
                            <div id="yearselect1">
                                <option value="1">1st</option>
                                <option value="2">2nd</option>
                                <option value="3">3rd</option>
                                <option value="4">4th</option>
                                <option value="5">5th</option>
                            </div>  
                        </option>
                    </select>
                </div> 


                    <select name="promotesemselect1" id="promotesemselect1" class="dropdown-select" form="promotionform" required>
                        <option value="">Select an option</option>
                        <option value="1">1st</option>
                        <option value="2">2nd</option>
                        <option value="3">3rd</option>
                        <option value="4">4th</option>
                        <option value="5">5th</option>
                        <option value="6">6th</option>
                        <option value="7">7th</option>
                        <option value="8">8th</option>
                        <option value="9">9th</option>
                        <option value="10">10th</option>


                    </select>





<form id="promotionform" action="promotestudents.php" method="POST">


                <div style=" position:relative; margin-top:10px; padding-left:44%;">  
                    <input type="submit" value="Promoted" class="button black" />
                    <input type="submit" value="Passed Out" class="button black" form="promotionform" action="alumni.php"/>
                </div>

            </form>

3 个答案:

答案 0 :(得分:2)

两个单独的表单不起作用,页面将在两者之间提交。您需要Ajax才能在不刷新页面的情况下发送场景后的数据

答案 1 :(得分:1)

最后在这段代码的帮助下完成了它。 来源:http://blog.theonlytutorials.com/multiple-submit-button-in-a-single-form-with-php/

 <?php

    if(isset($_POST['submit1']))
    {
    echo "You hit the button 1";
    }
    if(isset($_POST['submit2']))
    {
    echo "You hit the button 2";
    }

    ?>

<html>
<head><title>Multiple Submit button Solved with PHP!</title></head>
<body>
<form action="" method="post">

<h2> Hit the Submit Button</h2>

<input type="submit" name="submit1" value="btn1" />
<input type="submit" name="submit2" value="btn2" />

</form>
</body>

答案 2 :(得分:0)

只需使用jQuery

$(document).ready(function(){
   $('#button1').click(function(){
      $('#yourForm').attr("action","phpFile1.php");
      $('#yourForm').submit();
   });
   $('#button2').click(function(){
      $('#yourForm').attr("action","phpFile2.php");
      $('#yourForm').submit();
   });

});