Akash Gupta
Akash Gupta

Reputation: 318

assign a form element to more than one form

I have this small form with two submit buttons, on clicking each button I want form data to be sent to different PHP file. But, both submit buttons are sending the form data to the default PHP file specified in the action attribute of the form tag. Is there a way to send the form data to different files with different buttons. I made an unsuccessful attempt to do so by redefining the action attribute in the second submit button

                <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>

Upvotes: 1

Views: 146

Answers (3)

Akash Gupta
Akash Gupta

Reputation: 318

finally did it with the help of this code. source: 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>

Upvotes: 1

Jayyrus
Jayyrus

Reputation: 13051

just use jQuery:

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

});

Upvotes: 0

Zevi Sternlicht
Zevi Sternlicht

Reputation: 5399

Two separate forms wont work, the page will submit in between. You need Ajax to send the data behind the scene without refreshing the page

Upvotes: 2

Related Questions