我有一个要提交的简码(一种表格)。我正在尝试使用JQuery提交它,并且它在数据库中输入的值未定义。有人可以帮我找到为什么我对$ _POST请求的结果为0吗?
以下是过程: Submit.php
$county = sanitize_text_field($_POST['county']);
$country = sanitize_text_field($_POST['country']);
$town = sanitize_text_field($_POST['town']);
$postcode = sanitize_text_field($_POST['postcode']);
$property_description = sanitize_text_field($_POST['property_description']);
$displayable_address = sanitize_text_field($_POST['displayable_address']);
$nr_of_bedrooms = sanitize_text_field($_POST['nr_of_bedrooms']);
$nr_of_bathrooms = sanitize_text_field($_POST['nr_of_bathrooms']);
$price = sanitize_text_field($_POST['price']);
$property_type = sanitize_text_field($_POST['property_type']);
$sale_rent = sanitize_text_field($_POST['sale_rent']);
$custom = true;
$data_array = [
'county' => $county,
'country' => $country,
'town' => $town,
'postcode' => $postcode,
'property_description' => $property_description,
'displayable_address' => $displayable_address,
'nr_of_bedrooms' => $nr_of_bedrooms,
'nr_of_bathrooms' => $nr_of_bathrooms,
'price' => $price,
'property_type' => $property_type,
'sale_rent' => $sale_rent,
'custom' => $custom,
];
$wpdb->insert($wpdb->prefix . 'property', $data_array, $format=NULL);
这是我的JQuery:
$("#add-form").on("submit", function (e) {
e.preventDefault();
$(this).hide();
$("#property-status").html('<div class="alert alert-info text-center">Please wait!</div>');
var form = {
action: "r_submit_user_property",
county: $("#county").val,
country: $("#country").val,
town: $("#town").val,
postcode: $("#postcode").val,
property_description: $("#property_description").val,
displayable_address: $("#displayable_address").val,
nr_of_bedrooms: $("#nr_of_bedrooms").val,
nr_of_bathrooms: $("#nr_of_bathrooms").val,
price: $("#price").val,
property_type: $("#property_type").val,
sale_rent: $("#sale_rent").val
};
$.post(recipe_obj.ajax_url, form).always(function (response) {
if (response.status == 2) {
$('#property-status').html('<div class="alert alert-success">Property submitted successfully!</div>');
} else {
$('#property-status').html(
'<div class="alert alert-danger">Unable to submit property. Please fill in all fields.</div>'
);
$("#add-form").show();
答案 0 :(得分:1)
请全部将$("#county").val
更改为$("#county").val()
。因为val
是函数val()
。
var form = {
action: "r_submit_user_property",
county: $("#county").val(),
country: $("#country").val(),
town: $("#town").val(),
postcode: $("#postcode").val(),
property_description: $("#property_description").val(),
displayable_address: $("#displayable_address").val(),
nr_of_bedrooms: $("#nr_of_bedrooms").val(),
nr_of_bathrooms: $("#nr_of_bathrooms").val(),
price: $("#price").val(),
property_type: $("#property_type").val(),
sale_rent: $("#sale_rent").val()
};
此外,您还应该检查submit.php
文件中的参数值。