nash
nash

Reputation: 11

PHP code regarding multiple submit button on single form

i have two submit button on my index page namely International and Domestic. i want that two different button to point to different pages namely int.php and dom.php when i click on the buttons. can you help me out. thank

Upvotes: 1

Views: 1658

Answers (7)

Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25745

while it is allowed only to define single action = "" for form element. but if i have to do that, i would do it this way.

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

determine which button have been clicked and act accordingly.

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

}

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

and then you can include the necessary file.

or the other way is to go with AJAX/jQuery way

Upvotes: 4

dcd0181
dcd0181

Reputation: 1503

Your form action would have to contain some sort of conditional statement to redirect users based on which submit button is clicked

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

Or you could set up your conditionals on a page specified by the form actionand have them redirect based on which button was clicked,

Upvotes: 0

miszczu
miszczu

Reputation: 1189

You can do it in this way:

In form tag please leave empty action action=""

2 buttons to send:

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

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

Here is how to send data using ajax:
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/

Upvotes: 0

Emmanuel Okeke
Emmanuel Okeke

Reputation: 1482

That will not be possible since your form's action attribute can only point to one location at a time and both buttons are in the same form(but possible if you use AJAX).

If you wanted to use pure PHP (i.e. no Javascript involved), you'd have to write two different handlers for the different button clicks, like below:

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

In the actions part of each of the handlers, you could then do a redirect, with the URL containing the data to be processed either in the int.php or dom.php scripts

Upvotes: 0

Mike Boutin
Mike Boutin

Reputation: 5347

Do it with jquery! First, dont create submit buttons just create

<input type="button" />

Than give them an id like:

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

and with jquery bind the action

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

Upvotes: 0

Saleeh
Saleeh

Reputation: 402

you can just use switch in php for differ or use javascript

Upvotes: 0

CyberK
CyberK

Reputation: 1578

Just put a form tag, and set the action to the page. Then the submit button will navigate to that page where the action tag is pointing to...

Easy as that :D

Upvotes: -1

Related Questions