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

我在 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 TOUR

add_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 AJAX

function 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 AJAX

function 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");

?>


侃侃无极
浏览 102回答 1
1回答

繁华开满天机

您必须从 post_loader 函数以 JSON 形式返回数据。我已经清理了一点,但仍然是一团糟。// LOAD DEFAULT VALUES FROM DEFAULT TOURadd_action('wp_ajax_post_loader', 'post_loader');function post_loader() {&nbsp; &nbsp; $post_id = $_POST["post_id"];&nbsp; &nbsp; $args = array(&nbsp; &nbsp; &nbsp; &nbsp; 'p' => $post_id,&nbsp; &nbsp; &nbsp; &nbsp; 'numberposts'=> -1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Fetch all posts...&nbsp; &nbsp; &nbsp; &nbsp; 'post_type'=> 'location_tour',&nbsp; &nbsp; &nbsp; // from the 'location_tour' CPT...&nbsp; &nbsp; );&nbsp; &nbsp; $location = new WP_Query( $args );&nbsp; &nbsp; if ( $location->have_posts() ) :&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; while ( $location->have_posts() ) :&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $location->the_post();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $title = the_field('title');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $description = the_field('description');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // You have to return data as json&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wp_send_json([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'title' => $title,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'description' => $description&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //wp_reset_postdata();&nbsp; &nbsp; &nbsp; &nbsp; endwhile;&nbsp;&nbsp; &nbsp; endif;&nbsp;&nbsp;&nbsp; &nbsp; // Why do you need this inside this function?&nbsp; &nbsp; // add_action('acf/prepare_field/name=default_tour', 'post_loader');&nbsp;&nbsp;}JSjQuery(document).on( 'click', '#data_fetch', function( dohvati ){&nbsp; &nbsp; dohvati.preventDefault();&nbsp; &nbsp; 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&nbsp; &nbsp; jQuery.ajax({&nbsp; &nbsp; &nbsp; &nbsp; url: the_ajax_script.ajaxurl, //The URL that we set for the wordpress admin-ajax.php&nbsp; &nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; dataType: 'json',&nbsp; &nbsp; &nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; action: 'post_loader', // This is the name of the php function&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; post_id: post_id,&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; success: function(data){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(data)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jQuery("#acf-field_5cb991a9337db-row-0-field_5cbeabc041c8a").val(data.title); //This is replacing the title field - but the variables are missing&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jQuery("#acf-field_5cb991a9337db-row-0-field_5cbeab8f41c89").val(data.description); //This is replacing the description field - but the variables are missing&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; error: function(error){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(error)&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; });});
打开App,查看更多内容
随时随地看视频慕课网APP