1

我有这个带有两个提交按钮的小表单,单击每个按钮时,我希望将表单数据发送到不同的 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>
4

3 回答 3

2

两个单独的表单不起作用,页面将在两者之间提交。需要Ajax后台发送数据而不刷新页面

于 2013-07-13T20:16:22.937 回答
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>
于 2013-07-13T20:39:33.540 回答
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();
   });

});
于 2013-07-13T20:17:14.007 回答