Array value is replaced when second one stores in php

About First part mistakes :

    <a href="".$pdid.'&add=' .$pdid .'&size=".$si.""      class="cartBtn" onclick="return fun1()">Add to cart</a>';
  1. Your a tag is completely wrong as @Hearner said. It should be out of the php tag or inside with an “echo” like this :

    echo "<a href="".$pdid."&add=".$pdid."&size=".$si."" class="cartBtn" onclick='return fun1()'>Add to cart</a>";
  2. You cannot access your $si variable out of your if statement. As written here over, if your $si isn’t declared before (since you said that it was not your complete code…) then $si (in the link href) does not exist. You should therefore declare it before your if statement OR place your link (a tag) inside your if statement too!

  3. What if your “$_POST[‘radio’]” is NOT set ?? what happens? code missing… !! is $si declared anyway?

About Second part :

    for( $x = 0, $max = count($array); $x < $max; ++$x ) {
        $array = $_SESSION['wer'];
        echo $array[$x];

I don’t understand what you’re trying to do here… need more code/information… cannot help more without your whole code…


Here is a very simple example to show you how to keep your get vars into a session array.

page one (pageOne.php):

        $_SESSION['myTest'] = "AWESOME";
        $_SESSION['varToKeep'] = [];
        echo "A session is already started. This is : ".$_SESSION['myTest']."<br/>";
            echo "There are ".count($_SESSION['varToKeep'])." vars in the array!<br/>";
                echo "Item ".$i." : ".$_SESSION['varToKeep'][$i]."<br/>";
    echo "<br/>Click below to add a value in array<br/>";
    $random = rand(1,100);
    echo "<a href="".$random."">Click here</a>";


page two (pageTwo.php):

    echo "myTest value is : ".$_SESSION['myTest']."<br/><br/>";

    $value = $_GET['mygetvar'];
    $_SESSION['varToKeep'][] = $value;
    echo "<a href="">CLICK HERE TO RETURN ON PAGE ONE!</a>";

Browse More Popular Posts

Leave a Comment