php - 如何在不刷新页面的情况下使用ajax保存acf_form

标签 php jquery ajax wordpress advanced-custom-fields

我有这三个 acf_form。我想一个一个地显示这些表单,并且我想通过 ajax 保存按钮单击时的每个表单而不刷新页面。现在,每当我更新时它都会刷新页面。 我将使用 js 通过 display none & block 来展示。

<div class="SetupNew">
            <h2>Setup Deals To Attract New Clientele</h2>
            <p>Example: buy $15 get $30 for services</p>
            <a href="javascript:void(0)"><p id="newDealsTxt">[Click Here To Setup] </p></a></div>
            <?php acf_form($args =  array(
                                          'post_id' => $post_id,
                                          'field_groups' => array(2029),
                                          'form_attributes' => array(
                                              'id'=>'newDeals'
                                           ),
                                      ));  ?>
            <div class="SetupEx">
            <h2>Setup Deals To Bring In Clientele During Nonpeak Hours</h2>
            <p>Example: buy $15 get $30 for services Tue-Thur 9am - 2pm.</p>          
            <a href="javascript:void(0)"><p id="exDealsTxt">[Click Here To Setup]</p></a></div>
            <?php  acf_form($args =  array(
                                          'post_id' => $post_id,
                                          'field_groups' => array(2047),
                                          'form_attributes' => array(
                                              'id'=>'exDeals'
                                          ),
                                      ));  ?>
            <div class="SetupFb">
            <h2>Setup $5 Off Coupon To Increase Testimonials And Sharing</h2>
            <p>Example: Leave a testimonial and get $5 off your next service.</p>
            <a href="javascript:void(0)"><p id="fbDealsTxt">[Click Here To Setup] </p></a></div>

            <?php  acf_form($args =  array(
                                          'post_id' => $post_id,
                                          'field_groups' => array(2123),
                                          'form_attributes' => array(
                                              'id'=>'fbDeals'
                                          ),
                                      ));  ?>
            <a href="javascript:void(0)"><h2 id="backk">Back << </h2>  </a>

最佳答案

这来得太晚了,但我今天遇到了同样的问题。作为引用,这是我的 javascript:

// call this function on document ready or when your ajax page is loaded
function renderPage() {
  // initialize the acf script
  acf.do_action('ready', $('body'));

  // will be used to check if a form submit is for validation or for saving
  let isValidating = false;

  acf.add_action('validation_begin', () => {
    isValidating = true;
  });

  acf.add_action('submit', ($form) => {
    isValidating = false;
  });

  $('.acf-form').on('submit', (e) => {
    let $form = $(e.target);
    e.preventDefault();
    // if we are not validating, save the form data with our custom code.
    if( !isValidating ) {
      // lock the form
      acf.validation.toggle( $form, 'lock' );
      $.ajax({
        url: window.location.href,
        method: 'post',
        data: $form.serialize(),
        success: () => {
          // unlock the form
          acf.validation.toggle( $form, 'unlock' );
        }
      });
    }
  });
}

关于php - 如何在不刷新页面的情况下使用ajax保存acf_form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35058298/

相关文章:

ajax获取对Office365 REST Api的请求失败CORS?

ajax - jqueryui 对话框在 ajax 调用完成之前不会打开(仅限 IE)

php - Codeigniter POST/GET 变量和 $this->input->post

php - 如何使用 Laravel 从数据库中存储和检索图像内容

php - 此代码中如何执行 ffmpeg 命令

javascript - 如何运行远程 PHP 脚本,然后点击 HTML 页面中的链接

jquery - 清除jquery动画应用的css

jquery - 淡出和删除表行在我的 asp.net mvc 中不能一起工作

javascript - Yii加载动态添加(使用ajax)GridView TypeError : settings is undefined

php - @#variable 在 php 中的含义?