/    Sign up×
Community /Pin to ProfileBookmark

Form Validation

I would like my program to check if the user has typed anything into the input box, if not then it would ask the user to do so, otherwise it would submit my form to another page, display.html.
The problem here is that it just submit my form before validating if the user has typed anything into the input box.

How do I get it check the input box first before submit the form?

Here’s my code:

[code=html]<!doctype html>
<html lang=”en”>
<head>
<style>.error {color: #FF0000;}</style>
</head>

<?php
// define variables and set to empty values
$nameErr = $name = “”;

if ($_SERVER[“REQUEST_METHOD”] == “POST”) {
if (empty($_POST[“name”])) {
$nameErr = “Name is required”;
} else {
$name = $_POST[“name”];
}
}
?>

<body>
<form name=”search_form” id=”searchForm” method=”POST” action=”display.html”>
Name: <input type=”text” name=”myName” id=”myName”>
<span class=”error”>* <?php echo $nameErr;?></span><br><br>

<button id=”submit”>Submit</button>
</form>

</body>
</html>[/code]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@jedaisoulSep 02.2014 — 
  • 1. If you want the validation BEFORE the form is submitted, use JavaScript (hence I've moved this thread). If you meant, you wanted the validation done, before the input is processed, that is easy:


  • a) Make your form action "" (empty quotes), that causes the form to link back to itself.

    b) Include display.html as below...

    c) Name the submit control (so it is included in the POST).

    d) Test for the submit control.

    - if set, validate as required. - if found run display code.

    - if not set, or not found - display input form with error message as appropriate.

    Something like:
    [code=html]
    <body>
    <?php
    $error='';
    $mode='input';
    $name='';
    if (isset ($_POST['submit'])) {
    if (isset ($_POST['name'])) {

    $name = $_POST['name'];
    $mode = 'display';

    } else {

    $error = 'please set name...';
    $mode = 'input';

    } else {

    $error = '';
    $mode = 'input';

    }

    if ($mode == 'input') {
    echo $error;
    ?>

    <!-- put input form here -->
    <form method="POST" action="">
    <input type="text" name="name">
    <input type="submit" name="submit" value="submit">
    </form>

    <?php ;} else {

    include 'display.html';

    } ?>
    [/code]
    Copy linkTweet thisAlerts:
    @locbtranauthorSep 02.2014 — 1. If you want the validation BEFORE the form is submitted, use JavaScript (hence I've moved this thread). If you meant, you wanted the validation done, before the input is processed, that is easy:[/QUOTE]

    Not really sure what the difference is...

    If the user does NOT enter anything in the textbox but just hit the submit button, it does NOT go to a new page (just sit there) and will display the error message, telling him he has to enter something in the textbox FIRST.

    If the user does enter something in the text box, then hit the submit button, now it can go to a new page, [B]display.html[/B].

    Tks

    PS> I tested your code and it doesn&#8217;t work.
    Copy linkTweet thisAlerts:
    @jedaisoulSep 02.2014 — OOps! Try:

    <i>
    </i>&lt;!DOCTYPE html&gt;
    &lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
    &lt;title&gt;HTML5 Validator&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;?php
    error_reporting(E_ALL ^ E_NOTICE);
    $error='';
    $mode='input';
    $name='';
    if (isset ($_POST['name']) &amp;&amp; ($_POST['name'] &gt; '')) {
    $name = $_POST['name'];
    $mode = 'display';
    }

    elseif (isset ($_POST['submit'])) {

    $error = 'please set name...';
    }

    if ($mode == 'input') {
    echo $error;
    ?&gt;

    &lt;!-- put input form here --&gt;
    &lt;form method="POST" action=""&gt;
    &lt;input type="text" name="name"&gt;
    &lt;input type="submit" name="submit" value="submit"&gt;
    &lt;/form&gt;

    &lt;?php ;} else {

    include 'display.html';

    } ?&gt;
    &lt;/body&gt;
    &lt;/html&gt;
    ×

    Success!

    Help @locbtran spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...