亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 AJAX 網站上使用動態注入的 Ninja Forms

在 AJAX 網站上使用動態注入的 Ninja Forms

PHP
慕婉清6462132 2023-08-19 14:08:57
我正在構建一個 Wordpress 網站,并使用 AJAX 在用戶導航時加載后續頁面,以便頁面轉換具有精美的動畫效果。默認情況下,如果您加載并注入嵌入了 Ninja Form 的頁面,則該表單根本不會顯示。關于如何實現這一目標的信息非常少。我希望有一個官方的開箱即用的方式來實現這一點,但似乎沒有。當使用 AJAX 動態加載表單時,需要采取哪些步驟才能使表單顯示在頁面上?
查看完整描述

1 回答

?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

我嘗試了一些完全未記錄的代碼示例,并設法弄清楚了它,以及一些必要的調整和添加。我想我會在這里分享,以防其他人遇到困難。


步驟 1. 將必要的 JS 和 CSS 入隊


將以下內容添加到您的functions.php中,因為Ninja Forms依賴于主干js,并且需要CSS,如果您的初始登陸頁面上還沒有表單,則不會加載CSS。


add_action('wp_enqueue_scripts', function () {

  // enqueue ninja forms css, including for any ninja forms addons

  wp_enqueue_style('nf-display', content_url('/plugins/ninja-forms/assets/css/display-structure.css'), ['dashicons'], core_dev_version(''));

  wp_enqueue_style('nf-layout-front-end', content_url('/plugins/ninja-forms-style/layouts/assets/css/display-structure.css'), ['nf-display'], core_dev_version(''));


  // make sure that backbone is enqueued on the page, as ninja forms relies on this

  wp_enqueue_script('backbone');

}, 100);

.


步驟 2. 添加返回表單數據的 AJAX 函數


您不需要編輯它,只需將其添加到您的functions.php 中即可。我們在步驟 3 中使用的 javascript 將進行自己的 AJAX 調用,以非常特定的格式請求一些表單數據。


add_action('init', function () {

  // if this is not an AJAX form request, return

  if (! isset($_REQUEST[ 'async_form' ])) {

    return;

  }


  // clear default loaded scripts.

  global $wp_scripts;

  unset($wp_scripts->registered);


  // get the requested form id

  $form_id = absint($_REQUEST['async_form']);


  // retrieve the requested form

  ob_start();

  $form = do_shortcode("[ninja_forms id='{$form_id}']");

  ob_get_clean();


  // output the requested form on the page

  ob_start();

  NF_Display_Render::output_templates();

  $templates = ob_get_clean();

  $response = [

    'form' => $form,

    'scripts' => $wp_scripts->registered,

    'templates' => $templates

  ];

  echo json_encode($response);


  // die, because we don't want anything else to be returned

  die();

});

.


步驟 3. 將 JS 輔助函數添加到您的登陸頁面


這只是將一些有用的 JS 代碼添加到您的站點中。您可以將其按原樣添加到functions.php,或將其包含在單獨的JS文件中。這就是我們將用來加載和初始化我們想要的表單的方法。


add_action('wp_footer', function () {

  // adds a script to the footer

  ?>

  <script type="text/javascript">


  var NinjaFormsAsyncForm = function(formID, targetContainer) {


    this.formID = formID;

    this.targetContainer = targetContainer;


    this.formHTML;

    this.formTemplates;

    this.formScripts;


    this.fetch = function(callback) {

      jQuery.post('/', { async_form: this.formID }, this.fetchHandler.bind(this))

      .then(callback);

    }

    this.fetchHandler = function(response) {

      response = JSON.parse( response );


      window.nfFrontEnd = window.nfFrontEnd || response.nfFrontEnd;

      window.nfi18n = window.nfi18n || response.nfi18n || {};


      this.formHTML = response.form;

      this.formTemplates = response.templates;

      this.formScripts = response.scripts;

    }


    this.load = function() {

      this.loadFormHTML(this.formHTML, this.targetContainer);

      this.loadTemplates(this.formTemplates);

      this.loadScripts(this.formScripts);

    }


    this.loadFormHTML = function(form, targetContainer) {

      jQuery(targetContainer).append( form );

    }

    this.loadTemplates = function(templates) {

      document.body.innerHTML += templates;

    }

    this.loadScripts = function(scripts) {

      jQuery.each( scripts, function( nfScript ){

        var script = document.createElement('script');

        // note that eval() can be dangerous to use - do your research

        eval( scripts[nfScript].extra.data );

        window.nfFrontEnd = window.nfFrontEnd || nfFrontEnd;

        script.setAttribute('src',scripts[nfScript].src);

        var appendChild = document.head.appendChild(script);

      });

    }


    this.remove = function() {

      jQuery(this.targetContainer).empty();

    }

  }


  </script>

  <?php

}, 100);

.


步驟 4. 在頁面中使用 AJAX 后初始化表單


我無法告訴您如何在頁面中使用 AJAX - 這是您需要弄清楚的另一個主題。但是,一旦您使用包含的 Ninja Form 加載了頁面內容,并且成功位于頁面上,現在您需要初始化該表單。


這使用了 javascript 幫助程序(步驟 3),該幫助程序又調用 php 幫助程序(步驟 2),最后在您的頁面上顯示表單!


您需要知道已注入表單的 ID。我的策略是將其作為數據屬性包含在我的頁面標記中。


var form_id = 1;

var asyncForm = new NinjaFormsAsyncForm(form_id, '.ninja-forms-dynamic');

asyncForm.fetch(function () {

  asyncForm.load();

});

這就是全部內容了!


希望這可以節省其他人解決所有這些問題的時間和精力。


查看完整回答
反對 回復 2023-08-19
  • 1 回答
  • 0 關注
  • 200 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號