该表单有效,但 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>
有人能帮忙吗?