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

how to collect form variables in a array

Status
Not open for further replies.

hos2

Programmer
May 6, 2002
418
NL
perhaps this question is already mentioned but I can't access it. when I do a search I found a similar question but I can't read the replies :(


I have a form with
<INPUT TYPE=text name=1 size=4>
<INPUT TYPE=text name=2 size=4>
<INPUT TYPE=text name=3 size=4>
<INPUT TYPE=text name=4 size=4>
...
<INPUT TYPE=text name=99 size=4>

I want if values are filled in at some places after a submit to loop through the variables and only put the variabels which have an value filled in to be put in an array together with the name.

I had found some scripts which should do the trick bu I can't get them to work :(

has anyone got an simple example on how to do this ???





 
Try this:
<?php
while (list($key, $value) = each($_POST))
{
if (empty($value))
unset($_POST[$key]);
}
?>
Then $_POST should contain all POST variables with a value sent to the script. //Daniel
 
Simple answers:

1. Since PHP cannot have a variable named after a number (or even beginning with a number), then the following will not work:

<INPUT TYPE=text name=1 size=4>
<INPUT TYPE=text name=2 size=4>
<INPUT TYPE=text name=3 size=4>
<INPUT TYPE=text name=4 size=4>
...
<INPUT TYPE=text name=99 size=4>

2. But, PHP allows you to name textinputs using array element style:

<INPUT TYPE=text name=&quot;entry[1]&quot; size=4>
<INPUT TYPE=text name=&quot;entry[2]&quot; size=4>
<INPUT TYPE=text name=&quot;entry[3]&quot; size=4>
<INPUT TYPE=text name=&quot;entry[4]&quot; size=4>
...
<INPUT TYPE=text name=&quot;entry[99]&quot; size=4>

Or even

<INPUT TYPE=text name=&quot;entry[]&quot; size=4>
<INPUT TYPE=text name=&quot;entry[]&quot; size=4>
<INPUT TYPE=text name=&quot;entry[]&quot; size=4>
<INPUT TYPE=text name=&quot;entry[]&quot; size=4>
...

The second sample just pushes each text input onto the array as the last element, resulting in a numerically indexed array, starting at $entry[0] and iterating up to the number of form inputs processed. This is a great way of handling checkboxes, for example. Remember: PHP, as most programming languages, likes to start arrays at 0, not 1.

And, if I recall correctly, only inputs with values will be added to the array, so if you use the second method above, let's say you have 10 inputs, and the user fills out the first one and the last one, then your array will be $entry[0] and $entry[1]. -------------------------------------------

&quot;Now, this might cause some discomfort...&quot;
(
 
the first option doesn't work somehow, but the second is great. I made the mistake to use numbers as varname.

 
To elaborate on rycamors post:

To get text field responses into and array :
<INPUT TYPE=text name=&quot;entry[1]&quot; size=4>
<INPUT TYPE=text name=&quot;entry[2]&quot; size=4>
<INPUT TYPE=text name=&quot;entry[3]&quot; size=4>
<INPUT TYPE=text name=&quot;entry[4]&quot; size=4>
============================================
To loop through array after submit (if using php 4)
use the foreach loop:
<?
foreach($entry as $key => $value){
If ($value!=&quot; &quot;){
echo &quot;key: &quot;.$key.&quot;<br>value:&quot;.$value.&quot;<br><br>&quot;;
}
}
?> =================================
Imagination is more important than knowledge.
(A.E.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top