1

Since days I have tried to solve a problem but it hasn't still succeeded so far.

The solution would be that I choose a number from 0 till 5 from a SELECT and in this case input fields appear depending on the numbers, e.g. if I choose 3 from the SELECT, 3 input fields can be seen.

After that I would like to store data in these input fields. The actual solution that with Ajax I transfer the data to PHP, which stores the given value in a session cookie. The problems begin from here.

I would like to implement if a user arrives my webpage stores some activities from the visitors which are used later if they visit the page again. The basic thought is that I use SESSION during their visiting time until they don't delete the cookies.

Once I did that PHP create the session and it is stored but if I reload the site, the session is lost.

Actually I would like to implement: - store the SELECT value in a SESSION till deleting it, so repeat visitor can be identified. - also store the input fields in a SESSION - if a guest visits the site again and e.g. the SELECT is 4, then in the SELECT number 4 be chosen and further 4 input fields appear with the stored values.

I don't think the PHP is the best solution... Could you help me?

Thanks for your all helps in advance.

Best Regards, Atti

<?php
session_start(); 
echo $_SESSION['gyk1'];
?>
<p id="gyszam"></p>
<div class="row">    
    Type
        <select name="type" id="type">
                <option name="gy0" value="0">0</option>
                <option name="gy1" value="1">1</option>
        </select>                    
</div>

<div class="row" id="gyk1">

<?php 
if (isset($_SESSION['gyk1'])) {
    $session_gyk1 = $_SESSION['gyk1'];
    echo "<input type='number' id='gy1' class='gykk1' name='gyn1' min='0' max='17' value='$session_gyk1' ><br>";

    } else {
    echo "<input type='number' id='gy1' class='gykk1' name='gyn1' min='0' max='17' /><br>";

    }
?>


</div>
<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script>
    $(function() {
        $('#gyk1').hide(); 
        $('#type').change(function(){
            if($('#type').val() == '1') {
                $('#gyk1').show(); 
            } else {
                $('#gyk1').hide(); 
            } 
        });
    });

    $(function() {
        $("input#gy1.gykk1").change(function(){
            var gykk1 = $(this).val();


            $.ajax({
                type: "POST",
                url: "php.php",
                data: {gyk1: gykk1},
                success: function(records){
                    $("#gyszam").html((records));
                }       
            });
        });
    });

</script> 

PHP file:

<?php
session_start(); 
$gyk1 = $_POST['gyk1'];
$_SESSION['gyk1'] = $gyk1;
?>
Atti
  • 33
  • 1
  • 8
  • You may us cookie to save value till the next visit – splash58 May 02 '15 at 19:14
  • 1
    may help http://stackoverflow.com/questions/11287041/session-v-cookie ... http://stackoverflow.com/questions/4478805/what-is-difference-between-session-and-cookie-in-php – Mohamed-Yousef May 02 '15 at 19:14
  • It is actually legit to use $_SESSION for such cases. – sitilge May 02 '15 at 20:08
  • okay, I use $_SESSION, but if I push F5 then I lose this SESSION variables. – Atti May 02 '15 at 20:13
  • A session and therefore all its data is destroyed after the user stops interacting with your site. So holding data in a session between visits is not possible. You could use cookies, but they can be disabled by the browser. So you need another mechanism – RiggsFolly May 02 '15 at 20:15
  • yes, I understand the difference between cookies and sessions. RiggsFolly, what would be the another mechanism? – Atti May 02 '15 at 20:20
  • @RiggsFolly Incorrect. A session is destroyed when it is removed from storage (on server) by whatever is managing the session ([PHP has a bunch of settings to control this](http://php.net/manual/en/session.configuration.php)). Usually sessions are associated to users via cookies. If the cookie is removed when the browser is closed (a common default), then the association is lost and as far as the user is concerned, their session is "lost". Cookies can also be set to expire, or users can delete/disable them, which all have the same effect. – gregmac May 02 '15 at 20:23
  • @Atti `var_dump($_SESSION)` is your friend, as is the *Resources* panel in your browsers dev console (where you can see cookie details). Quick test to validate sessions are even working on your server, create a file with just: ``. It should start at 0, and every time you reload, increment by one. If you delete cookies/open another browser or private/incognito window, it'll start at 0 in that browser. If that doesn't work, you need to look at your server-side session configuration. – gregmac May 02 '15 at 20:29
  • Put the data you want to remember in a database, associate this with the users login. Anything else is unreliable or can be accidentally or intentionally destroyed by the user. – RiggsFolly May 02 '15 at 20:37
  • @gregmac I was trying to keep it simple for an obvious beginner. – RiggsFolly May 02 '15 at 20:38

0 Answers0