Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

session variables + checkbox

Status
Not open for further replies.

judi

Programmer
Jan 13, 2001
35
US
I'm using an input tag for a checkbox. If someone checks the box, I want the value put into a session variable. My input box is as follows:

echo &quot;<input type = \&quot;checkbox\&quot; name = \&quot;$deleteitem\&quot; value=1 >&quot;;

I want to put it in the session array: $_SESSION[&quot;deleteitem&quot;][$i]

It won't let me use that for the name in the input statement.

I also have a submit button that takes me to another script, but I can't capture the value. I've tried registering it the old way with session_register(&quot;$deleteitem&quot;); But it still doesn't work.

Thanks for any help you can give me.

 
You need to access the value first using post or get

$_SESSION['deleteitem'][$i]= $_POST['deleteItem'];

I would suggest that you make a array to store both the key (the item name) and the value. That way you can tell what the value and the item names are. Leaves less to the imagination and makes for easier debugging

Bastien

Any one have a techie job in Toronto, I need to work...being laid off sucks!
 
Instead of setting the input element's name to $deleteitem I suggest to use an Array and set the var as value:
Code:
echo &quot;<input type = \&quot;checkbox\&quot; name = \&quot;deleteme[]\&quot; value=&quot;$deleteitem&quot; >&quot;;
This will POST an array of item ID's to be deleted in the array $_POST['deleteme'].
 
where can I access the array $_POST['deleteme']? Can I access it in the script designated by the form? And how do I write it? Where do I put the subscript for $_POST['deleteme']f? No matter what I try, I don't get a value.
 
In the script that is pointed to by the &quot;action&quot; attribute of your form, $_POST is available everywhere.

You might just try outputting all of $_POST in your script to see what you're getting:

<?php
print '<pre>';
print_r ($_POST);
?>

Want the best answers? Ask the best questions: TANSTAAFL!!
 
This is what I get when I do a print_r($POST);

Array ( [Array] => 1 [submitter] => Delete Checked Items )

submitter is the name on my submit button.

All I want is the subscript of the items that have been checked for delete.

 
I take it then that $deleteitem is an array? If so, you're going to have to loop through the array, producing an <input...> tag for each element of the array.

Each <input> can have a name that is like an array reference&quot;

<input type=&quot;checkbox&quot; name=&quot;deleteme[1]&quot;>
<input type=&quot;checkbox&quot; name=&quot;deleteme[2]&quot;>
<input type=&quot;checkbox&quot; name=&quot;deleteme[3]&quot;>
<input type=&quot;checkbox&quot; name=&quot;deleteme[4]&quot;>

In which case $_POST['deleteme'] will itself be an array in the script to which your form's &quot;action&quot; attribute points. The trick is making the subscripts meaningful to your application.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
the $deleteitem is an array. I'm using a bunch of parallel arrays. What I'm doing is displaying a list of items with one of the fields a checkbox. I only need one checkbox the user can check if he wants the item deleted. I want to turn the $deleteitem into a session array. After I code the input statement, I have some submit buttons. They go to another script just to see what their value is. If the value is delete, I want to return to the original script, delete the items and redisplay them without the delete items.
 
Perhaps an example of what I'm trying to say will help....

The following script takes an array consisting of two-element subarrays consisting of item_name and item_id.

If it gets no input, it produces a form that for each element in $items, outputs the item name and a checkbox named &quot;delete[<item id>]&quot;.

If it gets input, it displays the contents of $_POST.

Code:
<?php
$items = array
(
   array('item_name' => 'lambs',             'item_id' => 1),
   array('item_name' => 'sloths',            'item_id' => 2),
   array('item_name' => 'carp',              'item_id' => 3),
   array('item_name' => 'anchovies',         'item_id' => 4),
   array('item_name' => 'orang-utans',       'item_id' => 5),
   array('item_name' => 'breakfast cereals', 'item_id' => 6),
   array('item_name' => 'fruit bats',        'item_id' => 7)
);

print '<html><body>';

if (isset($_POST['delete']))
{
   print '<pre>';
   print_r ($_POST);
   print '</pre>';
}
else
{
   print '<form method=&quot;post&quot; action=&quot;test_script.php&quot;>
      <table>
         <tr>
            <td>Item</td>
            <td>Delete?</td>
         </tr>';
   
   foreach ($items as $item)
   {
      print '
            <tr>
               <td>' . $item['item_name'] . '</td>';
               
      print '
               <td><input type=&quot;checkbox&quot; name=&quot;delete[' . $item['item_id'] . ']&quot;></td>
            </tr>';
   }
   
   print '
         <tr>
            <td colspan=&quot;2&quot;>
               <input type=&quot;submit&quot;>
            </td>
         <tr>
      </table>
   </form>';
}

print '</html></body>';

?>


Want the best answers? Ask the best questions: TANSTAAFL!!
 
I'm still confused. I'm sorry. Let me try again:

I have a for loop which is displaying a list of items chosen. Here is the checkbox for the input:

for ($i = 1; $i <= $NumItems ; $i++ and $empty == 0) {
echo &quot;<TR>&quot;;
// there are other items in this list
printf (&quot;<TD>%s&quot;, $_SESSION[&quot;cart&quot;][$i]->color);
printf (&quot;<TD>%d&quot;, $_SESSION[&quot;cart&quot;][$i]->qty);
echo &quot;<TD>&quot;;
echo &quot;<input type = \&quot;checkbox\&quot; name = $deleteitem[$i] value=1 >&quot;;
} // end for

I have multiple submits so the code goes to a switch.php script to parse out all my submits and tells me where to go.

For the delete checkbox, I return to the original script and delete those items. My questions are as follows

1 - Does the POST array will contain just those values that are checked? when I do a count($_POST['deleteitem']); it always has the value 2 no matter how many items I want to delete.
2 - I want to use the POST array as part of a loop that reads the subscript, compares it with the items and deletes those items from the arrays. But I am having trouble addressing the subscript of the array so I can compare it. How can I display the contents of the $_POST array so I can get at the subscript or at least compare one of the fields in the $_POST array with my parallel arrays?

More then likely, I'm making this harder then it needs to be. I'm sure I'm close but I need a little help. Thanks.



 
It's this line in your snippet that concerns me:

echo &quot;<input type = \&quot;checkbox\&quot; name = $deleteitem[$i] value=1 >&quot;;

What is the array $deleteitem?

What you want to do is create a form that, when submitted, produce in $_POST an array of all the checked items. (Yes, only checked checkboxes are submitted.) To do this, you need to name them as pseudo-arrays in HTML.

An example. Suppose you have the following HTML:
<html><body>
<form method=&quot;post&quot; action=&quot;somescript.php&quot;>
1<input type=&quot;checkbox&quot; name=&quot;foo[1]&quot;><br>
2<input type=&quot;checkbox&quot; name=&quot;foo[2]&quot;><br>
3<input type=&quot;checkbox&quot; name=&quot;foo[3]&quot;><br>
4<input type=&quot;checkbox&quot; name=&quot;foo[4]&quot;><br>
5<input type=&quot;checkbox&quot; name=&quot;foo[5]&quot;><br>
<input type=&quot;submit&quot;>
</form></body><html>

If a user checks off the first, third, and fifth boxes and posts the form, in the script somescript.php, $_POST['foo'] will itself be an array containing:

Array
(
[1] => on
[3] => on
[5] => on
)

If these subscripts match the item numbers in your shopping cart, then it is a simple matter to loop through the array keys of $_POST['foo'] and delete those items in the shopping cart whose item numbers match the keys returned.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
The array $deleteitem is just a list of checkboxes associated with model, category, etc. If the user checks one, it should delete the whole item. The input statement is in a For loop and it creates the checkbox dynamically depending on how many items the user has selected.

When I've tried to dump $deleteitem, I don't get anything. I've tried it with $_POST['deleteitem']. I've used foreach too. I know I'm doing (or not doing) something incredibly stupid.
 
this is what I get with that:

Array ( [] => )

I am going to an intermediate script after the user clicks on the delete button. I have to do this because I have multiple submit buttons. But that shouldn't make a difference since the $_POST is supposed to exist for the duration of the session.
 
No, it doesn't. $_SESSION lasts for the duration of a session. $_POST lasts for the duration of the run of one script to which data has been posted.

If you use something like a &quot;Location:&quot; header to redirect your users, then you're going to lose $_POST. A &quot;Location:&quot; header causes the browser to immediately sever the current connection and start a new one to the location specified. And the browswer does not repost the data.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
that's obviously my problem. I've tried putting a session variable in the input statement, but it gives me an error. So where do I move the $deleteitem[$i] to a session variable? It seems that when the user clicks the submit button, it goes right to the other script, thus destroying my $_POST variable.
 
Somewhere in your code before you bump your user to another script, you could put something like:

$_SESSION['deleteitem'] = $deleteitem;

That would store the data.


I also recommend that you take a look at the use of PHP's include() function. This allows you to insert, in place, code to be run. It works just as if you had included the code typographically. But you keep your variable context as you do it.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
I've done that. If I do that before the submit button, the value isn't there yet. If I do it after the submit button, it doesn't see it since it's gone to a new script.

I'm already using include. No matter what submit button is clicked, it goes to a switch.php. There depending on the button, it will include another script. But by then the checkbox is destroyed. Can I send the value to the switch.php someway?
 
I'm not sure what you mean about before and after the submit button.

When I want a script to either output a form for input or process input from the form it previously output, I have the script check for input and react accordingly:

<?php
if (isset($_POST['foo'])
{
//process input
}
else
{
//output a form with an input named &quot;foo&quot;
}
?>


Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top