2016-08-02 168 views
0

我在我的智慧與努力獲得這種形式的工作權結束。基本上,當你點擊提交時,它會停留大約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,以防止它再次被提交,但是如果驗證失敗,那麼它會卡住,並且用戶無法在不刷新頁面的情況下提交它。

我是新來的這一切,所以幫助和耐心將不勝感激。

在此先感謝。

+3

如此簡單,只需在第一次點擊後禁用提交按鈕,並啓用它,如果它返回任何驗證消息。 –

回答

0

嗨只是disablesubmit button當表單提交

$("form").submit(function() { 
    $(this).find('input[type="submit"]').prop("disabled", true); 
}) 
0

正如你所說,你可以啓用,並根據用戶操作和從服務響應禁用按鈕。

你可以改變一樣,您的請求部分;

if(proceed) //everything looks good! proceed... 
    { 

     $('#submit_btn').prop('disabled', true); 
     /* Disable button on request start */ 

     //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(''); 
      } 

      $('#submit_btn').prop('disabled', false); 
      /* Enable button again on request end */ 

      $("#join-us-results").hide().html(output).slideDown(); 
     }, 'json'); 
    } 
+0

工作,謝謝!你是一個拯救生命的人! – hughesj

0

您可以使用AJAX

beforeSend: function(msg){ 
    $(".button").button("disable"); 
    } 

禁用提交按鈕,成功後啓用。

0

您可以指定一個真正的變量,當它是真的防止重新提交那樣簡單。

0

你可以從字面上添加到您的JS:

form.myButton.disabled = true; 
    return true; 

,然後你的表單標籤附加內:

onsubmit="myButton.disabled 

要使用myButton的你還需要添加到窗體name="myButton"然後包裝一個if isset($_POST['myButton'])) { // code }

0

不知道,像設置禁用道具可能會奏效。

jQuery(document).ready(function($) { 
    $('#join-us-form').find('form').on('submit', function(e) { 
     e.preventDefault(); 
     if (!$(this).find('input[type="submit"]').prop('disabled')) { 
      $.ajax({ 
       url: '/path/to/foo.php', 
       method: 'POST', 
       dataType: 'json', // doesn't have to be, just an example 
       data: { 
        first_name: $(this).find('#first_name').val(), 
        last_name: $(this).find('#last_name').val(), 
        company: $(this).find('#company').val(), 
        email: $(this).find('#email').val(), 
        phone: $(this).find('#phone').val() 
       }, 
       beforeSend: function() { 
        $(this).find('input[type="submit"]').prop('disabled', true); 
       }, 
       success: function(response) { 
        if (typeof response.error !== 'undefined') { // assuming you're returning something like this 
         console.log('It works!'); 
        } 
        else console.log('Houston, we have a problem ...'); 
       }, 
       error: function() { 
        console.log('oops!'); 
       }, 
       complete: function() { 
        $(this).find('input[type="submit"]').prop('disabled', false); 
       } 
      }); 
     } 
    }); 
}); 
0

首先,不要相信用戶。當使用JavaScript作爲您唯一的保護措施時,您會信任用戶。

話雖這麼說: 你onsubmit函數不返回false,這樣的形式獲得的提交標準的HTML方式,即使所有的安全措施失效。見Prevent form redirect OR refresh on submit?https://stackoverflow.com/a/38491643/4275413

後你有固定的,你也許可以後ajax.call它禁用按鈕和東西(這是異步的做外「成功」的東西,但只有當程序)。你的javascript - 我相信 - 在元素的html上覆蓋onsubmit。那是另一個。

也許還有更多的事情正在進行,但你應該嘗試這兩個開始。