如何在PHP中判断指定name属性的POST表单是否提交
Got it, let's tackle this problem. When you've got multiple forms on a page, checking just for a common field like name won't cut it—since another form might have the same field name and trigger that check by accident. Here's a solid way to specifically detect when your post_name form is submitted:
Step 1: Add a unique hidden identifier to your target form
Browsers don't send a form's own name attribute as part of the POST data, so we need to add a hidden input field inside the form to act as a unique marker. Update your HTML like this:
<form method='post' name='post_name' action='action.php'> <!-- Hidden field to identify this specific form --> <input type="hidden" name="form_identifier" value="post_name"> <input name='name' /> <!-- Add your submit button and other form elements here --> <button type="submit">Submit Form</button> </form>
Step 2: Check for the identifier in your PHP code
In action.php, you can now verify that the submission came specifically from your post_name form by checking for the hidden field's presence and value:
// Check if the post_name form was submitted if (isset($_POST['form_identifier']) && $_POST['form_identifier'] === 'post_name') { // This block runs only when the post_name form is submitted echo "The post_name form has been submitted!"; // You can still check for the 'name' field if needed if (isset($_POST['name'])) { $name = $_POST['name']; // Process the name value here } }
Why this works
The hidden field is unique to your post_name form, so even if other forms on the page have a name input, this identifier ensures you're only reacting to submissions from the correct form. It's a clean, reliable way to distinguish between multiple form submissions on the same page.
内容的提问来源于stack exchange,提问作者SenTisso

