Search code examples
phphtmlmysqlpostundefined-index

PHP $_POST error Please Help me I am learning PHP


I am learning PHP. Here is the source code.

<?php
$text = $_POST['text'];

echo $text;
?>

<form action="index.php" method="post">
<input type="text" name="text" />
    <input type="submit">
</form>

Here is the result. I don't know where is the problem.

Notice: Undefined index: text in C:\xampp\htdocs\faisal\index.php on line 2


Solution

  • It means there's nothing in $_POST['text'] -- and there won't be, until after the form is submitted. You need to use isset() to check:

    <?php
    if(isset($_POST['text'])) {
        $text = $_POST['text'];
    
        echo $text;
    }
    ?>
    
    <form action="index.php" method="post">
    <input type="text" name="text" />
        <input type="submit">
    </form>