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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to store check box values in sessions

Status
Not open for further replies.

keith23

Technical User
May 26, 2005
97
NL
Hi all i got a few checkboxes and i let the user to select a few of them and then click a submit button. I want store the value of select checkbox in sessions and later one use them. could any one show me how i can do that.Thanks

Code:
<form action="./write.php" method=post >

<input type="checkbox" name="id" [b]value="1"[/b]
<input type="checkbox" name="id" value="2"
<input type="checkbox" name="id" value="3"

<input type="submit" value="Add this/these Songs to my PlayList" name="B1">
 
On the next page use [tt]$_SESSION['id'] = $_POST['id'];[/tt] to move it to the session variable. Make sure you initiate sessions by calling [tt]session_start()[/tt] before that. That is it. If you need to remember more than one value then run foreach on the next page to iterate through the values and write each of them to the session in the loop.
 
you won't be able to iterate through these values as php will only look at the last element as they are all called the same thing (actually each element will overwrite the previous element's value).

as i have said in previous posts by keith23, to make sure you can retrieve the value of each clicked checkbox you must either:
1. call each html element "id[]" or
2. use javascript to register each checked box and put the values into a textbox or something that will be separately submitted; or
3. call each checkbox something unique (eg. "id_unique_value"). you can then iterate through each form element that has the first three letters or its name = "id_"

i also note that the <input tags are not closed. my advice would be to go for option 1. if you need to address the checkbox in javascript add a unique id.

Code:
<input type="checkbox" name="id[]" value="1" id="id_1" />
<input type="checkbox" name="id[]" value="2" id="id_2" />
<input type="checkbox" name="id[]" value="3" id="id_3" />
 
jpadie i think it can be done without using checkbox i have seen sites that do exactly what i want using asp but i still did not figure how. I need some how to pass the selected values to next page. :-(
 
i don't understand your post i'm afraid. in the initial post you intimate that you are using checkboxes. but you have called them all id. categorically this will not work in php. if more than one checkbox is selected you will only get the value of the latest one.

the normal way around this is to use an array notation. I don't understand why you have a problem with this - it's neat and well understood.

there are other solutions which i have reported on above.

i do not know whether asp offers a different method. it seems irrelevant as php does not.

if you use an array notation you will have the values on the next page.

try this for an example
Code:
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="checkbox" name="id[]" value="1" id="id_1" /><br/>
<input type="checkbox" name="id[]" value="2" id="id_2" /><br/>
<input type="checkbox" name="id[]" value="3" id="id_3" /><br/>
<input type="submit" name="submit" value="submit"/>
</form>

<br/>
<?
if (isset($_POST['submit'])):
  echo "<pre>";
  print_r($_POST['id']);
  echo "</pre>";
endif;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top