There are two (multiple) submit in a form in HTML. how to distinguish between the backend (pure HTML implementation without javascript) and formjavascript

Source: Internet
Author: User

There are two (multiple) submit in a form in HTML. how to distinguish between the backend (pure HTML implementation without javascript) and formjavascript

A form may have multiple Submit elements. How can I identify which button is clicked in the background?

Many people on the Internet say that javascript is used to write a long function and click different buttons to submit different data. In other words, multiple forms are used. In fact, this is not necessary.

The two methods can be implemented without using javascript.

Method 1: use different name attributes
 
 
  1. <form method="post">
  2. <Input type = "submit" name = "save" value = "save settings"/>
  3. <Input type = "submit" name = "reset" value = "reset Settings"/>
  4. </form>

Background PHP processing:

 
 
  1. if ( $_REQUEST['save'] ) {
  2. // ...
  3. } elseif ( $_REQUEST['reset'] ) {
  4. // ...
  5. }

The reason is that only the data of the clicked submit button will be submitted.

Method 2: Use the same name and different values
 
 
  1. <form method="post">
  2. <Input type = "submit" name = "action" value = "Save settings"/>
  3. <Input type = "submit" name = "action" value = "Reset Settings"/>
  4. </form>

Background PHP processing:

 
 
  1. If ($ _ REQUEST ['action'] = 'Save settings '){
  2. // ...
  3. } Elseif ($ _ REQUEST ['action'] = 'reset settings '){
  4. // ...
  5. }

Note that in method 2, the front and back-end encoding must be consistent because the Chinese characters are used. Otherwise, the judgment may fail.

Because<input type="submit"/>Element,valueThe attribute is the text to be displayed on the button. It is very likely to use Chinese characters and may need to be modified because of the front-end. The background Code also needs to be modified, so the second method is not recommended, method 1 is recommended.

This article by jzj1993 original, reproduced please note Source: http://www.hainter.com/html-form-submit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.