HTML与ajax和PHP验证的联系
我是新的网站开发,最近我试图建立自己的网站。我有一个单页网站,我试图在不刷新页面的情况下合并一个联系人表单,并使用PHP服务器端验证提供客户端验证,最后在表单完成时给出一个平滑的成功消息 我现在面临的问题是页面重定向到contact.php,其中显示一个白色页面,然后不推送电子邮件。请帮我找出我犯的错误。下面是我在这个网站上使用的代码。请帮忙 Index.htmlHTML与ajax和PHP验证的联系,php,jquery,ajax,html,contact-form,Php,Jquery,Ajax,Html,Contact Form,我是新的网站开发,最近我试图建立自己的网站。我有一个单页网站,我试图在不刷新页面的情况下合并一个联系人表单,并使用PHP服务器端验证提供客户端验证,最后在表单完成时给出一个平滑的成功消息 我现在面临的问题是页面重定向到contact.php,其中显示一个白色页面,然后不推送电子邮件。请帮我找出我犯的错误。下面是我在这个网站上使用的代码。请帮忙 Index.html <div class="row"> <div class="span9"> <div
<div class="row">
<div class="span9">
<div class="response"></div>
<form id="contact-form" class="contact-form" action="contact.php" autocomplete="off" name=postlink method="post">
<p class="contact-name">
<input id="name" type="text" placeholder="Name" value="" name="name" required />
</p>
<p class="contact-email">
<input id="email" type="email" placeholder="Email Address" value="" name="email" required />
</p>
<p class="contact-phone">
<input id="phone" type="tel" placeholder="Mobile Number" maxlength="10" value="" name="phone" required />
</p>
<p class="contact-lead">
<input id="lead" type="text" placeholder="How did you hear about us" value="" name="lead" required />
</p>
<p class="contact-message">
<textarea id="message" placeholder="Tell Us More About You" name="message" rows="10" cols="40" required ></textarea>
</p>
<p class="contact-submit">
<input id="submit" type="submit" class="submit" href=# onclick="submitPostLink()" value="Send Your Email"></p>
</p>
</form>
</div>
我似乎找不到你的
submitPostLink()
只要从submithandler:
返回false,它就不会重定向到contact.php
亲爱的@Baig,请找到我在index.html函数submitPostLink(){document.postlink.submit();}中添加的submitPostPostLink()
的javascript代码
@dreamweiver你能解释一下吗?抱歉,我无法理解。@新Web开发人员:只需在document.postlink.submit()之后添加此返回语句即可代码>内部submitPostLink()
,类似于函数submitPostLink(){document.postlink.submit();return false;}
。这将阻止页面重定向
<?php
$to = 'info@YYYY.com';
$subject = 'Website Request';
$headers = "From: " . strip_tags($_POST['email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['email']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['name']) . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
$message .= "<tr><td><strong>Mobile Number:</strong> </td><td>" . strip_tags($_POST['phone']) . "</td></tr>";
$message .= "<tr><td><strong>How did you Find Us:</strong> </td><td>" . strip_tags($_POST['lead']) . "</td></tr>";
$message .= "<tr><td><strong>More About:</strong> </td><td>" . $_POST['message'] . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
if($_POST)
{
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$lead = $_POST['lead'];
$message = $_POST['message'];
// Full Name
if (eregi('^[A-Za-z0-9 ]{3,20}$',$name))
{
$valid_name=$name;
}
else
{
$error_name='Please Enter a valid Name.';
}
// Email
if (eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$', $email))
{
$valid_email=$email;
}
else
{
$error_email='Please Enter your correct Email.';
}
// Phone Number Validation
if (eregi('/^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/$',$phone))
{
$valid_phone=$phone;
}
else
{
$error_phone='Please Enter your valid Mobile Number';
}
// Lead
if (eregi('^[A-Za-z0-9 ]{3,20}$',$lead))
{
$valid_lead=$lead;
}
else
{
$error_lead='Please Let us know how you found us';
}
// Gender
if ($message==0)
{
$error_message='Please let us know more';
}
else
{
$valid_message=$message;
}
if((strlen($valid_name)>0)&&(strlen($valid_email)>0)&&(strlen($valid_phone)>0)&&(strlen($valid_lead)>0) && $valid_message>0 )
{
mail($to, $subject, $message, $headers);
echo "Thank You $name for contacting us, we will revert back to you with your request within 12 hours!";
}
else{ }
}
?>
<script type="text/javascript">
$('document').ready(function()
{
$('#contact-form').validate(
{
rules:
{
"name":{
required:true,
maxlength:30
},
"email":{
required:true,
email:true,
maxlength:100
},
"phone":{
required:true,
maxlength:10
},
"lead":{
required:true,
},
"message":{
required:true
}},
messages:
{
"name":{
required:"This field is required"
},
"email":{
required:"This field is required",
email:"Please enter a valid email address"
},
"phone":{
required:"This field is required"
},
"lead":{
required:"This field is required"
},
"message":{
required:"This field is required"
}},
submitHandler: function(contact-form){
$(contact-form).ajaxSubmit({
target: '#response',
success: function() {
$('#span9').slideUp('fast');
}
});
}
})
});
</script>
#contact-form {
margin-bottom: 0;
}
#contact-form p {
margin-bottom: 5px;
}
#contact-form error {
font-weight: bold;
font-size: 1.2em;
color: #9d3131;
}
#contact-form input,
#contact-form textarea {
border: none;
-webkit-border-radius: 0;
-moz-border-radius: 0;
border-radius: 0;
-webkit-box-shadow: none;
-moz-box-shadow: none;
box-shadow: none;
background: #26292E;
color: #FFFFFF;
font-size: 16px;
height: auto;
padding: 15px;
margin: 0;
outline-style:dotted;
outline-color:#09F;
resize: none;
}
#contact-form input {
width: 100%;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
#contact-form input:focus, #contact-form textarea:focus {
background: #26292E;
box-shadow: 0 0 3px #aaa;
}
#contact-form textarea {
width: 100%;
resize: vertical;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
#contact-form .submit {
background: #26292E;
color: #7F8289;
cursor: pointer;
display: inline-block;
font-size: 18px;
font-weight: 500;
padding: 16px 40px;
text-align: center;
vertical-align: middle;
width: auto;
margin-top: 10px;
-webkit-transition: background 0.1s linear 0s, color 0.1s linear 0s;
-moz-transition: background 0.1s linear 0s, color 0.1s linear 0s;
-o-transition: background 0.1s linear 0s, color 0.1s linear 0s;
transition: background 0.1s linear 0s, color 0.1s linear 0s;
}
#contact-form .submit:hover {
background: #09F;
color: #FFFFFF;
}
#response {
margin-top: 10px;
color: #9d3131;
}