cfocket
cfocket

Reputation: 109

PHP Form, if not filled properly, all field clear themselves

I made a contact form following this tutorial : http://tinyurl.com/nyxv8ub

Everything is working but some parameters are not adressed.

As a matter of fact, on submit, if the user hasn't filled the form properly he is notified and all fields clear themselves.

It is a pain for the user to re-type his message, so I'd like him to be notified of his mistakes without clearing any field.

Currently, there are 2 ways to get the form wrong. Whether you don't fill the Email or Name fields, or when the answer to the spam question is wrong.

Is it possible to prevent this from happening?

You can find the form here : http://bettercheckthekids.com/Form/index.php

And here is the php :

<?php
    $name = $_POST['name'];
    $firm = $_POST['firm'];
    $email = $_POST['email'];
    $list = $_POST['list'];
    $message = $_POST['message'];
    $spam = $_POST['spam'];
    $from = 'From: Adiwatt Online'; 
    $to = '[email protected]'; 
    $subject = 'Hello';

    $body = "From: $name\n Firm: $firm\n E-Mail: $email\n Objet: $list\n Message:\n $message";
    ?>


    <?php
    if ($_POST['submit']) {
    if ($name != '' && $email != '') {
        if ($spam == '4') {                 
            if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        } 
    } else if ($_POST['submit'] && $spam != '4') {
        echo '<p>You answered the anti-spam question incorrectly!</p>';
    }
    } else {
        echo '<p>You need to fill in all required fields!!</p>';
    }
    }
    ?>

Upvotes: 0

Views: 1470

Answers (3)

Lajos Arpad
Lajos Arpad

Reputation: 76583

First of all, I would separate the backend code from the structure as much as possible. I would not generate the html inside the php code if I had any other option. Your structure should look like this:

<form method="post" action="index.php"> 
    <label>Name *</label>
    <input name="name" placeholder="..." value="<?php echo ((isset($_POST["name"])) ? (htmlspecialchars($_POST["name"], ENT_QUOTES)) : ("")); ?>">

    <label>Firm</label>
    <input name="firm" placeholder="..." value="<?php echo ((isset($_POST["firm"])) ? (htmlspecialchars($_POST["firm"], ENT_QUOTES)) : ("")); ?>">

    <label>Email *</label>
    <input name="email" type="email" placeholder="..." value="<?php echo ((isset($_POST["email"])) ? (htmlspecialchars($_POST["email"], ENT_QUOTES)) : ("")); ?>">

    <label>Object</label>
    <select name="list" id="list"> 
       <option value="Pas d'objet" <?php if ((isset($_POST["list"])) && ($_POST["list"] === "Pas d'objet")) {echo "selected";} ?>>--</option> 
       <option value="Je veux sortir" <?php if ((isset($_POST["list"])) && ($_POST["list"] === "Je veux sortir")) {echo "selected";} ?>>Object 1</option> 
       <option value="Je sortirai demain" <?php if ((isset($_POST["list"])) && ($_POST["list"] === "Je sortirai demain")) {echo "selected";} ?>>Object 2</option> 
       <option value="Je n'ai pas d'argent" <?php if ((isset($_POST["list"])) && ($_POST["list"] === "Je n'ai pas d'argent")) {echo "selected";} ?>>Object 3</option> 
    </select> 

    <label>Message</label>
    <textarea name="message" placeholder="..."><?php echo ((isset($_POST["message"])) ? (htmlspecialchars($_POST["message"], ENT_QUOTES)) : ("")); ?></textarea>

    <label>What is 2+2? (Anti-spam) *</label>
    <input name="spam" placeholder="..." value="<?php echo ((isset($_POST["spam"])) ? (htmlspecialchars($_POST["spam"], ENT_QUOTES)) : ("")); ?>">    

    <input id="submit" name="submit" type="submit" value="Send">
</form>

Cheers!

Upvotes: 2

Justin Paul Pa&#241;o
Justin Paul Pa&#241;o

Reputation: 936

In your input tag say <input name="name" placeholder="...">, change that to this <input name="name" placeholder="..." value="<?php if(isset($POST['name'])) echo $_POST['name'] : '' ?>">.

There is also the ternary operator variation of my example like so: value="<?php echo isset($POST['name']) ? $_POST['name'] : '' ?>".

Upvotes: 1

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174967

Use the input's value attribute to pre-insert the user's input on the previous form.

Also, you can use HTML5's required attribute on the input, and use JavaScript to make sure all fields are set correctly before the form is submitted.

Examples:

  • Using value with PHP

    $value = isset($_POST["value"]) ? htmlspecialchars($_POST["value"], ENT_QUOTES) : "";
    /* ... snip ... */
    <input name="value" value="<?= $value ?>"> 
  • Using the required attribute

    <input name="value" required>

    This will make the browser (assuming it's a modern one) to validate and make sure it's not empty when the form gets submitted.

As for JavaScript, read up on a form's onsubmit event, and an input's value property.

Upvotes: 2

Related Questions