我的网站上有一个包含此代码的表单
<article id="contact" class="panel">
<header>
<h2>Contact Me</h2>
</header>
<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d2445.617108940998!2d5.3693015999999965!3d52.19583220000001!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x47c646d54f5f74b3%3A0x24f74fcdc9277232!2sRabouwgaarde%2C+3824+Amersfoort!5e0!3m2!1snl!2snl!4v1426155136287" width="400" height="300" frameborder="0" style="border:0" align="right"></iframe>
<P STYLE="text-align: left;">Bedankt voor het bekijken van mijn website. Wilt u contact met mij opnemen vul dan het contactformulier hier beneden in.</P>
<form name="contactform" method="post" action="send_form_email.php">
<div>
<div class="row">
<div class="6u">
<input type="text" name="first_name" placeholder="Name" />
</div>
<div class="6u">
<input type="text" name="email" placeholder="Your Email" />
</div>
</div>
<div class="row">
<div class="12u">
<input type="text" name="subject" placeholder="Subject" />
</div>
</div>
<div class="row">
<div class="12u">
<textarea name="message" placeholder="Message" rows="8"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<input type="submit" value="Submit" />
</div>
</div>
</div>
</form>
</article>
并且包含此php以便将电子邮件发送到我的电子邮件地址
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "contact@sandergouman.nl";
$email_subject = "Your email subject line";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['email']) ||
!isset($_POST['subject']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['subject']; // not required
$comments = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
/*if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
*/
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
/* $email_message .= "Last Name: ".clean_string($last_name)."\n";*/
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Subject: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
alert("succes! we will be in contact with your shortly")
<?php
}
?>
但是现在当我按下提交时,它会转到php页面并显示: 警报(“成功!我们将很快与你联系”)
我想要做的是当我按下提交按钮并且一切正常时我只是在同一页面上收到警报,我说我们很快会与您联系。
在javascript中是window.alert(“sometext”); 但是当我按下提交按钮时,我如何在这里使用它
如果有人按下提交按钮并且出现错误,我如何使用此框?
答案 0 :(得分:0)
您可以轻松检查邮件是否已成功发送,请查看下面的代码段。
if(@mail($email_to, $email_subject, $email_message, $headers)) {
//Mail has been sent succesfully
echo "<script>window.alert('Mail has been sent succesfully')";
} else {
//Something went wrong with emailing
echo "<script>window.alert('Mail could not be sent!')";
}
答案 1 :(得分:0)
将onsubmit
属性添加到表单并创建一个javascript函数来处理所有逻辑。请务必return validate()
,以便从false
返回validate
时,表单将停止提交:
<script>
function validate() {
var valid = false;
//do some validation here.
if (valid) {
alert('sometext');
return true;
}
return false;
}
</script>
<form name="contactform" onsubmit="return validate();" method="post" action="send_form_email.php">
为简单起见,我使用了onsubmit
属性,但应该注意的是,有更好的方法可以做到这一点,例如使用事件处理程序。
答案 2 :(得分:0)
您可以使用javascript检查点击提交按钮的时间。下一行就是您拥有的那条:
<form name="contactform" method="post" action="send_form_email.php">
添加javascript验证,如下一行:
<form name="contactform" method="post" action="send_form_email.php"
onsubmit="return check_everything();" >
现在,在您的javascript部分中,声明方法:
<script type="text/javascript">
function check_everything () {
var name = document.getElementsByName("first_name");
if ( name.value.length == 0 )
{ alert("Error: enter first name.");
return false;
}
alert("we will be in contact with you shortly.");
return true;
}
</script>
我只检查名称,您可以检查所有字段。如果出现任何错误,将显示警告窗口,方法将返回FALSE。如果一切正常,则显示另一个警报,它将返回TRUE,在这种情况下,将提交FORM。
请记住,这是客户端,如果您想在服务器端进行检查,请使用Jordy的解决方案。