0

I have the following code. I am trying to keep track of selected items and display them down from array.

<?php
session_start();

//temp stores the submitted value
$temp = $_POST['field'];

if (!isset($_SESSION['itemsList'])) {
    $itemsList = array();
} else {
    $itemsList = $_SESSION['itemsList'];
}

//check how many elements in array
$arraySize = count($itemsList);

//set that as i and then add after that
$emptyval = "";

if (strcmp($temp,$emptyval)!=TRUE) {
    array_splice($itemsList, $arraySize+1, 0, $temp);
    unset($temp);
    unset($_SESSION['field']);
    $_SESSION['itemList'] = $itemList;
}
?>
<html>
<head>
    <title>Test Page Khalid</title>
</head>
<body>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">

    <table height="100%" width="100%" cellspacing="10" cellpadding="10" border="1">
        <tr>
            <td align="center"><input name="field" value="shirt" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="pants" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="socks" style="width:200px; height:100px;" type="submit"/></td>
        <tr>
        <tr>
            <td align="center"><input name="field" value="dress" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="skirt" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="topbody" style="width:200px; height:100px;" type="submit"/></td>
        <tr>
        <tr>
            <td align="center"><input name="field" value="sheets" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="pillowcover" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="blanket" style="width:200px; height:100px;" type="submit"/></td>
        <tr>        
    </table>
</form>
<br><br><br>
<?php
$itemsReturned = $_SESSION['itemList'];
echo "The items stored are: <br>";
print_r($itemsReturned);
?>

</body>
</html>

Any idea why this is not displaying anything? Thanks,

6
  • What do you mean 'not displaying anything'? Is nothing coming up at all? Commented Apr 10, 2012 at 13:13
  • Turning on error reporting will allow people to provide more help. Commented Apr 10, 2012 at 13:13
  • Try the everyday debugging stuff, then supply that info: Is it getting into the IF-block where it sets the variable? If so, what's the value immediately after it's set? How about right after the IF-block...etc. Commented Apr 10, 2012 at 13:14
  • well the html is but nothing from the items in array...am i doing something wrong in script? Commented Apr 10, 2012 at 13:14
  • 2
    In your second if-statement, you are using $_SESSION['itemList'] and $itemList and in other places it's itemsList (looks like you forgot the s in the second if-statement). Also, be careful when setting the action of your form to $_SERVER['PHP_SELF']. This is vulnerable to cross-site scripting. Commented Apr 10, 2012 at 13:20

1 Answer 1

2

Is this what your trying to achieve:

<?php
session_start();
//store submitted value
$val = $_POST['field'];
//create a reference to the session
$items = (!isset($_SESSION['items']) ? array() : $_SESSION['items']);

//did the user submit a value?
if($val){
//append the value to the items array, contained within the session
    $_SESSION['items'][] = $val;
//update the reference
    $items =& $_SESSION['items'];
}

?>
<html>
<head>
    <title>Test Page Khalid</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

    <table height="100%" width="100%" cellspacing="10" cellpadding="10" border="1">
        <tr>
            <td align="center"><input name="field" value="shirt" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="pants" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="socks" style="width:200px; height:100px;" type="submit"/></td>
        <tr>
        <tr>
            <td align="center"><input name="field" value="dress" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="skirt" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="topbody" style="width:200px; height:100px;" type="submit"/></td>
        <tr>
        <tr>
            <td align="center"><input name="field" value="sheets" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="pillowcover" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="blanket" style="width:200px; height:100px;" type="submit"/></td>
        <tr>        
    </table>
</form>
<br><br><br>
<?php
echo "The items stored are: <br>";
print_r($items);
?>

</body>
</html>

Can you provide detail as to the purpose of the following:

$emptyval = "";

if (strcmp($temp,$emptyval)!=TRUE) {
    array_splice($itemsList, $arraySize+1, 0, $temp);
    unset($temp);
    unset($_SESSION['field']);
    $_SESSION['itemList'] = $itemList;
}
Sign up to request clarification or add additional context in comments.

5 Comments

I also agree with Travesty3, if using the $_SERVER['PHP_SELF'] superglobal, it needs to be escaped.
well here I am checking if temp is not equal to empty "". if it isnt, then add temp value to array in the next free slot (index), and store the itemsList to session
The function you have used is for comparing string values, this is overkill for just checking a values state. A better method would be the use of uk3.php.net/manual/en/function.empty.php.
I have an issue with the same code exactly. Now when I select an item, it reloads the page (because of self_submit). but the item doesn't appear in the list. THen I choose the same item again, the page loads with the item in list. Then I go an select another item, but what appears in the list this time is the lost entry of first item (as if it was cached and delayed). Any idea what such a thing would happen??
I can't say I've seen an instance of that before. I've updated the code to make use of the $items var set. In addition I forgot to echo out the form action, however the changes shouldn't reflect a functional change. You definitely haven't changed anything? Have you tried in a different browser?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.