如果Recaptcha无效,如何保留数据字段

时间:2012-02-14 00:20:52

标签: php recaptcha

我的php联系表单工作正常,我只需要帮助如果在重新访问不正确或无效时如何保留数据字段。我需要在哪里添加更多脚本才能完成此操作? 非常感谢您的任何答案。我还是php新手。这是所有在一页称为:CONTACTFORM.php

<?php


require_once('recaptchalib.php');

$publickey = "xxxxxxxxx";
$privatekey = "xxxxxxxx";
$error = false;
$message = '';



if (isset($_POST['send'])){     
    $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

    if (!$resp->is_valid)
    {
        $error = true;      
        $message = 'Invalid Captcha Please try again';
    }

      if(!$error)
    {
        $emailTitle = 'Email from website!';
        $emailAddress = 'peace@hotmail.com';

        /* Gathering Data Variables */

        $emailField = $_POST['email'];
        $phoneField = $_POST['phone'];
        $nameField = $_POST['name'];
        $subjectField = $_POST['subject'];
        $messageField = $_POST['message'];

        $body = <<<EOD
<br><hr><br>
Email: $emailField <br />
Phone: $phoneField <br />
Name: $nameField <br />
Subject: $subjectField <br />
Message: $messageField <br />
EOD;
        $headers = "From: $emailField\r\n";
        $headers .= "Content-type: text/html\r\n";
        $success = mail("$emailAddress", "$emailTitle", "$body", "$headers");

        $message = "Thank you! Your message has been sent.";
    }
}
?>

<form id="form1" name="form1" method="post" action="contactform.php">
      <table width="100%" border="0" cellspacing="6" cellpadding="0">
        <tr>
          <td align="right"><label for="email" class="titulos">Email: </label></td>
          <td align="left"><input name="email" type="text" id="email" size="35" maxlength="50" /> 
            *</td>
        </tr>
        <tr>
          <td align="right"><label for="phone" class="titulos">Phone: </label></td>
          <td align="left"><input name="phone" type="text" id="phone" size="35" maxlength="50" /></td>
        </tr>
        <tr>
          <td align="right"><label for="name" class="titulos">Name:</label></td>
          <td align="left"><input name="name" type="text" id="name" size="35" maxlength="30" />
            *</td>
        </tr>
        <tr>
          <td align="right"><label for="subject" class="titulos">Subject:</label></td>
          <td align="left"><input name="subject" type="text" id="subject" size="35" maxlength="60" /></td>
        </tr>
        <tr>
          <td align="right" valign="top"><label for="message" class="titulos">Message:</label></td>
          <td align="left"><p>
            <textarea name="message" cols="27" rows="5" id="message"></textarea>
            *</p></td>
        </tr>
        <tr>
          <td align="right">&nbsp;</td>
          <td align="left"> 
          <?php echo recaptcha_get_html($publickey);
    ?>


</td>
        </tr>
        <tr>

          <td align="right"><label for="reset"></label></td>
          <td align="left"><label for="send"></label>
          <input name="send" type="submit" id="send" onclick="MM_validateForm('email','','RisEmail','name','','R','message','','R');return document.MM_returnValue" value="Send Email" /> <label for="reset"></label>
            <input type="reset" name="reset" id="reset" value="Reset form" /></td>
        </tr>
        <tr>
          <td align="right">&nbsp;</td>
          <td align="left" class="content">(*) Are Required fields</td>
        </tr>
        <tr>
          <td colspan="2" align="center"><?php echo $message; ?>
          </td>
          </tr>
      </table>
    </form>

1 个答案:

答案 0 :(得分:5)

您可以在输入字段的“值”属性中进行设置。像

这样的东西
<input name="email" type="text" value="<?php echo (isset($_POST['email']) ? htmlspecialchars($_POST['email']) : ''); ?>" />