dslfq06464 2014-01-24 11:29 采纳率: 0%
浏览 30
已采纳

PHP表单,如果没有正确填写,所有字段都清除自己

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 = 'clement.fauquet@gmail.com'; 
    $subject = 'Hello';

    $body = "From: $name
 Firm: $firm
 E-Mail: $email
 Objet: $list
 Message:
 $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>';
    }
    }
    ?>
  • 写回答

3条回答 默认 最新

  • dpm91915 2014-01-24 12:38
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助