pass input value to php session variable without page Reload

Asked

Viewed 475 times

0

Good.

How best to pass an input value to a php session variable without page Reload?

For example, with this method the page does the Reload:

<form method="post">
<input type="text" name="valor1" onchange="submit()">
</form>

<?php
session_start();

if($_POST['valor1']){
   $_SESSION['valor1'] = $_POST['valor1'];
}
?>

How can I get the same result without Reload?

  • With Jquery + AJAX

1 answer

0


Using asynchronous charging, using Ajax.

Example with jQuery:

Page form.php

<html>
  <body>
    <form>
      <form method="post">
        <input type="text" name="valor1" id="valor1">
      </form>
    </form>
    
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
   <script>
   $("#valor1").change( function(){
      
      $.ajax({
        type: 'POST',
        url: "salvarNaSessao.php",
        data: 'valor' + $("#valor1").val()
      }).done(function() {
        alert('dados enviados');
      });
      
    });
   </script>
  </body>
</html>

Save pageNew.php

<?php
session_start();

if(isset($_POST['valor1'])){
   $_SESSION['valor1'] = $_POST['valor1'];
}
?>
  • following this example, I did not get the result in the php session variable. it adds a variable (value1) in the url, e.g.: ? value1=677666.

  • Missed type: "POST" in the answer why is sending GET

  • @Groot Fixed the code....

Browser other questions tagged

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