Why is $_POST not set after submit ?
I’m using WAMPSERVER (x64) up to date.
Thank you for your help.
<!-- index.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form</title>
</head>
<body>
<form method="post" action="index.php">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if (isset($_POST))
print_r($_POST); // Result: 'Array ( )'
?>
</body>
</html>
5
Answers
It looks like your form is missing a name attribute for the submit button
. Without this, the $_POST array won’t be populated when the form is submitted
. Try adding a name attribute to your submit button like this:
If fname is set, it means the form has been submitted, and $_POST data will be printed.
The issue with this code is that You are using isset($_POST), which will always be true because $_POST is always an array, even if it’s empty and You don’t check for the presence of form field data like $_POST[‘fname’].
You can refer this if you need more details.
The issue with your code is that you are checking isset($_POST) instead of checking if the specific form field is set. The $_POST superglobal itself will always be set; you should check for the specific values submitted.
Here’s the corrected code:
Key Changes:
Check Specific Field: Instead of isset($_POST), use isset($_POST[‘fname’]) to check if the form field was submitted.
Output Condition: The print_r($_POST); will now execute only if the form is submitted.
The issue with your code is that you are checking is set ($_POST) instead of checking if the specific form field is set.
The $_POST superglobal itself will always be set; you should check for the specific values submitted.
Here’s the corrected code:
Key Changes:
Check Specific Field: Instead of isset($_POST), use isset($_POST[‘fname’]) to check if the form field was submitted.
Output Condition: The print_r($_POST); will now execute only if the form is submitted.