错误处理期间将重新显示不需要的字段

时间:2019-04-09 13:57:55

标签: javascript php html css ajax

我已经做到了,所以我有1种形式,其中包括一个隐藏的div,它带有单击“添加更多”按钮时显示的额外输入字段。提交带有无效数据的表单时,将重新显示报价表,其中包含有效数据以及无效数据的错误消息。

问题是,处理错误时,第一次进入页面时显示的原始3个字段会重新显示在“报价单”上方。

重新显示报价表时是否有办法隐藏这3个字段?

// Error checking. If there are errors, call the redisplayForm function to redisplay the Quote Form Handler.
if ($errorCount>0 || $errorCount<0) {
    redisplayForm($bizName, $bizType, $address1, $address2, $city, 
    $state, $zip, $sqft, $cName, $email, $bizNameErr, $bizTypeErr,
    $address1Err, $cityErr, $stateErr, $zipErr, $sqftErr, $cNameErr, $emailErr );

    $bizNameErr = $bizTypeErr = $address1Err = $address2Err = $cityErr = $stateErr
    = $zipErr = $sqftErr = $cNameErr = $emailErr = "";

    $bizName = $bizType = $address1 = $address2 = $city = $state
    = $zip = $sqft = $cName = $email = "";
// If there are no errors, an email will be sent to Conversion Worx with the user's input.
} else {
    $To = "myemail"; 
    $Subject = "Quote Form Results";
    $Message = "Business Name: " . $bizName . "\n" 
    . "Business Type: " . $bizType . "\n"
    . "Address Line 1: " . $address1 . "\n"
    . "Address Line 2: " . $address2 . "\n"
    . "City: " . $city . "\n"
    . "State: " . $state . "\n"
    . "Zip Code: " . $zip . "\n"
    . "Estimated Square Feet: " . $sqft . "\n"
    . "Contact Name: " . $cName . "\n"
    . "Email Address: " . $email;
    $result = mail($To, $Subject, $Message);
}
// If email to Conversion Worx is sent succesfully, send thank you email to user.
if (isset($result)) {
    $To = $email;
    $Subject = "Virtual Tour Quote Request";
    $Headers = 'From: myemail' . "\r\n" . 
        'Reply-To: myemail' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
    $Message = $cName . ",";
    $Message .= "\n" . "\n" . "Thank you for your interest in our 3D 360° Virtual Tours!";
    $Message .= "\n" . "\n" . "Your information has been submitted. ";
    $Message .= "A Conversion Worx represenative will be contacting you shortly to arrange a quote via phone or on-site visit.";
    $Message .= "\n" . "\n" . "We look forward to working with you! ";
    $Message .= "\n" . "\n" . "\n" . "Sincerely,";
    $Message .= "\n" . "\n" . "The Conversion Worx Team";
    $result = mail($To, $Subject, $Message, $Headers);
}
 ?>
<?php

$errorCount = "";

$bizNameErr = $bizTypeErr = $address1Err = $address2Err = $cityErr = $stateErr
= $zipErr = $sqftErr = $cNameErr = $emailErr = "";

$bizName = $bizType = $address1 = $address2 = $city = $state
= $zip = $sqft = $cName = $email = "";
// Check to make sure the required fields from the Quote Form are not empty
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["bizName"])) {
    $bizNameErr = "Business name is required";
    ++$errorCount;
  } else {
    $bizName = test_input($_POST["bizName"]);
  } 
  if (empty($_POST["cName"])) {
    $cNameErr = "Contact Name is required";
    ++$errorCount;
  } else {
    $cName = test_input($_POST["cName"]);
  }
  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
    ++$errorCount;
  } else {
    $email = test_input($_POST["email"]);
  }
  if (empty($_POST["bizType"])) {
    $bizTypeErr = "Business Type is required";
    ++$errorCount;
  } else {
    $bizType = test_input($_POST["bizType"]);
  }
  if (empty($_POST["address1"])) {
    $address1Err = "Address Line 1 is required";
    ++$errorCount;
  } else {
    $address1 = test_input($_POST["address1"]);
  }
   if (!empty($_POST["address2"])) {
    $address2 = test_input($_POST["address2"]);
  }
  if (empty($_POST["city"])) {
    $cityErr = "City is required";
    ++$errorCount;
  } else {
    $city = test_input($_POST["city"]);
  }
  if (empty($_POST["state"])) {
    $stateErr = "State is required";
    ++$errorCount;
  } else {
    $state = test_input($_POST["state"]);
  }
  if (empty($_POST["zip"])) {
    $zipErr = "Zip Code is required";
    ++$errorCount;
  } else {
    $zip = test_input($_POST["zip"]);
  }
  if (empty($_POST["sqft"])) {
    $sqftErr = "Square Feet is required";
    ++$errorCount;
  } else {
    $sqft = test_input($_POST["sqft"]);
  }   
}
// Form validation
function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

1 个答案:

答案 0 :(得分:0)

您有两种选择:在浏览器(通过javascript / jQuery)或服务器(PHP)中进行验证。

如果您选择PHP路由,则页面 将会更改或刷新,除非您采取特定步骤存储/检索,否则在字段中键入的所有数据都会丢失。 /替换它。

javascript / jQuery路由:

如果您选择JavaScript路由,则可以中断表单提交过程并处理最多(如果不是全部)表单字段验证。如果任何字段的验证失败,则不仅可以将所有数据仍保留在原位的控制权还给用户,还可以将他们的注意力转移到错误上(例如,方法是在错过的字段中添加一个引起注意的类,或者弹出模式对话框消息,或者将光标聚焦在错过的字段上,等等。

PHP路线:

如果选择PHP路由,如何保存/恢复用户数据?大量的体力劳动...您可以:

1)使用一些JavaScript将字段值存储到localStorage中。您需要进行大量编码,但是可以正常工作。同样,您将中断表单提交过程,并在提交表单之前立即存储数据。

2)在PHP中,接收所有字段值。如果有任何字段未通过验证,则可以将用户(通过PHP headers指令)发送回同一页或另一页,以及所有用户数据。这意味着要么创建一个新页面,要么将PHP代码添加到现有页面中以查找要发布的这些值,如果找到了这些值,则以显示已填充这些值的形式来构造页面该方法比第一种方法需要更多的工作。

坦率地说,完成您想要的最简单的方法是使用基于JavaScript的客户端进行字段验证-这就是99%的网站如何处理这种情况。优点远大于缺点。如果您担心使用F12 DevTools来欺骗您的字段验证的超级用户,那么您可以在PHP中进行一次备份检查,该检查不会打扰保留表单数据(正确使用它们吧!! )。属于黑客类别的人数如此之少,以至于您根本不需要迎合他们-但是您确实需要在PHP方面进行仔细检查,只是不必担心保存其数据。在javascript端注意这一点

请参阅以下资源:

How to direct a user directly to a form