Jump to content

How do I access an array outside of if?


StayG
 Share

Recommended Posts

When I send values to the foreach they are not recorded in the array.

$x = $getProductID;
$_SESSION['pColors'] = array();

if(isset($_POST['pColors'])) {

    foreach($_POST['pColors'] as $colorKey => $RColors) {
        $_SESSION['pColors'][$colorKey] = $RColors;
    }
}

var_dump($_SESSION['pColors']);

Send date with AJAX:

                $('.addToCart').click(function(){
                    $.ajax({
                        url:""+realLink+"cart.php",
                        method:"POST",
                        data:{action:action,pColors:pColors},
                            success:function(data){
                               //alert(data);
                            }
                    });
                });
Link to comment
Share on other sites

array(1) {
  [3]=>
  array(4) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
    [2]=>
    string(1) "3"
    [3]=>
    string(1) "4"
  }
}
It gets the values, but when it comes to the next spin, foreach replaces them.
$i = 0;
foreach($_SESSION['pID'] as $getProductID) {

	$x = $getProductID;
	//$_SESSION['pColors'] = array(); > this is when array is outside and stay empty 

	if(isset($_POST['pColors'])) {
		$_SESSION['pColors'] = array(); // > this is when array is in and get values
		foreach($_POST['pColors'] as $colorKey => $RColors) {
			$_SESSION['pColors'][$x][$colorKey] = $RColors;
		}
	
	var_dump($_SESSION['pColors']);

	$i++;
}

 

In each case, values come to pColors but they are not recorded in the array.

Edited by StayG
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.