我有一個PHP表單,在jQuery的幫助下發送數據。每當我點擊「提交」,我看到它發送兩次數據,導致雙重數據庫條目等等。PHP的JQuery POST - 發送重複的數據
此外,我仍在努力如何顯示try/catch塊的「錯誤」和異常。
注意:此表單在另一個網站上工作,我只更改了字段以及它發送數據的位置,但無法使其工作。
謝謝你的幫助。
登記表
<div id="registration" class="representativeForm">
\t <div class="result">
\t </div>
\t <div class="formbody">
\t \t <div class="left">
\t \t \t <input required="required" type="text" name="name" placeholder="Your name" />
\t \t \t <input required="required" type="text" name="surname" placeholder="Your surname" />
\t \t \t <input required="required" type="text" name="birthdate" placeholder="Your birth date" />
\t \t \t <input required="required" type="text" name="nationality" placeholder="Your nationality" />
\t \t \t <?php countriesSelection($con, $_LANG, "country"); ?>
\t \t \t <input required="required" type="email" name="email" placeholder="Your Email adress" />
\t \t \t <input required="required" type="text" name="position" placeholder="Your title or job position" />
\t \t \t <div class="schoolSelectionfields">
\t \t \t \t <?php schoolSelection($con, $_LANG, "schools"); ?>
\t \t \t </div>
\t \t \t <input type="checkbox" id="schoolnotlisted" name="schoolnotlisted" value="1">Your school is not listed? Register school here.<br />
\t \t </div>
\t \t <div class="right">
\t \t \t <div class="school_register">
\t \t \t \t <?php schoolRepresentativeRegistration($_LANG, $con); ?>
\t \t \t </div>
\t \t </div>
\t </div>
\t <input type="hidden" name="form" value="representativeForm">
\t <input type="submit" value="Register" id="representativeSubmit" />
</div>
jQuery代碼
$("#representativeSubmit").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
$("#registration.representativeForm input[required=true]").each(function(){
$(this).css('border-color','');
if(!$.trim($(this).val())){ //if this field is empty
$(this).css('border-color','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-color','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
post_data = {
'form' : $('input[name=form]').val(),
'name' : $('input[name=name]').val(),
'surname' : $('input[name=surname]').val(),
'birthdate' : $('input[name=birthdate]').val(),
'nationality' : $('input[name=nationality]').val(),
'email' : $('input[name=email]').val(),
'position' : $('input[name=position]').val(),
'country' : $('select[name=country]').val(),
};
if($("#schoolnotlisted").prop('checked') == true) {
post_data += {
'schoolName' : $('input[name=schoolName]').val(),
'schoolAddress' : $('input[name=schoolAddress]').val(),
'schoolZipcode' : $('input[name=schoolZipcode]').val(),
'schoolCity' : $('input[name=schoolCity]').val(),
'schoolCountry' : $('select[name=schoolCountry]').val()
};
}
else {
post_data += {
'schools' : $('select[name=schools]').val()
};
}
//Ajax post data to server
$.post('registration.php', post_data, function(response){
if(response.type == 'error'){ //load json data from server and output message
output = '<div class="error">'+response.text+'</div>';
}else{
output = '<div class="success">'+response.text+'</div>';
//reset values in all input fields
$("#registration.representativeForm input[required=true]").val('');
$("#registration.representativeForm .formbody").slideUp(); //hide form after success
}
$("#registration .result").hide().html(output).slideDown();
}, 'json');
}
});
//reset previously set border colors and hide all message on .keyup()
$("#registration.representativeForm input[required=true]").keyup(function() {
$(this).css('border-color','');
$(".result").slideUp();
});
告訴我,如果我的理解 - 它導航到一個新的頁面?默認情況下,單擊「提交」按鈕將提交表單並導航到操作頁面。您可能想使用'e.preventDefault'來禁用它。 – TastySpaceApple