0

该表单有效,但 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>

有人能帮忙吗?

4

3 回答 3

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>
于 2013-05-11T06:55:58.073 回答
5

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

并替换

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

if (isset($_POST['submit'])) {

首先if检查您的表单方法是否为POST. 这并不一定意味着用户是否通过按下按钮提交表单。submit但是第二次if检查用户是否真的通过按下submit按钮提交表单

于 2013-05-11T03:23:25.073 回答
2

多亏了 Steel 和 Amir,我纠正了我的代码,这更整洁,更简单,更有效。

我想我会发布这个,因为它对其他人有用。

这提供了客户端和服务器端检查以防止向电子邮件地址发送垃圾邮件。我删除了多余的错误处理,删除了多余的 html,强制发出错误警报。

//javascript in head with loop to check fields 

 <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 != null)
            {
              alert(error);
                return false;
            }
        }
</script>
</head>    

//amended form
<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' - client side check for spam
          <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" name="submit" onclick="Validate ()" />
        </form>

        </body>
</html>

//amended php

<body>
<?php

//==
//server side check for spam
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 (isset($_POST['submit'])) {

    //process form data
  //check if the email address is invalid
  $mailcheck = spamcheck($_POST['email']);
 if ($mailcheck==TRUE){

  //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.";
 }}

include ("contact.html");

?>
</body>
</html>
于 2013-05-11T11:10:12.827 回答