我有一个隐藏的选择下拉列表,根据用户选择动态显示。提交表单后,所有表单字段(包括隐藏表单)都将发布到PHP脚本。 表单使用ajax提交。有没有办法禁用隐藏字段提交。 谢谢。
Ajax:
$("#new-contact-form").validate({
rules: {
contact_fname: "required",
contact_lname: "required",
},
messages: {
contact_fname: "First name is missing",
contact_lname: "Last name is missing",
},
submitHandler: function(form) {
$.ajax ({
type:"POST",
url:"../includes/functions/contact-functions.php",
data: $('form#new-contact-form').serialize(),
success: function(msg){
closeModal();
//notify();
location.reload();
},
});
}
});
模态/形式仅部分(非常长形式):
<div class="form-group ">
<label class="col-lg-3 col-sm-2 control-label">Contact Type</label>
<div class="col-lg-4">
<select class="form-control m-bot15" id="contact_type">
<option value="None"> Contact Type</option>
<option value="Account">Account</option>
<option Value="Service_provider">Service Provider</option>
<option value="Lead">Lead</option>
</select>
</div>
<!--Start Contact Type inputs-->
<!-- Show Accounts -->
<div class="col-lg-5 ">
<select class="form-control m-bot15 " id="accounts_list" name="contact_account_name" style="display:none">
<option value="no-account">Select Account</option>
<?php list_accounts();?>
<option value="Other_Account">Other</option>
</select>
</div>
<!--Show Service Providers-->
<div class="col-lg-5 ">
<select class="form-control m-bot15 " id="providers_list" name="contact_sp_name" style="display:none">
<option value="no-sp">Select Service Provider</option>
<?php list_providers();?>
<option value="Other_Provider">Other</option>
</select>
</div>
</div>
功能:
if ($_POST['contact_account_name']!='Other_Account' && $_POST['contact_account_name'] != 'no-account' && $_POST['contact_sp_name'] = 'no-sp') {
$current_account_id = $_POST['contact_account_name'];
$result = "INSERT INTO contacts (contact_fname, contact_lname, contact_email, contact_phone, contact_fax, contact_account_id, contact_type)
VALUES ('$contact_fname', '$contact_lname', '$contact_email', '$contact_phone', '$contact_fax', '$current_account_id', 1)";
mysqli_query($connection, $result);
if (!$result) {
printf("Error: %s\n", mysqli_error($connection));
exit();
}
echo "new ct insert";
}
//insert contact with new account
elseif ($_POST['contact_account_name'] ='Other_Account' && $_POST['contact_sp_name'] = 'no-sp' ){
$new_account_name = $_POST['new_account_name'];
$new_account = "INSERT INTO accounts (account_name, account_address1, account_address2, account_city, account_state, account_zip, account_phone)
VALUES ('$new_account_name', '$contact_address1', '$contact_address2', '$contact_city', '$contact_state', '$contact_zip', '$contact_phone')";
mysqli_query($connection, $new_account);
if (!$new_account) {
printf("Error: %s\n", mysqli_error($connection));
exit();
}
echo "new acct insert";
$new_account_inserted_id = mysqli_insert_id($connection);
$result = "INSERT INTO contacts (contact_fname, contact_lname, contact_email, contact_phone, contact_fax, contact_account_id, contact_type)
VALUES ('$contact_fname', '$contact_lname', '$contact_email', '$contact_phone', '$contact_fax', '$new_account_inserted_id', 1)";
mysqli_query($connection, $result);
if (!$result) {
printf("Error: %s\n", mysqli_error($connection));
exit();
}
echo "new ct with act insert";
}
//insert contact with existing SP
elseif ($_POST['contact_sp_name']!='Other_Account' && $_POST['contact_account_name'] = 'no-account') {
$current_sp_id = $_POST['contact_sp_name'];
$result = "INSERT INTO contacts (contact_fname, contact_lname, contact_email, contact_phone, contact_fax, contact_account_id, contact_type)
VALUES ('$contact_fname', '$contact_lname', '$contact_email', '$contact_phone', '$contact_fax', '$current_sp_id', 2)";
mysqli_query($connection, $result);
if (!$result) {
printf("Error: %s\n", mysqli_error($connection));
exit();
}
echo "new sp insert";
}
答案 0 :(得分:0)
我可以看到这样的事情:
$('#myForm').submit(function() {
var $fields = $('#myForm :input');
$fields.each(function() {
if( $(this).val() == "") {
$(this).remove();
}
});
// submit remaining forms data
});