我在我的智慧与努力获得这种形式的工作权结束。基本上,当你点击提交时,它会停留大约5秒钟,然后进入感谢页面,并且用户通常认为他们没有点击右键并尝试再次提交至少3次。在此之后,我最终收到4份意见书,当时我只想要一份意见书。我知道这个问题已经被问了很多,但是我发现似乎没有任何解决方案可行,因为这个“表单”似乎没有表单标签,只是输入。避免双重提交的Form
此着陆页是以前的网页设计师,谁我假设只是用来自上帝的模板知道在哪里,我一直负责得到它与我的非常有限的知识/经验的工作权利的工作。我喜欢它的外观和功能,但存在上述问题。
这里的HTML:
<div class="col-sm-4" id="join-us-form">
<div class="form-bg">
<div class="form-arrow"></div>
<h3>title</h3>
<div id="join-us-results"></div>
<!-- Form -->
<form method="POST" action="join_us.php">
<div class="form-group">
<input type="text" name="first_name" id="first_name" class="form-control f-input input-field" placeholder="First Name" />
</div>
<div class="form-group">
<input type="text" name="last_name" id="last_name" class="form-control f-input input-field" placeholder="Last Name" />
</div>
<div class="form-group">
<input type="text" name="company" id="company" class="form-control f-input input-field" placeholder="Company" />
</div>
<div class="form-group">
<input type="text" name="email" class="form-control f-input input-field" placeholder="Email" />
</div>
<div class="form-group">
<input type="text" name="phone" maxlength="15" class="form-control f-input input-field" placeholder="Phone" />
</div>
<input type="submit" class="submit-btn" value="Submit" id="submit_btn" onsubmit="document.getElementById('submit_btn').disabled=true; document.getElementById('submit_btn').value='Submitting, please wait...';" />
</form>
</div>
</div>
这里是JavaScript的:
/* ==========================
JOIN-US FORM
=============================*/
$('#submit_btn').on('click', function() {
var proceed = true;
//simple validation at client's end
//loop through each field and we simply change border color to red for invalid fields
$("#join-us-form input[required=true]").each(function(){
$(this).css('border-color','');
if(!$.trim($(this).val())){ //if this field is empty
$(this).css('border','solid 1px red'); //change border color to red
proceed = false; //set do not proceed flag
}
//check invalid email
var email_reg = /^([\w-\.][email protected]([\w-]+\.)+[\w-]{2,4})?$/;
if($(this).attr("type")=="email" && !email_reg.test($.trim($(this).val()))){
$(this).css('border','solid 1px red'); //change border color to red
proceed = false; //set do not proceed flag
}
});
if(proceed) //everything looks good! proceed...
{
//get input field values data to be sent to server
var post_data = {
'first_name' : $('input[name=first_name]').val(),
'last_name' : $('input[name=last_name]').val(),
'user_email' : $('input[name=email]').val(),
'phone_number' : $('input[name=phone]').val(),
'company' : $('input[name=company]').val()
};
//Ajax post data to server
$.post('join_us.php', post_data, function(response){
if(response.type == 'error'){ //load json data from server and output message
var output = '<div class="error">'+response.text+'</div>';
}else{
window.location.href = "http://url.com/landing/thank-you.html";
//output = '<div class="success">'+response.text+'</div>';
//reset values in all input fields
$("#join-us-form input[required=true]").val('');
}
$("#join-us-results").hide().html(output).slideDown();
}, 'json');
}
});
而这里的PHP:?
<?php
if($_POST)
{
$to_email = "[email protected]"; //Recipient email, Replace with your own email.
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
$output = json_encode(array(//create JSON data
'type'=>'error',
'text' => 'Sorry Request must be Ajax POST'
));
die($output); //exit script outputting json data
}
//Sanitize input data using PHP filter_var().
$first_name = filter_var($_POST["first_name"], FILTER_SANITIZE_STRING);
$last_name = filter_var($_POST["last_name"], FILTER_SANITIZE_STRING);
$user_email = filter_var($_POST["user_email"], FILTER_SANITIZE_EMAIL);
$phone_number = filter_var($_POST["phone_number"], FILTER_SANITIZE_NUMBER_INT);
$company = filter_var($_POST["company"], FILTER_SANITIZE_STRING);
$subject = "Lead/url.com";
//additional php validation
if(strlen($first_name)<4){ // If length is less than 4 it will output JSON error.
$output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
die($output);
}
if(strlen($last_name)<4){ // If length is less than 4 it will output JSON error.
$output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
die($output);
}
if(strlen($company)<3){ //check emtpy company
$output = json_encode(array('type'=>'error', 'text' => 'Please enter a Company Name.'));
die($output);
}
if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)){ //email validation
$output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
die($output);
}
if(!filter_var($phone_number, FILTER_SANITIZE_NUMBER_FLOAT)){ //check for valid numbers in phone number field
$output = json_encode(array('type'=>'error', 'text' => 'Enter only digits in phone number'));
die($output);
}
//email body
$message_body = "From : ".$first_name."\r\nEmail : ".$user_email."\r\nPhone Number : (".$phone_number.")". $phone_number."\r\nCompany: ".$company ;
//proceed with PHP email.
$headers = 'From: '.$first_name.'' . "\r\n" .
'Reply-To: '.$user_email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$send_mail = mail($to_email, $subject, $message_body, $headers);
//header('Location: thank-you.html');
if(!$send_mail)
{
//If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
$output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
die($output);
}else{
$output = json_encode(array('type'=>'message', 'text' => 'Hi '.$first_name .' ! Look to hear from us soon!'));
die($output);
}
} >
的只有那些有效的工作是在提交输入中添加一个onclick,以防止它再次被提交,但是如果验证失败,那么它会卡住,并且用户无法在不刷新页面的情况下提交它。
我是新来的这一切,所以帮助和耐心将不胜感激。
在此先感谢。
如此简单,只需在第一次点击后禁用提交按钮,并启用它,如果它返回任何验证消息。 –