Solved

storing data into an array variable from a form

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

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

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?
0
Comment
Question by:jabrthel
  • 2
  • 2
5 Comments
 
LVL 10

Expert Comment

by:wildzero
Comment Utility
for( $i=0; $i<count($_REQUEST['keepIt']); $i++){
$keepIt[] = $_REQUEST['keepIt[$i]'];
}

print_r($keepIt);

that better?

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

Make sense?
0
 

Author Comment

by:jabrthel
Comment Utility
It still didn't work... maybe if I show you the full code of what I'm trying to do.
pokerDice.txt
0
 
LVL 6

Accepted Solution

by:
psychic_zero earned 500 total points
Comment Utility
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">

  <tr>

<?

for ($i=1;$i<=5;$i++)

{

	$die[$i] = "Value $i";

?>

	<td>

    <input type="checkbox" name="keepIt<? echo($i) ?>" value="<? echo($die[$i]) ?>"> <? echo($die[$i]) ?>

    </td>

<?

}

?>

  </tr>

</table>

<p>

  <input name="cntarray" type="hidden" value="<? echo($i-1) ?>">

  <input name="Submit" type="submit" value="Submit">

</p>

</form>
 
 

// In test2.php
 

<?

$cntarray = $_REQUEST['cntarray'];
 

for( $i=1; $i<=$cntarray; $i++)

{

	$keepIt[$i] = $_REQUEST['keepIt'.$i];

	echo($keepIt[$i]."<br>");

}

?>

Open in new window

0
 

Author Comment

by:jabrthel
Comment Utility
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?
0
 
LVL 6

Expert Comment

by:psychic_zero
Comment Utility
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...
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now