根据所选字段的选择自动填写表单中的字段

问题描述 投票:0回答:2

我有一个用于添加帖子的前端表单(acf 前端),在此表单中,我有一个用于客户名称的字段(帖子对象),我在其中成功选择了客户。我还有一些字段(文本)、客户电话、客户电子邮件、客户地址等。我需要这些字段自动成为我在帖子对象字段中选择的客户值的字段。帖子对象字段返回帖子 ID。我将氧气生成器和 acf 用于自定义字段和自定义类型帖子。我在代码片段中使用了以下代码,但我无法管理它。

jQuery(document).ready(function($) {
   $('#postObjectField').change(function() {
    var selectedCustomerId = $(this).val(); // Get the selected customer ID
    // Send an AJAX request to fetch customer details based on the selected ID
    $.ajax({
        url: ajax_object.ajax_url, // WordPress AJAX URL
        method: 'POST',
        data: {
            action: 'fetch_customer_details',
            customer_id: selectedCustomerId
        },
        success: function(response) {
            // Update the customer phone, email, and address fields with the fetched values
            $('#customerPhone').val(response.customer_phone);
            $('#customerEmail').val(response.customer_email);
            $('#customerAddress').val(response.customer_address);
        },
        error: function(xhr, status, error) {
            // Handle errors if any
            console.error(error);
        }
    });
});

});

add_action('wp_ajax_fetch_customer_details', 'fetch_customer_details');
add_action('wp_ajax_nopriv_fetch_customer_details', 'fetch_customer_details');

function fetch_customer_details() {
$customer_id = $_POST['customer_id'];

// Use ACF functions to get customer details based on the provided ID
$phone = get_field('phone', 'customer_' . $customer_id);
$email = get_field('email', 'customer_' . $customer_id);
$address = get_field('address', 'customer_' . $customer_id);

// Return the customer details as JSON
echo json_encode(array(
    'customer_phone' => $phone,
    'customer_email' => $email,
    'customer_address' => $address
));

wp_die(); // Always include this to terminate the script

}

谢谢你!

advanced-custom-fields
2个回答
0
投票

您的流程可能在多个地方中断。让我们看看他们:

1.可能缺少 jQuery 或其他先决条件

查看浏览器的开发工具,看看 jQuery 和您想要加载的其他库是否实际加载。试试这个:

jQuery(document).ready(function($) {
    console.log("loaded");
    //your further code here
})

如果在加载页面时您在浏览器控制台中看到“已加载”,则 jQuery 已成功加载,并且到目前为止一切看起来都很好。如果您在控制台中没有看到它,请尝试找出发生了什么。也许您错过了加载 jQuery 或包含此加载器的文件。

2. jQuery 与 $

如果您看到一条错误消息,指出未找到

jQuery
,则您的 jQuery 变量可能名为
$
。确保其已正确加载。

3.有表格吗?

如果涉及

form
并且您以某种方式触发其提交事件,那么您将进行回发,并且您将不会看到字段因重新加载而更改。

4.您假设存在的所有元素在页面加载时实际上都存在吗?

页面加载时是否有

postObjectField
customerPhone
customerEmail
customerAddress
项目?确保这些都没有丢失。

5.仔细检查您的网址

确保您的

ajax_object.ajax_url
正确。

6.您的帖子格式正确吗?

执行 AJAX 调用时,请仔细检查

selectedCustomerId
最终是否具有正确的值以及是否正在发送 POST 请求。

7.检查您的回复

如果一切正常,您将收到包含所需数据的响应。是这样吗?如果没有,那么您的

fetch_customer_details
函数没有被调用或没有被正确处理。检查服务器日志。

8.格式正确吗

你做了一个

echo json_encode(array(
    'customer_phone' => $phone,
    'customer_email' => $email,
    'customer_address' => $address
));

转换为:

{
    "customer_phone": "abc",
    "customer_email": "[email protected]",
    "customer_address": "ghi"
}

当然,您的实际值会有所不同。但这可能是一个字符串。现在,让我们做一些实验:

let template = `
{
    "customer_phone": "abc",
    "customer_email": "[email protected]",
    "customer_address": "ghi"
}
`;

//String does not have the fields you need:

console.log(`
    customer_phone: ${template.customer_phone},
    customer_email: ${template.customer_email},
    customer_address: ${template.customer_address}
`);

//Now we have the fields

let convertedJSON = JSON.parse(template);

console.log(`
    customer_phone: ${convertedJSON.customer_phone},
    customer_email: ${convertedJSON.customer_email},
    customer_address: ${convertedJSON.customer_address}
`);

第一个结果显示未解析的字符串没有您想要的字段。第二个结果显示,如果我们将字符串解析为 JSON,那么它就会包含您想要的字段。也许你需要这个:

        success: function(response) {
            let parsedJSON = JSON.parse(response);
            // Update the customer phone, email, and address fields with the fetched values
            $('#customerPhone').val(parsedJSON.customer_phone);
            $('#customerEmail').val(parsedJSON.customer_email);
            $('#customerAddress').val(parsedJSON.customer_address);
        },

0
投票

谢谢您的帮助,但实际上我做了其他事情,这是有效的,但仅在后端,当我通过 acf 前端表单添加帖子时,它不起作用,但只有当我从后端更新帖子时,我使用以下代码,

function update_transfer_custom_fields_from_customer_post($post_id) {
// Check if this is a "transfers" post being saved
if (get_post_type($post_id) === 'transfers') {
    // Get the customer post ID from the ACF field
    $customer_post_id = get_field('customer_name', $post_id);

    // Check if the customer post ID is not empty
    if ($customer_post_id) {
        // Get the customer post object using the post ID
        $customer_post_object = get_post($customer_post_id);

        // Check if the customer post object exists and if it's of the 
  "customers" post type
        if ($customer_post_object && $customer_post_object->post_type === 
 'customers') {
            // Get the properties you want to update from the "customers" 
 post
             $customer_phone = get_field('customer_phone', 
 $customer_post_id, true);
             $customer_email = get_field('customer_email', 
 $customer_post_id, true);
             $customer_address = get_field('customer_address', 
 $customer_post_id, true);
             $customer_vat_id = get_field('customer_vat_id', 
 $customer_post_id, true);
             $customer_tax_office = get_field('customer_tax_office', 
 $customer_post_id, true);
    
             // Update custom fields of the "transfers" post
             update_field('customer_phone', $customer_phone, $post_id,);
             update_field('customer_email', $customer_email, $post_id,);
             update_field('customer_address', $customer_address, 
 $post_id,);
             update_field('customer_vat_id', $customer_vat_id, $post_id,);
             update_field('customer_tax_office', $customer_tax_office, 
 $post_id,);    
          
        }
    }
 }
}

// Hook into the save_post action
 add_action('save_post', 
'update_transfer_custom_fields_from_customer_post');

当我从前端添加帖子时,任何人都可以帮助我使其工作吗?谢谢!

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