The check message cannot show after i press the submit button

Viewed 24

How to edit the code by only php? After I insert nothings in the textbox and click submit, the span there will run out with the message of 'Please insert this' . I done same code with yt but still not successfully done. Here is my code

<html>
<head>
    <h1>Members</h1>
       <style>
        .error {color: #FF0000;}
        </style>
</head>
<body>
    <form method="post" action="process.php"></form>
        <table border="1">
            <tr>
                <td >Number of Person</td>
                <td align="center"><input type="text" name="person" size="18" maxlength="18"><span class="error"><?php echo $sperson;?></span></td>
            </tr>
            <tr>
                <th colspan="2" align="right"><input type="submit" name="submit" value="submit"></th>
              </tr>
        </table>
    </form>
    <?php

if (isset($_POST['submit']))
{
    $vperson = $_POST['person'];
    if (empty($vperson))
    {
        $sperson = "Person is required";
    }
    else 
    {
        $sperson = "Person is required";
    }
}

?>

1 Answers

There is a redundant </form> in your HTML code (see below), remove it so that the system can include the real submission data by referring to the other </form> tag

The code below contains the redundant </form>

<form method="post" action="process.php"></form>

On the other hand, for your case please put the php part to the top of your script so that the variable $sperson can be set and be displayed in the subsequent HTML/php part.

So change your PHP code to :

<?php

if (isset($_POST['submit']))
{
    $vperson = $_POST['person'];
    if (empty($vperson))
    {
        $sperson = "Person is required";
    }
    else 
    {
        $sperson = "Thank you.";
    }
}

?>
<html>
<head>
    <h1>Members</h1>
       <style>
        .error {color: #FF0000;}
        </style>
</head>
<body>
    <form method="post" action="#">
        <table border="1">
            <tr>
                <td >Number of Person</td>
                <td align="center"><input type="text" name="person" size="18" maxlength="18"><span class="error"><?php echo $sperson;?></span></td>
            </tr>
            <tr>
                <th colspan="2" align="right"><input type="submit" name="submit" value="submit"></th>
              </tr>
        </table>
    </form>


Related