0

It's OK to have a form with more submit buttons? I mean I have a form which contains some input fields and a table and 3 buttons which will export some information in different formats

<form id="form1" name="form1" method="post" >
    <label for="country"><strong>Countries: </strong></label><br />
    <select id="country" name="country">
      <?php
      //some php code here
      ?>
    </select> 
    <br />
    <br/>
    <label for="start_date"><strong>Start date: </strong></label><br />
    <input name="start_date" type="text" id="start_date" value="<?php if(!empty($start))echo $start; ?>" size="20" />
    <br />
    <br />  
    <label for="stop_date"><strong>Stop date: </strong></label><br />
    <input name="stop_date" type="text" id="stop_date" value="<?php if(!empty($stop)) echo $stop; ?>" size="20" />
    <br />
    <br />
        <input type="submit" name="fill_table" value="Retrieve data" /> 

    <div id="table">
      <h1>&nbsp;</h1>
      <hr />
      <h2>Sales Report</h2>
        <table class="display" id="data_table" >
          <thead>
              <tr>
                <th>Product</th>
                <th>Date</th>
                <th>Quantity</th>
              </tr>
          </thead>
          <tbody>
            <?php
                //some php code here
            ?>
          </tbody>
        </table>
        <br />
        <br />
        <br />
        <input type="submit" class="button" name="export_xls" value="Export xls" />
        <input style="margin-left:10px;" type="submit" name="export_html" value="Export html" />
  </form>

Is it ok to do it this way or should i make 3 forms each one having a particular submit button? Each time i press a submit button I'm intrested in the input fields :start_date, stop_date and the selected item from "country". P.S. I want to know if it's optimal and how other programmers would handle this stuff

Nikola
  • 14,888
  • 21
  • 101
  • 165

4 Answers4

2

The main problem is: What happens when the user presses Enter in a text input field? On most browsers, the form data gets submitted, but does this correspond to pressing one of the buttons, and which one? See e.g. the question Multiple submit buttons in an HTML form.

It would be safest to have just one submit button per form. (However it would be safe to add submit buttons with identical functionality into a long form, for user convenience.) This means that the user would make a choice between alternatives (e.g., output formats) by using radio buttons or a select menu, not by a choice between buttons. This is not always practical, though.

Community
  • 1
  • 1
Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
1

I don't think it's a bad idea. But all your submit buttons must have the same name attribute.

Check out this

NoZip
  • 26
  • 5
1

Yes, you can have more submit buttons in one form. Since you're using php to process the submited form, I wouldn't put the same name for each submit button as @NoZip suggested, but different instead, so that then in the php processing part of the code I would ask:

<?php
    if (isset($_POST["submitButtonName1"]))
        some code here...
    else if (isset($_POST["submitButtonName2"]))
        some other code here...
    else
        code logic for no submit button clicked   
?>
Nikola
  • 14,888
  • 21
  • 101
  • 165
  • Whats the benefit of using different names over one name and different values? – Dpolehonski Jul 20 '12 at 07:54
  • 1
    @Dpolehonski: I don't know what's the benefit (if any) in other programming languages, but I was referring to the PHP language. How would you check if certain button was clicked in PHP if you don't give them different `name` attributes? – Nikola Jul 20 '12 at 07:58
  • you compare the value of the submitted "name" like `if "bsubmit" == "opt1" //do something if "bsubmit" == "opt2" //do something else ` – 55651909-089b-4e04-9408-47c5bf Jul 20 '12 at 08:16
  • 1
    @nickNatra: As they say, the preferences are not to be discussed. I feel my way is somewhat "cleaner" cause I have "one" button for each "action". Also, IMO, the statement that makes most sense to why not use the checking by value is - what if you have a multilanguage site and you go checking by value - your script would fall apart in this case. – Nikola Jul 20 '12 at 08:19
1

I have just finished a system in which you cna update data records, one of the features I used was to use mulitple submits to allow the user to 'stamp' the update with different options. e.g 'active','pending', 'disable'. It saved a drop down box of multiple options and kept the system intuitive.

Multiple submit buttons with different values allow you to submit the form with the submit field having one of many values and can imrove a user interface. Plus, its valid HTML.

Dpolehonski
  • 948
  • 6
  • 11