N
N
Nick_KrM2021-06-18 10:55:33
PHP
Nick_KrM, 2021-06-18 10:55:33

How to pass (export/import) a variable from javascript to php?

There is a variable in js file:

const orderMessage = `шаблонная строка в виде сообщения на почту`;

How to transfer it to sendmail.php and to the body of the sent message?

$body = ' значение импортированной переменной (то есть та самая шаблонная строка) ';

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Egor Davydov, 2021-06-18
@Nick_KrM

If there is an AJAX submit in the code, then the best solution would be to add this secret string to the form.
Just add an input with the hidden parameter, and when processing the form submission, you set the value of this input in the form to the desired string.

B
BornTo FreeFall, 2021-06-18
@BornToFreeFall

Use AJAX (needs to be improved)

$(document).ready(function(){
          $.ajax({ type: 'POST', url: 'LINK_TO_YOUR_SENDMAIL.PHP', success: function(response){
                    // SOME_ACTION
                 }
          });
});

R
rPman, 2021-06-18
@rPman

If the variable is needed in each request for some time and is small in size, then it can be stored in session cookies setCookies and in php $_COOKIE[name]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question