在html中发布一个带有javascript和php验证的表单..一个错误 - 返回true或false进入表单的文本框

Yve*_*omb 0 html javascript php forms post

表单有效,除了true或false的值,被放入'messagetxt',这是我通过电子邮件收到的消息.我发现为无效数据创建连续警报框的最简单方法是在加载时创建第二个带有自动警报的联系页面.

    //this is on page contact.html
//head etc..
<body>
<form  action="email.php" method="post" name="contact" >
          First Name:
          <input type="text" placeholder="First Name" name="fname" id="fname" />
          Last Name:
          <input type="text" placeholder="Last Name" name="lname" id="lname"/>
          Email Address:
          <input type="email" placeholder="Email Address" name="email"  id="email"/>
          Message:
          <input type="text" placeholder="Message" name="messagetxt" id="messagetxt"/>
          <input type="submit" value="submit" />
        </form>
        <script type="text/javascript">
  var inputElem = document.getElementById('fname'); 
        var inputElem = document.getElementById('lname');
          var inputElem = document.getElementById('email');
            var inputElem = document.getElementById('messagetxt');
             var form = document.getElementsByTagName('form')[0];
form.onsubmit = function (){
if (inputElem.value = '' || inputElem.value.length < 1){
        alert('Please complete all fields'); 
        return false; 
    }
};
</script>
</body>
</html>


//===
//this is on a page contacterror.html
//head etc..
<body>
<form  action="email.php" method="post" name="contact" >
          First Name:
          <input type="text" placeholder="First Name" name="fname" id="fname" />
          Last Name:
          <input type="text" placeholder="Last Name" name="lname" id="lname"/>
          Email Address:
          <input type="email" placeholder="Email Address" name="email"  id="email"/>
          Message:
          <input type="text" placeholder="Message" name="messagetxt" id="messagetxt"/>
          <input type="submit" value="submit" />
        </form>
        <script type="text/javascript">
  var inputElem = document.getElementById('fname'); 
        var inputElem = document.getElementById('lname');
          var inputElem = document.getElementById('email');
            var inputElem = document.getElementById('messagetxt');
             var form = document.getElementsByTagName('form')[0];
form.onsubmit = function (){
if (inputElem.value = '' || inputElem.value.length < 1){
        alert('Please complete all fields'); 
        return false; 
    }
};
</script>
<script type="text/javascript">
alert("Please complete all fields");
</script>
</body>
</html>


//===
//this is my email.php
<body>
<?php

function spamcheck($field)
  {
  //filter_var() sanitizes the e-mail
  //address using FILTER_SANITIZE_EMAIL
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);

  //filter_var() validates the e-mail
  //address using FILTER_VALIDATE_EMAIL
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
  else
    {
    return FALSE;
    }
  }

if ($_SERVER['REQUEST_METHOD'] == 'POST') {


    //process form data
  //check if the email address is invalid
  $mailcheck = spamcheck($_POST['email']);
  if ($mailcheck==FALSE)
    {
    header ("Location:contacterror.html");
            }

else
      {
  //send email
  $email=$_POST['email'];
  $fname=$_POST['fname'];
  $lname=$_POST['lname'];
  $message = $_POST['messagetxt'] ;
  $subject="website contact";
  mail("contact@yve3.com", $subject ,$message , "From: $email");
  echo "Thank you for using our mail form.";
  }

  }
else
  {//if "email" is not filled out, display the form
   header ("Location:contacterror.html");
        }

include ("contact.html");

?>
</body>
Run Code Online (Sandbox Code Playgroud)

有人能帮忙吗?

Ste*_*eel 7

你想看看你的javascript.它似乎导致消息文本框在重新加载页面时返回false.如果您删除了javascript或将其放在页面的头部,这应该解决问题.此外,应为要检索的每个元素更改变量inputElem.每次调用getElementById()时,您的变量都会覆盖它自己.尝试这样的事情:

    <!doctype>
<html>
<head>
    <meta charset='utf-8'>
    <script type="text/javascript">

        function Validate()
        {
            // create array containing textbox elements
            var inputs = [document.getElementById('fname'), document.getElementById('lname'), document.getElementById('email'), document.getElementById('messagetxt')];

            var error;

            for(var i = 0; i<inputs.length; i++)
            // loop through each element to see if value is empty
            {
                if(inputs[i].value == '')
                {
                    error = 'Please complete all fields.';
                }
            }
            if(error)
            {
                alert(error);
                return false;
            }
        }
</script>
</head>
<body>
<form action='email.php' method="POST" name="contact" >
          First Name:
          <input type="text" placeholder="First Name" name="fname" id="fname" />
          Last Name:
          <input type="text" placeholder="Last Name" name="lname" id="lname"/>
          Email Address:
          <input type="email" placeholder="Email Address" name="email"  id="email"/>
          Message:
          <input type="text" placeholder="Message" name="messagetxt" id="messagetxt"/>
          <input type="submit" value="submit" onClick="return Validate();"/>
        </form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)


Ami*_*mir 5

使用 <input type="submit" value="submit" name="submit"/>

并且也取代

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
Run Code Online (Sandbox Code Playgroud)

if (isset($_POST['submit'])) {
Run Code Online (Sandbox Code Playgroud)

首先if检查您的表单方法是否正确POST.如果用户使用按下按钮提交表单,则没有必要意味着.submit但是第二次if检查用户是否真的使用按下submit按钮提交表单