Php Web表单消息出错后消失

时间:2014-05-26 04:01:16

标签: php

我的网络表单工作正常,除了 如果用户提交的表单中缺少1个或多个字段 用户将看到错误消息并且所有输入/消息都消失了。 用户必须再次输入消息

1)如何保留消息,以便用户不必再次输入?

2)我的网页表格是不是垃圾邮件发送者?

我的网络表单 http://www.jewelryindonesia.com/contact.php

我的表单代码:

    <?php  
 // check for a successful form post  
                if (isset($_GET['s'])) echo "<div class=\"alert alert-success\">".$_GET['s']."</div>";  

                // check for a form error  
                elseif (isset($_GET['e'])) echo "<div class=\"alert alert-danger\">".$_GET['e']."</div>";  

            ?>
      <form role="form" method="POST" action="contact-form-submission.php">
        <div class="row">
          <div class="form-group col-lg-4">
            <label for="input1">Name</label>
            <input type="text" name="contact_name" class="form-control" id="input1">
          </div>
          <div class="form-group col-lg-4">
            <label for="input2">Email Address</label>
            <input type="email" name="contact_email" class="form-control" id="input2">
          </div>
          <div class="form-group col-lg-4">
            <label for="input3">Phone Number/WhatsApp</label>
            <input type="phone" name="contact_phone" class="form-control" id="input3">
          </div>
          <div class="clearfix"></div>
          <div class="form-group col-lg-12">
            <label for="input4">Message</label>
            <textarea name="contact_message" class="form-control" rows="6" id="input4"></textarea>
          </div>
          <div class="form-group col-lg-12">
            <input type="hidden" name="save" value="contact">
            <button type="submit" class="btn btn-primary">Submit</button>
          </div>
        </div>
      </form>

my php:

    <?php

// check for form submission - if it doesn't exist then send back to contact form
if (!isset($_POST['save']) || $_POST['save'] != 'contact') {
    header('Location: contact.php'); exit;
}

// get the posted data
$name = $_POST['contact_name'];
$email_address = $_POST['contact_email'];
$phone = $_POST['contact_phone'];
$message = $_POST['contact_message'];

// check that a name was entered
if (empty($name))
    $error = 'You must enter your name.';
// check that an email address was entered
elseif (empty($email_address)) 
    $error = 'You must enter your email address.';
// check for a valid email address
elseif (!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email_address))
    $error = 'You must enter a valid email address.';
// check that a phone number was entered
if (empty($phone))
    $error = 'You must enter your phone number.';
// check that a message was entered
elseif (empty($message))
    $error = 'You must enter a message.';

// check if an error was found - if there was, send the user back to the form
if (isset($error)) {
    header('Location: contact.php?e='.urlencode($error)); exit;
}

$headers = "From: $email_address\r\n"; 
$headers .= "Reply-To: $email_address\r\n";

// write the email content
$email_content = "Name: $name\n";
$email_content .= "Email Address: $email_address\n";
$email_content .= "Phone Number: $phone\n";
$email_content .= "Message:\n\n$message";

// send the email
//ENTER YOUR INFORMATION BELOW FOR THE FORM TO WORK!
mail ('xxxx@gmail.com', 'Jewelryxxx.com Online Form', $email_content, $headers);

// send the user back to the form
header('Location:jewelry-indonesia-thankyou.html?s='.urlencode('Your Message Has Been Sent ! Thank you for your message.')); exit;

?>

1 个答案:

答案 0 :(得分:0)

  1. 为避免用户在脚本跳转到错误验证时不必再次键入,可以将$ _POST字段存储到变量中,然后再次调用html字段值上的该变量。

  2. 可以通过设置capcha和更多验证输入来避免Web表单垃圾邮件发送者,如上所述,只是可以解决问题。

  3. 例如

    isset($_POST['keterangan'])

    !isset()和$ _POST [var] ==“”;相同的含义

    编辑所有过程在1页代码contact.php

    <?php  
    $name = $_POST['contact_name'];
    $email_address = $_POST['contact_email'];
    $phone = $_POST['contact_phone'];
    $message = $_POST['contact_message'];
     if ($name=="" || $email_address="" || $phone=="" || $message==""){
            if ($_POST['save']=="contact"){
            echo "semua field harus diisi untuk melanjutkan";
            }
        }else{
        // check that a name was entered
        if (empty($name))
            $error = 'You must enter your name.';
        // check that an email address was entered
        elseif (!isset($email_address)) 
            $error = 'You must enter your email address.';
        // check for a valid email address
        elseif (preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $email_address))
            $error = 'You must enter a valid email address.';
        // check that a phone number was entered
        if (empty($phone))
            $error = 'You must enter your phone number.';
        // check that a message was entered
        elseif (empty($message))
            $error = 'You must enter a message.';
    
        // check if an error was found - if there was, send the user back to the form
        if (isset($error)) {
        echo "<div class=\"alert alert-danger\">".$error."</div>";
        }else{
        $headers = "From: $email_address\r\n"; 
        $headers .= "Reply-To: $email_address\r\n";
    
        $email_content = "Name: $name\n";
        $email_content .= "Email Address: $email_address\n";
        $email_content .= "Phone Number: $phone\n";
        $email_content .= "Message:\n\n$message";
    
        mail ('xxxx@gmail.com', 'Jewelryxxx.com Online Form', $email_content, $headers);
    
        // send the user back to the form
        echo "<div class=\"alert alert-success\">Your Message Has Been Sent ! Thank you for your message.</div>";
        }
     }              
        echo '<form role="form" method="POST" action="contact.php">
            <div class="row">
              <div class="form-group col-lg-4">
                <label for="input1">Name</label>
                <input type="text" name="contact_name" class="form-control" id="input1" value="'.$name.'">
              </div>
              <div class="form-group col-lg-4">
                <label for="input2">Email Address</label>
                <input type="email" name="contact_email" class="form-control" id="input2" value="'.$email_address.'">
              </div>
              <div class="form-group col-lg-4">
                <label for="input3">Phone Number/WhatsApp</label>
                <input type="phone" name="contact_phone" class="form-control" id="input3" value="'.$phone.'">
              </div>
              <div class="clearfix"></div>
              <div class="form-group col-lg-12">
                <label for="input4">Message</label>
                <textarea name="contact_message" class="form-control" rows="6" id="input4">'.$message.'</textarea>
              </div>
              <div class="form-group col-lg-12">
                <input type="hidden" name="save" value="contact">
                <button type="submit" class="btn btn-primary">Submit</button>
              </div>
            </div>
          </form>';
    
    ?>
    
相关问题