storing data into an array variable from a form

Posted on 2008-06-20
Last Modified: 2011-10-19
my code prints out check boxes as such...

print <<<HERE
    <input type = "checkbox" name = "keepIt[$i]" value = $die[$i]>

There is a corresponding for loop which assigns $i the values 1-5.

I wish to retrieve this data using $_REQUEST variable, but my current code below is not working...

for( $i=0; $i<count($_REQUEST['keepIt']); $i++){
$keepIt = $_REQUEST['keepIt[$i]'];

any ideas?
Question by:jabrthel
  • 2
  • 2
LVL 10

Expert Comment

ID: 21836095
for( $i=0; $i<count($_REQUEST['keepIt']); $i++){
$keepIt[] = $_REQUEST['keepIt[$i]'];


that better?

doing the [] after $keepIt means the value goes into a new array element.

Make sense?

Author Comment

ID: 21836113
It still didn't work... maybe if I show you the full code of what I'm trying to do.

Accepted Solution

psychic_zero earned 500 total points
ID: 21836121
Try this, should be ok
// In 1st form let say test1.php
<form name="form1" method="post" action="test2.php">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
for ($i=1;$i<=5;$i++)
	$die[$i] = "Value $i";
    <input type="checkbox" name="keepIt<? echo($i) ?>" value="<? echo($die[$i]) ?>"> <? echo($die[$i]) ?>
  <input name="cntarray" type="hidden" value="<? echo($i-1) ?>">
  <input name="Submit" type="submit" value="Submit">
// In test2.php
$cntarray = $_REQUEST['cntarray'];
for( $i=1; $i<=$cntarray; $i++)
	$keepIt[$i] = $_REQUEST['keepIt'.$i];

Open in new window


Author Comment

ID: 21836143
Psychic_Zero, that works and I understand it... but just for curiosity's sake, is there any way to do it going down the path I was going?

Expert Comment

ID: 21836164
So far what I know, array cannot be passed through form, like you want to pass a form object checkbox with name keepIt[1], keepIt[2], keepIt[3],etc... and then receive back in next page using $_REQUEST with array keepIt[1],keepIt[2], etc...

What I'm doing is to send the value not in array parameter like keepIt1, keepIt2, keepIt3, etc so that I can get back in another page like this $_REQUEST['keepIt'.$i] -> keepIt1, keepIt2, etc...

The $cntarray I used to tell next page how many counter of keepIt it have. Hope you understand...

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
php function to remove a file 31 39
tutorial for ebay api 3 34
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 33
How can I make this form submit to itself? 10 27
This article will show, step by step, how to integrate R code into a R Sweave document
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question