$_POST Question

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
ktownjack
Forum Newbie
Posts: 1
Joined: Sun Jul 05, 2009 11:01 am

$_POST Question

Post by ktownjack »

Greetings:

I am a newbie to this forum. I am haing a problem accessing the individual elements in a form array. For example, below is a simple example:

The form contains the following

Code: Select all

 
<input type="text" name="material[0]" />
<input type="text" name="material[1]" />
<input type="text" name="material[2]" />
<input type="text" name="material[3]" />
 
So I have the array material that is numerically indexed. Now when I do a phpinfo(0), the result shows:

Code: Select all

 
POST["material"] Array
(
    [0] => 65
    [1] => 8
    [2] => 8
    [3] => 15
)
 
Now, the problem I am having is I cannot access each inividual value in the material array. For example, here is my simplified code;

Code: Select all

 
$i=0;
while ( $i < 4 ) {
  $material=$_POST['material[$i]'];
  $i++;
}
 
when I print $material within the loop, it is empty.

I then tried this:

Code: Select all

 
$i=0;
while ( $i < 4 ) {
  $material=$_POST['materia'][$i]];
  $i++;
}
 
Again, nothing???

Thanks in advance
Last edited by Benjamin on Mon Jul 06, 2009 12:10 pm, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: $_POST Question

Post by JAB Creations »

1.) Welcome to DevNetwork.

2.) This is not a coding forum if you read the forum description.

3.) It looks like you have JavaScript on the brain. You are currently posting $_POST['material[0]'] which does not exist, isn't valid (X)HTML, etc. You're clearly not validating your code.

So...

1.) Get Firefox.

2.) Install Chris Pederick's Web Developer Toolbar.

3.) Use the toolbar's tools menu to validate your (X)HTML.

4.) Give each name attribute a unique value (name="material_1", name="material_2", etc).

5.) Escape data to prevent SQL inject attacks...

Code: Select all

$material_1 = mysql_real_escape_string($_POST['material_1']);
Post Reply