skip to Main Content

Trying to convert $_GET to $_SESSION but alot of them using php for loop!

But i cant get it to work!

for ($x = 0; $x <= 2; $x++) {
$_SESSION["u'.$x.'_first_name"] = $_GET["u'.$x.'_first_name"];
}

2

Answers


  1. Chosen as BEST ANSWER

    Everyone was right. It works already, maybe i have some intefering $x somewhere else that was making the code not to work. As when i run it by it self, its all good!

    for ($u = 1; $u <= $_GET['amount']; $u++) {
    $_SESSION['u'.$u.'_first_name'] = $_GET['u'.$u.'_first_name'];
    echo 'session : '.$_SESSION['u'.$u.'_first_name'].'<br>';
    }
    
    echo '
    <form action="" id="formBook">
    Amount : <input id="amount" name="amount" value="'.$_GET['amount'].'"><br>
    ';
    
    for ($x = 1; $x <= $_GET['amount']; $x++) {
    echo '
    Name '.$x.' : <input type="text" class="form-control rounded-0" id="u'.$x.'_first_name" name="u'.$x.'_first_name" value="'.$_SESSION['u'.$x.'_first_name'].'"><br>
    ';
    }
    
    echo '
    <input class="btn btn-primary mb-3" type="submit" name="submit" value="Next" class="button"><br>
    ';
    

    https://ideone.com/sodDBm (a fiddle for it)

    Thanks everyone for your help!


  2. I think this what you want

        for ($x = 1; $x <= 2; $x++) {
        $_SESSION['u'.$x.'_first_name'] = $_GET['u'.$x.'_first_name'];
        }
    

    Your single quotes were bracketing the $x.

    And I have started the loop counter at 1 (from the comments).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search