如何访问使用PHP函数存储的JS文件中的值
P粉590929392
P粉590929392 2023-07-29 00:04:23
0
1
483
<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粉590929392
P粉590929392

全部回复(1)
P粉785957729

您可以将函数的输出值设置为$_SESSION['admin_phone_no'],然后在您的JS文件中使用会话中的值。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!