我在同一页面上使用PHP获取HTML表单的以下代码。实际上,问题在于,当任何人打开网站时,它会自动运行PHP Mail()
脚本并向我发送一封空邮件。
我希望它只在某人成功提交表单时才能工作。请帮忙。
<!--HTML FORM--><form method="post" name="contactform" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" onsubmit="return validateform()">
<input type="text" name="fullname" class="fullname" placeholder="Full Name*"
onfocus="this.placeholder = '';" onblur="if (this.placeholder == '') {this.placeholder = 'Full Name*';}">
<input type="text" name="email" class="email" placeholder="Email Address*"
onfocus="this.placeholder = '';" onblur="if (this.placeholder == '') {this.placeholder = 'Email Address*';}">
<textarea placeholder="Your Message*:" name="message" class="message"
onfocus="this.placeholder = '';" onblur="if (this.placeholder == '') {this.placeholder = 'Your Message*';}"></textarea>
<input type="submit" name="submit" value="Submit" />
</form><!--/HTML FORM> <!--Contact Form PHP-->
<?php
// define variables and set to empty values
$fullname = $email = $message = "";
$subject="Message from Website Visitor:".test_input($_POST["fullname"]);
$to="camadhusudanmishra@gmail.com";
$headers = "From:".test_input($_POST["email"]);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$fullname = test_input($_POST["fullname"]);
$email = test_input($_POST["email"]);
$message = test_input($_POST["message"]);}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;}
$message = str_replace("\n.", "\n..", $message);
mail($to,$subject,$message,$headers);
?>
<!--/Contact Form PHP-->
验证的JS代码:
function formValidation() {
var fullname = document.contactform.fullname;
var email = document.contactform.email;
var message = document.contactform.message;
if(validateName(fullname))
{
if(validateEmail(email))
{
if(validateMessage(message))
{
}
}
}
return false;
}
function validateName(fullname)
{
var letters = /^[A-Za-z]+$/;
if(fullname.value.match(letters) && fullname.value.length >= 3 && fullname.value.length <= 30)
{
return true;
}
else {
alert('Please enter a valid name');
return false;
}
}
function validateEmail(email)
{
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if(email.value.match(mailformat))
{
return true;
}
else
{
alert("You have entered an invalid email address!");
return false;
}
}
function validateMessage(message)
{
if(message.value.length >=5 && message.value.length <=300)
{
alert('Form Succesfully Submitted');
window.location.reload();
return true;
}
else
{
alert("Please type a valid message.");
return false;
}
}
答案 0 :(得分:0)
您可以使用以下代码。
<?php
if(isset($_POST['submit']))
{
// Add here function
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;}
// define variables and set to empty values
$fullname = $email = $message = "";
$subject="Message from Website Visitor:".test_input($_POST["fullname"]);
$to="camadhusudanmishra@gmail.com";
$headers = "From:".test_input($_POST["email"]);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$fullname = test_input($_POST["fullname"]);
$email = test_input($_POST["email"]);
$message = test_input($_POST["message"]);}
$message = str_replace("\n.", "\n..", $message);
mail($to,$subject,$message,$headers);
}
?>
<!--/Contact Form PHP-->
<!--HTML FORM-->
<form method="post" name="contactform" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" onsubmit="return validateform()">
<input type="text" name="fullname" class="fullname" placeholder="Full Name*"
onfocus="this.placeholder = '';" onblur="if (this.placeholder == '') {this.placeholder = 'Full Name*';}">
<input type="text" name="email" class="email" placeholder="Email Address*"
onfocus="this.placeholder = '';" onblur="if (this.placeholder == '') {this.placeholder = 'Email Address*';}">
<textarea placeholder="Your Message*:" name="message" class="message"
onfocus="this.placeholder = '';" onblur="if (this.placeholder == '') {this.placeholder = 'Your Message*';}"></textarea>
<input type="submit" name="submit" value="Submit" />
</form><!--/HTML FORM> <!--Contact Form PHP-->