PHP电子邮件验证无法正常工作

时间:2017-11-09 01:55:54

标签: javascript php jquery html css

我使用单选按钮显示表单的文本框。我添加了一个PHP脚本来验证电子邮件,并在完成后打开另一个页面。但似乎没有任何效果。它显示文本框为空时的错误消息,即使没有按下提交按钮,如果电子邮件格式不正确,也会刷新页面。

这是我的代码:

<?php

// define variables and set to empty values
$emailErr = "";
$email = "";

if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format"; 
    }
    else{
        header("Location:create.php")
    }
  }

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


?>

&#13;
&#13;
function ShowHideDiv() {
  var chkYes1 = document.getElementById("chkYes1");
  var dvtext1 = document.getElementById("dvtext1");
  var chkYes2 = document.getElementById("chkYes2");
  var dvtext2 = document.getElementById("dvtext2");
  var button = document.getElementById("button");
  dvtext1.style.display = chkYes1.checked || chkYes2.checked ? "block" : "none";
  dvtext2.style.display = chkYes2.checked ? "block" : "none";
  btn1.style.display = chkYes1.checked ? "block" : "none";
  btn2.style.display = chkYes2.checked ? "block" : "none";
}
&#13;
<div class="content login-container">
  <div class="form-login">
     <label for="chkYes1">
        <input type="radio" id="chkYes1" name="chk" onclick="ShowHideDiv()" />
        I am new on Swadonline
      </label>
      <br>
      <br>
      <div id="dvtext1" style="display: none" class="lgntxt">
        Please enter your Email Address *
        <br>
        <input type="text" id="txtBox" name="email" />
      </div>
      <button style="display: none" id="btn1" class="btn third" type="submit" name="submit" value="Submit">CONTINUE</button>
  </div>
</div>
  
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

在html中更改此行

<button style="display: none" id="btn1" class="btn third" type="submit" name="submit" value="Submit">CONTINUE</button>

<input type="submit"  style="display: none" id="btn1" class="btn third"  name="my_form_submit" value="CONTINUE">

现在你的php更改如下:

<?php

// define variables and set to empty values
$emailErr = "";
$email = "";
if(isset($_POST['my_form_submit'])){ // only check validation if form has been submitted, else don't validate

    if (empty($_POST["email"])) {
        $emailErr = "Email is required";
      } else {
        $email = test_input($_POST["email"]);
        // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
          $emailErr = "Invalid email format"; 
        }
        else{
            header("Location:create.php");
        }
      }

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

?>

答案 1 :(得分:0)

我曾经采用这条路线提交表格,从那以后我强烈建议为任何项目设置PHPMailer

  • 对于电子邮件部分:PHPMailer相对容易设置,从不让我失望。
  • 对于验证,您所要做的就是为表单添加“必需”属性,据我所知,浏览器将完成剩下的工作:)


希望这在任何方面都有帮助!