How to add a javascript variable in PHP? [duplicate]

1

I want to get the variável of the vetor that I create in the JS and store the contents of it in one of PHP .

<?php
       print("<SCRIPT language=javascript>  
              vetor_dados[$cont] ="text";
       </SCRIPT>"); 
$conteudo = vetor_dados;
?>

How should I do it?

Note:  The way to use the, eg:

<script type="text/javascript">
var x = 'valor';
</script>
<?php
    $x = "<script>document.write(x)</script>";
    echo $x;
?>

Causes $x to show < script>document.write(x)</script> and not JS ;

Image as it appears in the form above:

    
asked by anonymous 08.10.2014 / 19:27

2 answers

3

It's not possible this way. You need to understand the concept: HTML and JS are client language which means that the execution of them is done on your computer. PHP is a server language, which means that the PHP "engine" is on the server, not on your computer.

From the chronological point of view, the browser requests the page, the PHP engine parses the request and generates the code and sends it back to the browser, so PHP terminates. The page travels through the internet, reaches the user's computer and from this moment the browser renders the received HTML and executes the Javascript.

It means that the part

 <?php $conteudo = vector_dados ?>

runs on the server, and the rest (HTML, CSS, and JS) runs AFTER the user machine.

To recover Javascript data with PHP, you need to re-send the data to the server.

    
08.10.2014 / 19:40
0

You'll need to do this:

<SCRIPT language=javascript>  
    var vetor_dados ="text";
</SCRIPT>

<?php
    $conteudo = "<script>document.write(vetor_dados)</script>";

    echo $conteudo;
?>
    
08.10.2014 / 19:34