0

Ok I have been fighting this for quite a while and it has been really irritating. So I need some help.

I have a drop down that allows the user to select a size for the picture they are purchasing. This value needs to saved in a php session and assigned to that particular picture, allowing the user to choose multiple pictures with a different size on each picture.

I am submitting the drop down through ajax when the user selects the size:

<script type="text/javascript">
    $('#sizeConfirm').click(function() {
        var size = $("#sizeSelector option:selected").val(); 
        var id = $("#idInput").val();
        $("#sizeId"+id).html(size);

        var dataString = 'size='+ size + '&id='+id;

        $.ajax({
            type:"POST", 
            url: "../functions/size-functions.php",
            data: dataString,
            success: function() {

            }
        })
    })
</script>

And this submits to the following script:

<?php 

   session_start();

   $id = $_POST['id'];

   $_SESSION['size'][$id] = $_POST['size'];

?>

I have used the set up in the code above for adding the pictures to the shopping cart. I placed: <?php echo $_SESSION['size']; ?> on the same page the java script is placed and I receive: Array as a result of that.

What am I doing wrong? Can someone help me out?

Basically, I want to be able to do the following:

User selects Category  -- This works 
  User selects Picture -- This works  
    User select size on cart page -- This is what the question is about. 

If it helps the select option looks like this:

<select class="form-control" id="sizeSelector">
    <option>Select a Size</option>
    <option>4x4</option>
    <option>4x6</option>
    <option>5x7</option>
    <option>6x6</option>
    <option>8x8</option>
    <option>8x10</option>
    <option>8x12</option>
    <option>10x12</option>
    <option>12x16</option>
    <option>4x18</option>                   
</select>

I wasn't sure exactly what to put in this question -- So if you believe I am missing something that would require the solution to be found, simply comment and tell me and I will add it. Thank you!

4

1 回答 1

1

因为您将会话分配为数组$_SESSION['size'][$id]

要显示结果,请尝试以下操作:

<?php
session_start();
$id = "1";//$_POST['id'];
$size="14";//$_POST['size'];
$_SESSION['size'][$id] = $size;
//print_r($_SESSION['size']);


echo $_SESSION['size'][$id];
?>

编辑:这将显示他们正在查看的每个产品的选定尺寸。

于 2013-10-13T23:16:43.247 回答