skip to Main Content

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


  1. 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:

    <?php
    if (isset($_POST['submit'])) {
        print_r($_POST);
    }
    ?>
    
    Login or Signup to reply.
  2. <!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" value="Submit">
        </form>
    
        <?php
        
        if (isset($_POST['fname'])) {
            print_r($_POST);
        } 
        ?>
    </body>
    </html>
    

    If fname is set, it means the form has been submitted, and $_POST data will be printed.

    enter image description here

    Login or Signup to reply.
  3. 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.

     <!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" value="Submit">
            </form>
        
            <?php
            // Check if the form was submitted and 'fname' is set
            if ( isset($_POST['fname'])) {
                echo 'Form submitted!<br>';
                print_r($_POST['fname']);
            } else {
                echo 'No form data submitted.';
            }
            ?>
        </body>
    </html>
    
    Login or Signup to reply.
  4. 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:

    <!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
        // Check if the form has been submitted and the 'fname' field is set
        if (isset($_POST['fname'])) {
            print_r($_POST);
        }
        ?>
    </body>
    </html>
    

    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.

    Login or Signup to reply.
  5. 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:

    <!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
        // Check if the form has been submitted and the 'fname' field is set
        if (isset($_POST['fname'])) {
            print_r($_POST);
        }
        ?>
    </body>
    </html>
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search