arrays and form input

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
cutthroat
Forum Newbie
Posts: 8
Joined: Wed Apr 02, 2008 7:41 pm

arrays and form input

Post by cutthroat »

Can i retrieve data from a forms input boxes directly into an array. i.e

Code: Select all

 
<?php
      $iData=$_POST['inputData'];
      $sz=sizeof($iData);
    //ect ect
?>
 
This code works with checkboxes, but not with input boxes. Am i missing something.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: arrays and form input

Post by John Cartwright »

Code: Select all

 
<input name="foo[]" type="checkbox" value="1">
<input name="foo[]" type="checkbox" value="2">
<input name="foo[]" type="checkbox" value="3">

Code: Select all

echo '<pre>';
print_r($_POST['foo']);
echo '</pre>';
So yes, you can specify the input as an array.
cutthroat
Forum Newbie
Posts: 8
Joined: Wed Apr 02, 2008 7:41 pm

Re: arrays and form input

Post by cutthroat »

Sorry i never made myself clear. I have already done this with checkboxes, can i do the same with a text input box.
I have a series of text input boxes and have entered them like this.

Code: Select all

 
<?php
    $price[0] = $_POST['pr1'];$price[1] = $_POST['pr2'];$price[2] = $_POST['pr3'];$price[3] = $_POST['pr4'];
    $price[4] = $_POST['pr5'];$price[5] = $_POST['pr6'];$price[6] = $_POST['pr7'];$price[7] = $_POST['pr8'];
?>
 
and then dereferenced the data. It would be nice if the checkbox way worked for Text input boxes. Thanks for your quick response anyway.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: arrays and form input

Post by John Cartwright »

Yes it's possible. In fact, I arrange my forms using arrays nearly always.

You example is confusing.. can you post your form?

Did you try a print_r() on the data to see how it is organized?
cutthroat
Forum Newbie
Posts: 8
Joined: Wed Apr 02, 2008 7:41 pm

Re: arrays and form input

Post by cutthroat »

here's the relevent bit of html

Code: Select all

 
     <form action="process.php" method = "post">
     Price 1. <input type="text" name="pr1" tabindex="2" style="text-align:right;" MAXLENGTH="6" size="4">
     Price 2. <input type="text" name="pr2" tabindex="2" style="text-align:right;" MAXLENGTH="6" size="4">
     ect ect
    </form>
 
Then i use the posted code above to extract in process.php. Hope this is off help. I tried naming pr1,pr2 ect, name="pr[]", but it did'nt seem to work.
Last edited by cutthroat on Wed Apr 02, 2008 9:12 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: arrays and form input

Post by John Cartwright »

I don't see you assigning the the elements as arrays.

<input name="foobar[]" ... >

Notice the [] .. this means we want it as an element of the array (in the order we put them in unless we specify it a key). And remember, you can go deeper than 2 dimensions.

<input name="data[user][profile]" ... >
cutthroat
Forum Newbie
Posts: 8
Joined: Wed Apr 02, 2008 7:41 pm

Re: arrays and form input

Post by cutthroat »

Sorry i re-edited my post after you replied. I obviously need to slow down and step back. I will play around with a form with some data in it until i get the jist. Thanks for letting me no it is possible. And more dimensions, this could make life easy. :D
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: arrays and form input

Post by John Cartwright »

I don't believe you did a print_r() after changing your names to include the array indices ;)

Code: Select all

<form action="process.php" method = "post">
     Price 1. <input type="text" name="pr[1]" tabindex="2" style="text-align:right;" MAXLENGTH="6" size="4">
     Price 2. <input type="text" name="pr[2]" tabindex="2" style="text-align:right;" MAXLENGTH="6" size="4">
     ect ect
</form>
If you leave both of the names to pr[] like in my other example, then the keys will start counting from 0, 1, 2, 3, and so forth. I think you expected it to start from 1. In the example I just posted above, you can specify what you want the key to be, which is what you supposedly want.

Code: Select all

 
echo '<pre>';
print_r($_POST['pr']); //array(1 => "", 2 => "")
echo '</pre>';
cutthroat
Forum Newbie
Posts: 8
Joined: Wed Apr 02, 2008 7:41 pm

Re: arrays and form input

Post by cutthroat »

Bang on, you got it. Problem solved. The little things in life that get you down. Thanks for your help. :D
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: arrays and form input

Post by John Cartwright »

cutthroat wrote:Bang on, you got it. Problem solved. The little things in life that get you down. Thanks for your help. :D
Excellent. Enjoy the new found knowledge :)
Post Reply