简体   繁体   English

提交失败后如何保持表格输入

[英]How to keep form input after failed submit

I am fairly new to coding and I am building a contact form using PHP. 我对编码非常陌生,并且正在使用PHP构建联系表单。 I have included a reCaptcha that must be checked. 我已包括必须检查的reCaptcha。 If the user fills out the form, and presses the submit button without checking the reCaptcha. 如果用户填写表格,然后按提交按钮,而不检查reCaptcha。 The form resets and the user would have to fill everything out once again. 表单将重置,用户将不得不再次填写所有内容。

How can I keep the input when the reCaptcha is not checked and the form is submitted? 当未选中reCaptcha并提交表单时,如何保留输入?

I have found on here to use htmlspecialchars. 我在这里发现使用htmlspecialchars。 It does work by replacing the html characters, but the user would still have to fill it out again if they used quotes or < >. 它确实可以通过替换html字符来工作,但是如果用户使用引号或<>,则仍必须再次填写。

Any advice would be awesome on XSS or SQL injection safety measures or using AJAX possibly. 关于XSS或SQL注入安全措施或可能使用AJAX的任何建议都很棒。

<label for="message"> Message:</label>
<textarea class="form-control" type="textarea" id="message" 
name="message" maxlength="6000" rows="5" value="<?php echo 
htmlspecialchars($message); ?>" required ></textarea>

Here is how I have my PHP 这是我的PHP的方式

    ```            

   if(filter_has_var(INPUT_POST, 'submit')) 
    {



  $name = htmlspecialchars($_POST['name']);
  $email = htmlspecialchars($_POST['email']);
  $message = htmlspecialchars($_POST['message']);
  $phone = $_POST['phone'];



  $mail = new PHPMailer;

                                                            // Enable verbose debug output

 $mail->isSMTP();    // Set mailer to use SMTP
  $mail->SMTPDebug = 0; 
 $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;    // Enable SMTP authentication
 $mail->Username = EMAIL;                 // SMTP username
  $mail->Password = PASS;         // SMTP password
 $mail->SMTPSecure = 'tls';    // Enable TLS encryption, `ssl` also accepted
 $mail->Port = 587;    // TCP port to connect to

  $mail->setFrom($email, $name);
  $mail->addAddress('mail.com', 'Joe User');     // Add a recipient
 // Name is optional
   $mail->addReplyTo($email);   // Optional name
    $mail->isHTML(true);       // Set email format to HTML

   $mail->Subject = 'Client Contact Email';
    $mail->Body    = '<h2>Contact Request</h2>
                     <h4>Name</h4><p>'.$name.'</p>
                     <h4>Email</h4><p>'.$email.'</p>
                     <h4>Message</h4><p>'.$message.'</p>
                      <h4>Phone</h4><p>'.$phone.'</p>';

if ($decgoogresp->success == true)
 {
 // Success
   if(!$mail->send())
 {
       $msg = 'Message could not be sent.';
        $msgClass = 'Mailer Error: ' . $mail->ErrorInfo;
        } else {
             $msg = 'Your email has been sent';
              $msgClass = 'alert-success';
            }
            } else {
               $msg = "Please check the Captcha";
               $msgClass = 'alert-danger'; 
                   }
    }



First, you can do some basic form validity testing on the javascript side, during the form submit process. 首先,您可以在表单提交过程中在javascript方面进行一些基本的表单有效性测试。 If, for example, a required field is blank, you can return false; 例如,如果必填字段为空白,则可以return false;否则, return false; - which will stop the submit process and return control to the user. -这将停止提交过程并将控制权返回给用户。

Specifically for Google Recaptcha, Google has added a callback option for when the form has been checked. 专门针对Google Recaptcha,Google在检查表单时添加了一个回调选项。 See this answer for how to implement. 请参阅此答案以了解如何实施。

The more secure field validations, though, must be actioned on the PHP side (a savvy user, with DevTools, can fake the javascript validations and get the form to submit). 不过,必须在PHP方面采取更为安全的字段验证措施(精明的用户使用DevTools可以伪造javascript验证并提交表单)。 If any fields fail validation on the PHP side, there is only one option: send the form data back to the page and reconstruct the user's input. 如果任何字段在PHP端均未通过验证,则只有一个选择:将表单数据发送回页面并重建用户的输入。

Here is an example of simple form validation on the javascript side, for your reference. 这是 javascript端的简单表单验证示例 ,供您参考。

References: 参考文献:

Add a callback to Google recaptcha to check if user checked the box 向Google Recaptcha添加回调以检查用户是否选中了该框

MDN article on form validation - note the SmashingMagazine links at bottom 有关表单验证的MDN文章-请注意底部的SmashingMagazine链接

TutorialsPoint - more concise example of the same TutorialsPoint-相同示例的更简洁示例

Video tutorial of same (30 min) 相同的视频教程(30分钟)

Javascript - check if Google Recaptcha was clicked Javascript-检查是否单击了Google Recaptcha

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM