为了账号安全,请及时绑定邮箱和手机立即绑定

在 AJAX 中获取 PHP 值以替换输入字段值

在 AJAX 中获取 PHP 值以替换输入字段值

PHP
侃侃无极 2021-06-30 18:00:47
我在 Wordpress 中创建了一个名为Location/Tour的自定义帖子类型和另一个名为Itinerary的自定义帖子类型。在我的 CPT 行程中,我有一些ACF 自定义字段,其中之一是具有子字段的转发器字段(CPT 位置/旅游的关系字段、标题字段、描述字段)。我创建了一个按钮,它应该触发一个 AJAX 脚本,它的工作是从CPT Location/Tour(Title and Description)获取值,并将它们放在我的CPT Itinerary 的输入子字段(Title and Description)中。我创建了一个 PHP 函数,它从CPT Location/Tour获取值,现在我正在尝试使用 AJAX 运行 PHP 函数。我能够让 AJAX 工作,并且我在 ResponseText 下的控制台日志中获得了值。现在是我正在努力的部分。我需要在 JS中将每个值设置为一个单独的变量,以便我可以用新的值替换输入字段值,但不幸的是我不知道如何。我几乎尝试了所有方法,我认为我已经接近答案了,但我错过了一些东西。:(这是我的post-value-loader.php<?php// LOAD DEFAULT VALUES FROM DEFAULT TOURadd_action('wp_ajax_post_loader', 'post_loader');function post_loader($field) {  $post_id = $_POST["post_id"];  $args = array(    'p' => $post_id,    'numberposts'=> -1,           // Fetch all posts...    'post_type'=> 'location_tour',      // from the 'location_tour' CPT...  );  $location = new WP_Query( $args );  if ( $location->have_posts() ) : while ( $location->have_posts() ) : $location->the_post();        $title = the_field('title'); //The Title field value that we need        $description = the_field('description'); //The Description field value that we need        wp_reset_postdata();      ?>     <?php endwhile; endif; ?><?php add_action('acf/prepare_field/name=default_tour', 'post_loader'); ?><?php }// BUTTON TO RUN AJAXfunction my_acf_prepare_field($field) {  echo '<div class="acf-field"><button type="submit" id="data_fetch" class="button acf-load-default-tour-values">Load default value</button></div>';    return $field;}add_action('acf/prepare_field/name=default_tour', 'my_acf_prepare_field');// ADD SCRIPT TO WORDPRESS ADMIN AJAXfunction js_data_fetch() {  wp_enqueue_script ("ajax-data-fetch", get_stylesheet_directory_uri() . "/inc/assets/js/data-fetch.js", array('jquery'));   //the_ajax_script will use to print admin-ajaxurl in data-fetch.js  wp_localize_script('ajax-data-fetch', 'the_ajax_script', array('ajaxurl' =>admin_url('admin-ajax.php')));} add_action("admin_enqueue_scripts", "js_data_fetch");?>
查看完整描述

1 回答

?
繁华开满天机

TA贡献1816条经验 获得超4个赞

您必须从 post_loader 函数以 JSON 形式返回数据。我已经清理了一点,但仍然是一团糟。


// LOAD DEFAULT VALUES FROM DEFAULT TOUR

add_action('wp_ajax_post_loader', 'post_loader');


function post_loader() {


    $post_id = $_POST["post_id"];


    $args = array(

        'p' => $post_id,

        'numberposts'=> -1,           // Fetch all posts...

        'post_type'=> 'location_tour',      // from the 'location_tour' CPT...

    );


    $location = new WP_Query( $args );


    if ( $location->have_posts() ) : 

        while ( $location->have_posts() ) : 

            $location->the_post();


            $title = the_field('title');

            $description = the_field('description');


            // You have to return data as json

            wp_send_json([

                'title' => $title,

                'description' => $description

            ]);


            //wp_reset_postdata();

        endwhile; 

    endif;  


    // Why do you need this inside this function?

    // add_action('acf/prepare_field/name=default_tour', 'post_loader');  


}

JS


jQuery(document).on( 'click', '#data_fetch', function( dohvati ){

    dohvati.preventDefault();


    var post_id = jQuery('.acf-row .selection .values ul li span').data('id'); // This takes the post ID from the selected Post(Location/Tour) in the Relationship field


    jQuery.ajax({

        url: the_ajax_script.ajaxurl, //The URL that we set for the wordpress admin-ajax.php

        type: "POST",

        dataType: 'json',

        data: {

            action: 'post_loader', // This is the name of the php function

            post_id: post_id,

        },

        success: function(data){

            console.log(data)


            jQuery("#acf-field_5cb991a9337db-row-0-field_5cbeabc041c8a").val(data.title); //This is replacing the title field - but the variables are missing

            jQuery("#acf-field_5cb991a9337db-row-0-field_5cbeab8f41c89").val(data.description); //This is replacing the description field - but the variables are missing


        },

        error: function(error){

            console.log(error)

        },

    });


});


查看完整回答
反对 回复 2021-07-02
  • 1 回答
  • 0 关注
  • 101 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信