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 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败