Rumah > Artikel > pembangunan bahagian belakang > Bagaimana untuk Menangkap Nilai Butang Hantar dalam Penyerahan Borang?
In web development, it is common to have a form that sends user input to a server-side script for processing. When the user clicks on a submit button, the form data is sent along with the request. However, the submit button's value is often not included in the form data by default.
Problem:
Consider a code snippet that allows users to select a name from a drop-down list and then click on a button to purchase a product. The form is sent to the buy.php script, which processes the name field but does not include the value of the submit button.
Solution:
To send the submit button's value, we need to give it a name that is identifiable by the server-side script. In the modified HTML code provided:
<input>
We assign the name "submit" to both submit buttons. Now, in the PHP script:
<?php if (isset($_POST['action'])) { echo '<br />The ' . $_POST['submit'] . ' submit button was pressed<br />'; } ?>
We check if the "action" hidden input is set, which indicates that the form was submitted. If it is, we echo the value of the "submit" field, which represents the submit button that was clicked.
Note:
It's crucial to ensure that each HTML element has a unique ID and to give the submit buttons the same name to capture the correct value on the server side.
Atas ialah kandungan terperinci Bagaimana untuk Menangkap Nilai Butang Hantar dalam Penyerahan Borang?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!