How to access value in JS file stored using PHP function
P粉590929392
P粉590929392 2023-07-29 00:04:23
0
1
438

I'm building a custom plugin for a chatbot in WordPress, where I've written some logic in a JS file to get the API and responses. As request body I am sending two parameters. One of the parameters is stored in the wp database using a PHP function (my core plugin file). So I want to access that function/that value in my JS file so I can send it in the API request.

JS function -


fetch('https://whatgpt.up.railway.app/api/query-train-gpt', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ query: userMessage, number: phoneNumber, }), })

Backend PHP functions -

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; }


P粉590929392
P粉590929392

reply all (1)
P粉785957729

You can set the output value of the function to $_SESSION['admin_phone_no'] and then use the value from the session in your JS file.

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!