0

フォームは機能しますが、true または false の値が「messagetxt」に入れられ、それがメールで受け取るメッセージです。無効なデータの継続的なアラート ボックスを作成する最も簡単な方法は、読み込み時に自動アラートを表示する 2 つ目の連絡先ページを作成することでした。

    //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 に答える
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 に答える