用户单击提交后,页面将转到php文件.它如何保留在HTML文件中并将表单信息提交到电子邮件中
After the user clicks submit, the page goes to the php file. How can it remain on the HTML file and submit the form information to the email?
我是PHP的初学者,正在处理一个联系人表单。
选择提交按钮后,不会显示响应消息。页面将转到PHP文件,并且永远不会发送电子邮件。此外,不会显示任何错误消息。
我使用的是validate.js和Jquery form.js。以下是参考链接:http://www.tutwow.com/htmlcss/create-a-simple-and-secure-contact-form-with-jquery-and-php/
联系表格的风格与Clicky Media页面上的风格相似(点击在网站上获得报价)HTML:
<div class='contentform'>
<form id="contact_form" action="contactform.php" method="post" novalidate="nonvalidate">
<div class="form">
<select>
<option value="Select Topic">--Select Topic--</option>
<option value=""></option>
<option value=""></option>
<option value=''></option>
</select>
<input kl_virtual_keyboard_secure_input="on" id="overlay_name" name="overlay_name" value="First Name" onfocus="if (this.value == 'First Name') {this.value = '';}" onblur="if (this.value == '') {this.value = 'First Name';}" type="text"></input>
<input kl_virtual_keyboard_secure_input="on" id="overlay_name2" name="overlay_name2" value="Last Name" class="right" onfocus="if (this.value == 'Last Name') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Last Name';}" type="text"></input>
<input kl_virtual_keyboard_secure_input="on" id="overlay_telephone" name="overlay_telephone" value="Phone" class="left" onfocus="if (this.value == 'Phone') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Phone';}" type="text"></input>
<input kl_virtual_keyboard_secure_input="on" id="overlay_email" name="overlay_email" value="Email" class="right" onfocus="if (this.value == 'Email') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Email';}" type="email"></input>
<textarea style="overflow: hidden; word-wrap: break-word; resize: none; height: 27px;" rows="1" id="overlay_message" name="overlay_message" onfocus="if (this.value == 'Message') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Message';}"> Message </textarea>
</div>
<div class='formfooter'>
<input id="send" type='submit' name='sumbit' value ='send message'></input>
</div>
</form>
<div id="response"></div>
</div>
Javascript:
$('#contactform').validate({
// Specify what the errors should look like
// when they are dynamically added to the form
errorElement: "input",
errorPlacement: function(error, element) {
error.insertBefore( element.parent().parent() );
error.wrap("<input class='ErrorField'></input>");
},
// Add requirements to each of the fields
rules: {
overlay_name: {
required: true,
minlength: 3
},
overaly_name2:{
required:true,
minlength:3
},
overlay_email: {
required: true,
email: true
},
overlay_telephone:{
required:true,
minlength:10,
expression: "if (VAL != 'Phone') return true; else return false;",
},
overlay_message: {
required: true,
minlength: 10
}
},
// Specify what error messages to display
// when the user does something horrid
messages: {
overlaye_name: {
required: "Please enter your name.",
minlength: jQuery.format("At least {0} characters required.")
},
overlaye_name2: {
required: "Please enter your last name.",
minlength: jQuery.format("At least {0} characters required.")
},
overlay_email: {
required: "Please enter your email.",
email: "Please enter a valid email."
},
overlay_telephone:{
required:"Please enter telephone number.",
phone:"Please enter valid number."
},
overlay_message: {
required: "Please enter a message.",
minlength: jQuery.format("At least {0} characters required.")
}
},
// Use Ajax to send everything to processForm.php
submitHandler: function(form) {
$("#send").attr("value", "Sending...");
$(form).ajaxSubmit({
target: "#response",
success: function(responseText, statusText, xhr, $form) {
$("#response").html(responseText).hide().slideDown("fast");
}
});
return false;
}
});
PHP:
// Clean up the input values
foreach($_POST as $key => $value) {
if(ini_get('magic_quotes_gpc'))
$_POST[$key] = stripslashes($_POST[$key]);
$_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}
// Assign the input values to variables for easy reference
$name=$_POST["overlay_name"];
$name2=$_POST["overlay_name2"];
$telephone=$_POST["overlay_telephone"];
$email=$_POST["overlay_email"];
$message=$_POST["overlay_message"];
// Test input values for errors
$errors = array();
if(strlen($name) < 2) {
if(!$name) {
$errors[] = "You must enter a name.";
} else {
$errors[] = "Name must be at least 2 characters.";
}
}
if(!$email) {
$errors[] = "You must enter an email.";
} else if(!validEmail($email)) {
$errors[] = "You must enter a valid email.";
}
if(strlen($message) < 10) {
if(!$message) {
$errors[] = "You must enter a message.";
} else {
$errors[] = "Message must be at least 10 characters.";
}
if($errors) {
// Output errors and die with a failure message
$errortext = "";
foreach($errors as $error) {
$errortext .= "<li>".$error."</li>";
}
die("<span class='failure'>The following errors occured:<ul>". $errortext ."</ul></span>");
}
$to = "email address here";
$subject = "Contact Form: $name";
$message = "$message";
$headers = "From: $email";
mail($to, $subject, $message, $headers);
// Die with a success message
die("<span class='success'>Success! Your message has been sent.</span>");
// A function that checks to see if
// an email is valid
function validEmail($email)
{
$isValid = true;
$atIndex = strrpos($email, "@");
if (is_bool($atIndex) && !$atIndex)
{
$isValid = false;
}
else
{
$domain = substr($email, $atIndex+1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64)
{
// local part length exceeded
$isValid = false;
}
else if ($domainLen < 1 || $domainLen > 255)
{
// domain part length exceeded
$isValid = false;
}
else if ($local[0] == '.' || $local[$localLen-1] == '.')
{
// local part starts or ends with '.'
$isValid = false;
}
else if (preg_match('/''.''./', $local))
{
// local part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^[A-Za-z0-9''-''.]+$/', $domain))
{
// character not valid in domain part
$isValid = false;
}
else if (preg_match('/''.''./', $domain))
{
// domain part has two consecutive dots
$isValid = false;
}
else if(!preg_match('/^(''''.|[A-Za-z0-9!#%&`_=''/$''*+?^{}|~.-])+$/',
str_replace("''''","",$local)))
{
// character not valid in local part unless
// local part is quoted
if (!preg_match('/^"(''''"|[^"])+"$/',
str_replace("''''","",$local)))
{
$isValid = false;
}
}
if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
{
// domain not found in DNS
$isValid = false;
}
}
return $isValid;
}
您根本没有将submitHandler
与<form>
的submit
事件连接起来。这样做:
$(function () {
$("#contactForm").submit(submitHandler);
});
相关文章:
- 我想使用模态通过php文件发送邮件,并且我希望在提交关闭后关闭pop
- 使用ajax上传的文件返回错误,但使用c#在asp.net的服务器端正常提交的文件
- Firefox,如何提交表单触发文件下载,但停留在网页上
- html5拖放文件-在提交整个表单时上传
- javascript下载一个文件并执行提交操作
- 如何在不重新加载页面的情况下选择文件提交表单
- 应将缩小的文件提交到源代码管理中
- Django View 无法使用请求获取文件.来自 ajax 表单的文件提交
- JQuery 通过表单输入类型=“文件”提交后检测插入的图像
- 如何使用jquery发布文件提交
- 使用JSP从外部Javascript文件提交变量
- Fin上传程序在用户对文件提交做出响应后取消上传
- 用javascript将输入文件提交到服务器端
- 向php文件提交angular表单
- php文件中的多个表单和文件提交
- jquery验证表单字段,但不向目标文件提交表单数据
- 文件提交有问题
- Javascript文件提交导致php脚本服务器错误
- 从Angular应用到Express Web Server的文件提交
- 剑道文件上传并没有将所有上传的文件提交给控制器以保存在IE11上