PHP表单在提交后用一个填充

时间:2019-01-27 23:38:58

标签: php html forms

我很困惑。...

我创建了一个PHP表单来收集一些用户信息,并通过电子邮件将其发送给网站所有者,在表单提交后,这些字段会自动完成,并带有一个“ 1”。我假设这意味着该字段为真并已提交。 / p>

有人在提交表单后如何隐藏或删除1方面有任何建议吗?我只希望表单提交后为空白。这是代码,对不起格式不好和令人眼花wall乱的代码。

在此先感谢您的帮助!

PHP代码

    $errName = "";
    $errcatBreed = "";
    $errEmail = "";
    $errMessage = "";
    $result = "";

if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $catBreed = $_POST['catBreed'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'PearTreeHill Contact Form';
    $to = 'test@domain.com';
    $subject = "New furbaby enquiry from $name";

    $body ="From: $name\n Cat Breed: $catBreed\n E-Mail: $email\n Message:\n $message";

    // Check if name has been entered
    if (!$_POST['name']) {
        $errName = 'Please enter your name';
    }

    // Check if cat breed has been entered
    if (!$_POST['catBreed']) {
        $errcatBreed = 'Please enter either Ragdoll or British ShortHair';
    }

    // Check if email has been entered and is valid
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Please enter a valid email address';
    }

    //Check if message has been entered
    if (!$_POST['message']) {
        $errMessage = 'Please enter your message';
    }

    // RECAPTCHA
    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']))
  {
        $secret = 'Private Key';
        $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
        $responseData = json_decode($verifyResponse);
        if($responseData->success)
        {
            $succMsg = 'Your contact request have submitted successfully.';
        }
        else
        {
            $errMsg = 'Robot verification failed, please try again.';
        }
   }

// If there are no errors, send the email
if (!$errName && !$errcatBreed && !$errEmail && !$errMessage) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again.</div>';
    }
}
    }

HTML代码

<!-- contact form -->
<section id="contactUs" class="bg">
  <div class="container">
    <div class="row">
      <div class="col-lg-8 mx-auto">
        <div class="text-center">
        <h2 class="w3-tangerine">Contact Us</h2>
      </div>
      <!-- start of entry form -->
      <form class="form-horizontal" role="form" method="post" action="index.php">
        <!-- name entry -->
        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">Name</label>
            <div class="col-sm-12">
                <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars(isset($_POST['name'])); ?>">
            <?php echo "<p class='text-danger'>$errName</p>";?>
            </div>
        </div>
        <!-- cat breed selection -->
        <div class="form-group">
            <label for="catBreed" class="col-sm-2 control-label">Cat Breed</label>
            <div class="col-sm-12">
                <input type="text" class="form-control" id="catBreed" name="catBreed" placeholder="'Ragdoll' or 'British ShortHair'" value="<?php echo htmlspecialchars(isset($_POST['catBreed'])); ?>">
            <?php echo "<p class='text-danger'>$errcatBreed</p>";?>
            </div>
        </div>
        <!-- email address entry -->
        <div class="form-group">
            <label for="email" class="col-sm-2 control-label">Email</label>
            <div class="col-sm-12">
                <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="<?php echo htmlspecialchars(isset($_POST['email'])); ?>">
            <?php echo "<p class='text-danger'>$errEmail</p>";?>
            </div>
        </div>
        <!-- Body of message -->
        <div class="form-group">
            <label for="message" class="col-sm-2 control-label">Message</label>
            <div class="col-sm-12">
                <textarea class="form-control" rows="4" name="message" placeholder="Please enter any other information"><?php echo htmlspecialchars(isset($_POST['message']));?></textarea>
            <?php echo "<p class='text-danger'>$errMessage</p>";?>
            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-10 col-sm-offset-2">
            <!-- reCAPTCHA -->
            <div class="g-recaptcha" data-sitekey="Public Key" data-callback="recaptcha_callback"></div>
            <!-- send button -->
            <input disabled="disabled" id="submit" name="submit" type="submit" value="Send Message" class="btn btn-danger">
            <label for="submit" class="col-sm-8 control-label">Please allow up to 48 hours for a response!</label>
            </div>
        </div>
        <!-- entry alert -->
        <div class="form-group">
            <div class="col-sm-10 col-sm-offset-2">
                <?php echo $result; ?>
            </div>
        </div>
      </form>
    </div>
   </div>
  </div>

2 个答案:

答案 0 :(得分:0)

<?php echo htmlspecialchars(isset($_POST['email'])); ?>

此处显示isset()TRUEFALSE)的结果。

您应该这样做:

<?php if ( isset($_POST['email']) ) echo htmlspecialchars($_POST['email']); ?>

每个字段都有这个:-)

答案 1 :(得分:0)

更改

echo htmlspecialchars(isset($ _ POST ['name']));

echo htmlspecialchars($ _POST ['name'])

我的意思是删除isset();