发布请求wordpress undefined

问题描述 投票:1回答:1

我有一个我想提交的短代码(表格)。我正在尝试使用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();

enter image description here

php jquery wordpress
1个回答
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文件中的参数值。

© www.soinside.com 2019 - 2024. All rights reserved.