pass value js to another page in action form

1

Hello, I have a simple form with some fields, which I send the values to another page via form action:

<form class="form-horizontal" action="novapagina.php" enctype="multipart/form-data" method="post" >

I would like to send a js variable together to the form fields, for this, I tried:

<?php 

 $variavel_php ="<script>document.write(variavel_java_script)</script>";
        ?>  

and no form action:

<form class="form-horizontal"   enctype="multipart/form-data"  action="novapagina.php?id=<?php    echo     $variavel_php;   ?>" method="post" >

so the value of the variable on the page you receive is the string: <script>document.write(variavel_java_script)</script>

I need to pass the integer value contained in the js variable.

Using jquery, so vi would have to redo the whole form, it is not my intention,

1) Is there a way to pass (integer) the integer value to php variable and open it in a new page ??

2) Is there a method of passing the variable js in the form action ??

    
asked by anonymous 05.06.2017 / 07:20

2 answers

0

You can not pass values from javascript to PHP, unless you do this with Ajax. But we can do this in a simpler way.

Create a field of type hidden:

<input type="hidden" name="variavel_js" id="variavel_js" />

Now assign the value you have in your js.

document.getElementById("variavel_js").value = variavel_java_script;
    
05.06.2017 / 16:23
-1

One solution is to use localstorage, it's very simple, just run the command below via javascript:

// Na página do formulário
localStorage.setItem("variavel_js", "Valor da sua variável");

// Na página que gostaria de resgatar o valor, pode ser em qualquer uma desde que o valor foi setado:
var variavel_js = localStorage.getItem("variavel_js"); // Vai ser igual a "Valor da sua variável"

In the w3schools website, you have great documentation on this feature, see link .

This feature is supported in the following browsers:

    
05.06.2017 / 16:34