pass js value to another page in the action form

Asked

Viewed 651 times

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 wish to send a variable js together to form fields, for such, tried:

<?php 

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

and in 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>

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

Using jquery, from what I saw I would have to redo the whole form, it is not my intention,

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

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

2 answers

0


You cannot 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;

0

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"

On the w3schools website there is great documentation about this resource, see on link.

This feature is supported in the following browsers:inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.