简体   繁体   中英

One form with multiple submit actions

I have an HTML with three links and a form, and three PHP scripts: send1.php, send2.php, send3.php.

            <a id=""  name="signup1" href="#signup" >1-Send form one</a>
            <a id=""  name="signup2" href="#signup" >2-Send form two</a>
            <a id=""  name="signup3" href="#signup" >3-Send form three</a>

            <div id="signup"> 
                <form action="send_form_x.php">                     
                    <input id="" name="" type="text">
                    <button type="submit">Send</button>
                </form>
            </div> 

First the user clicks on one of the three links to show and fill the form. When he clicks on "send" button, I want that the html form call the right action, according to the clicked link. If the user clicks on "2-Send form two", send2.php must be executed, If he clicks on "3-Send form three", then send3.php must be executed. I don't want to write a form for each link, how to achieve this please?

It's hard to say without knowing what you're trying to do.

The easiest way would be to add another parameter to your form, and have the form submit to 1 php page which calls one of the other 3 pages depending on the value of that parameter.

You can either add some radio buttons to your form, or add a hidden input and change the value with javascript whenever the user clicks one of the 3 links.

Perhaps you could submit 3 times using AJAX ? Here is a simple tutorial using jQuery sending 1 form: http://vimeo.com/1392589 .

Do it 3 times :

$("form").on("submit", function() {
  $.ajax(...) // do the ajax call 1
  $.ajax(...) // do the ajax call 2
  $.ajax(...) // do the ajax call 3
})

You can use Javascript for checking which link was clicked

<a id=""  name="signup1" href="#signup" onclick="testFunction(signup1)>1-Send form one</a>
<a id=""  name="signup2" href="#signup" onclick="testFunction(signup2)>2-Send form two</a>
<a id=""  name="signup3" href="#signup" onclick="testFunction(signup3) >3-Send form three</a>
         <div id="signup"> 
            <form action="send_form_x.php">   
                <input id="clickedLink" type="hidden">
                <input id="" name="" type="text">
                <button type="submit">Send</button>
            </form>
        </div> 

//JavaScript            
function testFunction(signup){
document.getElementById("clickedLink").value = signup;
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM