B
B
barakuda12021-07-30 11:33:16
PHP
barakuda1, 2021-07-30 11:33:16

Why is the value not being passed from js to php?

Welcome all.
Explain why my value and JS are not being passed to PHP and how to do it correctly.

<script>
var abcd = 2222;
</script>


$GetVar = "<script>document.write(abcd)</script>"; 
    console.log($GetVar);


Console is empty. No errors are observed.

Also, the question is of interest: what is the actual way to transfer the value of a variable from PHP to JS?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya, 2021-07-30
@rpsv

You generally have a language written in some kind of PHJS.
You can transfer from PHP to JS like this:

<?php

$abcd = 1234;

?>
<script>
    var abcd = <?= json_encode($abcd) ?>;
    var abcd = <?= $abcd ?>;
    console.log(abcd);
</script>

You need to pass a value from JS to PHP via AJAX (go to the search, there are a lot of such questions)

R
Roman Yamchuk, 2021-07-30
@tomgif

PHP is a server-side language and JS is a client-side language (runs in a browser). First, the PHP script will be executed, and only then the JS script will be executed.
For your task - you need to pass data to a php script, this can be done through a form or an ajax request

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question