HTML和PHP文件之间的动态表单验证。这可能吗?

时间:2017-11-01 23:27:53

标签: php html css forms dynamic

目前在我的网站JorgeGoris.com上我有一个表单指向一个名为form_process.php的php文件进行验证。我的问题是我从未看到我实现的验证或成功消息发生,因为当我点击提交时,我被重定向到form_process.php我希望这在同一页面中动态发生。我怎样才能做到这一点?这是我的表单和php代码:

<?php

// define variables and set to empty values
$firstName_error = $email_error = $phone_error = "";
$firstName = $lastName = $email = $phone = $message = $success = "";

//form is submitted with POST method
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["firstname"])) {
    $firstName_error = "First name is required";
  } else {
    $firstName = test_input($_POST["firstname"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$firstName)) {
      $firstName_error = "Only letters and white space allowed"; 
    }
  }

    if (empty($_POST["email"])) {
    $email_error = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $email_error = "Invalid email format"; 
    }
  }
  
  if (empty($_POST["phone"])) {
    $phone_error = "Phone is required";
  } else {
    $phone = test_input($_POST["phone"]);
    // check if e-mail address is well-formed
    if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$phone)) {
      $phone_error = "Invalid phone number"; 
    }
  }

  if (empty($_POST["message"])) {
    $message = "";
  } else {
    $message = test_input($_POST["message"]);
  }
  
  if ($firstName_error == '' and $email_error == '' and $phone_error == '' ){
      $message_body = '';
      unset($_POST['submit']);
      foreach ($_POST as $key => $value){
          $message_body .=  "$key: $value\n";
      }
      
      $to = 'J_goris@live.com';
      $subject = 'Potential Client/Employer-JorgeGoris';
      if (mail($to, $subject, $message_body)){
          $success = "Message sent, I'll get back to you shortly!";
          $firstName = $email = $phone = $message = '';
          echo "<h1>Got it! I'll get back to you shortly!</h1>";
      }
  }
  
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

?>
<form action="form_process.php" method="post" name="contact_form">
				<div class="form-row">
					<div class="col-lg-3 offset-lg-3 col-sm-12">
						<input type="text" class="form-control" placeholder="First Name *" name="firstname">
						<span class="error"><?php= $firstName_error ?></span>
					</div><!--end col-->
					<div class="col-lg-3 col-sm-12">
						<input type="text" class="form-control" placeholder="Last Name" name="lastname">
					</div><!--end col-->
				</div><!--end row-->
				<div class="form-row">
					<div class="col-lg-3 offset-lg-3 col-sm-12">
						<input type="text" class="form-control" placeholder="Phone Number" name="phone">
						<span class="error"><?php= $phone_error ?></span>
					</div><!--end col-->
					<div class="col-lg-3 col-sm-12">
						<input type="email" class="form-control" placeholder="Email *" name="email">
						<span class="error"><?php= $email_error ?></span>
					</div><!--end col-->
				</div><!--end row-->
				<div class="row">
					<div class="col-lg-5 offset-lg-3 col-sm-12">
						<textarea class="form-control" rows="5" placeholder="Tell me a little about your project. Budget details are also appreciated. *" name="message"></textarea>
					</div><!--end col-->
				</div><!--end row-->
				<div class="success"><?php= $success; ?></div>
				<button type="submit" class="button" name="submit">Submit</button>
			</form><!--end form-->

1 个答案:

答案 0 :(得分:0)

实现所需功能的正确方法是将表单数据发送到负责加载表单的PHP文件(或函数)。加载表单的PHP代码应该是有条件的。例如,您可以检查提交的后期数据中的错误,如果出现错误,除了表单的普通HTML代码外,还应输出一些显示错误的HTML代码。

在您的情况下,当您点击提交按钮时没有看到任何内容,因为表单指向的PHP脚本不会输出包含表单的页面的HTML。

(我无法给你一个代码示例,因为这个问题太笼统了。)