I am not sure where to place the validation script:
if (! strlen($_POST['first_name'])) {
echo 'You must enter your first name.';
As it is now, the echo 'You must.... is being echoed onto the page when it is loaded. You can check it at http://www.studylinkjapan.com/testform.php
The code is below:
<?php if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME'] ?>" method="post">
What is your first name?
<input type="text" name="first_name" />
<input type="submit" value="Say Hello" />
</form>
<?php
}
if (! strlen($_POST['first_name'])) {
echo 'You must enter your first name.';
}
else
{
echo 'Hello, ' . $_POST['first_name'] . '!';
}
?>
Simple required field validation php
Moderator: General Moderators
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Simple required field validation php
Code: Select all
<?php if(isset($_POST['submitButton']))
{
// validation here.
}
?>Code: Select all
<input type="form" name="submitButton" value="Say hello" />“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering