Is there any way to save the input value in a session?

1

I need to save the value entered by the user to a input without using form . I thought of SESSION , but I do not know any way to do that. Is there?

The input: <input type="text" name="valor">

I want to save on $_SESSION['valor']

    
asked by anonymous 16.03.2016 / 13:30

1 answer

2

You will need to make a request via ajax for a PHP code, I believe the best solution is js as Diego commented, so via POST it will be possible to store something in the variable $ _SESSION ['value'] in a PHP script .

Here is an example with jQuery:

$("input[name=valor]").keypress(function(){
    $.post( "salvaSession.php", { dado: $("input[name=valor]").val() } );
});

That is, every time you press a key in the field, it will send a POST request to salvaSession.php, where salvaSession.php would contain something like:

<?php
session_start(); //se ainda não começou a session.
$_SESSION['valor'] = $_POST['dado'];
?>

This is the basic concept, now you just have to improve it so there are not so many requests, maybe in the lostFocus field.

    
16.03.2016 / 13:49