Page 1 of 1

$_POST Question

Posted: Sun Jul 05, 2009 11:13 am
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

Re: $_POST Question

Posted: Sun Jul 05, 2009 12:49 pm
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']);