Jump to content

Recommended Posts

<?php
if(isset($_POST["submit"])) {
$fullName = $_POST["fullname"];
$errors = array();
if(empty($fullName)){
array_push($errors, "Fullname is empty");
if (count($errors)>0) {
foreach($errors as $error) {
echo $error;}
}
}
?>

<html>
<head>
  <title>Help</title>
  </head>
  <body>
  <input class="submit-btn" name="submit" value="Register">
  </body>
</html>
  

if i use this code, it says Fullname is empty before i click the button i wanna check if something is missing while someone hits Register

How is this script executed the first time?  From the address bar of your web page? Or from a command that you issue?  Obviously it has to be started somehow and that means that you either triggered it from a submit that is from a form that was sent to the user by a different script.  If not that is your problem.  The form in this script is not being sent until your code runs and that is your query. Also - Your form only contains a text field and no buttons.  And I wouldn't name my text field 'submit' since it is NOT a submit button.  The default value of an input tag is 'text' I believe.

  • You really have to use an editor with indentation and make sure that you are matching braces. 
    • The code you provided is missing a matching bracket.
  • There's also an obvious problem which is that you output errors, before you've started the html section of the page (in cases where there is an error). 
    • This will at best malform your html document.
  • The other issue is that you don't have a form, nor does that form set the method to make a POST, so there's no way that the code can work as you intended.
    • You also don't have a form element to test against  

Here's something more likely to work, based on what you provided:

 

<?php
    $errors = array();
    if (!empty($_POST["submit"])) {
        $fullName = $_POST["fullname"];

        if (empty($fullName)) {
            array_push($errors, "Fullname is empty");
        }
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Help</title>
    </head>

    <body>
        <?php if (count($errors)>0): ?>
            <ul>
                <?php foreach($errors as $error): ?>
                    <li><?= $error ?></li>
                <?php endforeach ?>
            </ul>
        <?php endif ?>
        <form method="POST">
            <input name="fullname" type="text">
            <input class="submit-btn" name="submit" type="submit"  value="Register">
        </form> 
    </body>
</html>

 

 

 

If you sit back and look at what you have written it might just come to you.

You execute the php code BEFORE even send the html to the client.  OF Course it is going to tell you things are empty because at that time THEY ARE.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.