首頁  >  問答  >  主體

如何存取使用PHP函數儲存的JS檔案中的值

<p>我正在為WordPress中的聊天機器人建立一個自訂插件,其中我在JS檔案中編寫了一些邏輯來獲取API和回應。作為請求體,我正在發送兩個參數。其中一個參數使用PHP函數(我的核心插件檔案)儲存在wp資料庫中。所以我想在我的JS檔案中存取該函數/該值,以便在API請求中發送它。 </p><p>JS函式 -</p><p><br /></p> <pre class="brush:php;toolbar:false;">fetch('https://whatgpt.up.railway.app/api/query-train-gpt', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ query: userMessage, number: phoneNumber, }), })</pre> <p>後端的PHP函數 -</p> <pre class="brush:php;toolbar:false;">function get_admin_phone_number() { // Retrieve the stored admin phone number from the database. $admin_phone_number = get_option('admin_phone_number', ''); // Provide a default value if the option is not set. // Return the admin phone number. return $admin_phone_number; }</pre> <p><br /></p>
P粉590929392P粉590929392427 天前489

全部回覆(1)我來回復

  • P粉785957729

    P粉7859577292023-07-29 17:02:15

    您可以將函數的輸出值設為$_SESSION['admin_phone_no'],然後在您的JS檔案中使用會話中的值。

    回覆
    0
  • 取消回覆