How to send a URL paramter in the form of an array ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

How to send a URL paramter in the form of an array ?

Post by anjanesh »

<FORM ACTION="Register.php">
<INPUT name=somename1>
<INPUT name=somename2>
<INPUT name=somename3>
<INPUT name=somename4>
<INPUT name=somename5>
<INPUT name=somename6>
<INPUT name=somename7>
<INPUT name=somename8>
<INPUT name=somename9>
<INPUT name=somename10>
<INPUT type="submit" value="Submit" name=Submit>
</FORM>


Is there anyway to send this as an array so that I can access it in the php code as

Register.php
for ($i=1;$i<=10;$i++)
{
print its name & value
}


somename1 value1
somename2 value2
somename3 value3
somename4 value4
somename5 value5
somename6 value6
somename7 value7
somename8 value8
somename9 value9
somename10 value10

Thanks
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Code: Select all

<pre>
<?php
print_r($_POST);
?>
<form method="post">
<input type="text" name="somename[]">
<input type="text" name="somename[]">
<input type="text" name="somename[]">
<input type="text" name="somename[]">
<input type="text" name="somename[]">
<input type="text" name="somename[]">
<input type="text" name="somename[]">
<input type="text" name="somename[]">
<input type="text" name="somename[]">
<input type="submit" value="submit" name="submit">
</form>
I dont name them with numbers, as I know that index 0 in the array being sendt is the first somename[], 1 the second etc.
If nothing is entered in the fields, they end up as empty, but yet visible so the keys are intact when dealing with the array.

Hope I made sence ;)

[Edit: Corrected spelling. --JAM]
Last edited by JAM on Fri Jan 02, 2004 11:33 am, edited 1 time in total.
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Yes you can but you will need to change a couple of things. First of all change all of the inputs to:

Code: Select all

<?php
<FORM ACTION="Register.php" method="post">
<INPUT name="somename[]" type="text">
<INPUT name="somename[]" type="text">
<INPUT name="somename[]" type="text">
<INPUT name="somename[]" type="text">
<INPUT name="somename[]" type="text">
<INPUT name="somename[]" type="text">
<INPUT name="somename[]" type="text">
<INPUT name="somename[]" type="text">
<INPUT name="somename[]" type="text">
<INPUT name="somename[]" type="text">
<INPUT type="submit" value="Submit" name="Submit">
</FORM>
?>
This will create an array of $_POST['somename'] on the next page. You can then loop through the array using foreach, like this:

Code: Select all

<?php
        $i = 0;
        foreach($_POST['somename'] as $value)
        {
                echo "somename[$i] - $value<br>\n";
                $i++;
        }
?>
Edit:
Damn, Jam beat me to it :wink:

Edit:
Yah well, you did continue on it, that i forgot. ;)
Oh, and added " around values as in my post. -- JAM
Post Reply