So...

I have a form with 14 sets of identical fields to track penalties in a sporting event. Each field allows the selection of a name, penalty, severity and number of times it was repeated.

That is all well and good, but I would like to read each of these 14 lines into an array on the form processing script. The bugger I have is I can't crack the syntax to allow me to us an $i variable to read from the form fields.

So, for example, each line submits a numbered value: Player1, Infraction1, Level1, Repeat1... Player2, Infraction2, Level2, Repeat2... Player3, Infraction3, and so on.

I wanted to read these into the array like so, but I don't know how I can incorporate the incremental variable into the post name:

for ($i=1;$i<=14;$i++)
{
	$penPlayer[$i] = $_POST['player$i'];
	$penInfract[$i] = $_POST['infraction$i'];
	$penLevel[$i] = $_POST['level$i'];
	$penRepeat[$i] = $_POST['repeat$i'];
	echo "$i $penPlayer[$i] $penInfract[$i] $penLevel[$i] $penRepeat[$i]<BR><BR>";
}

This code doesn't work. It doesn't like:
$penPlayer[$i] = $POST['player'$i];
nor
$penPlayer[$i] = $
POST[player$i];

So how do you use an incremental variable to roll through these various post variables?

Thanks!

\V

    No, there are more simple way to resolve your problem:

    <form>
              <input type='text' name='user[]'/>
              <input type='text' name='user[]'/>
              <input type='text' name='user[]'/>
    </form>
    

    HTML forms support arrays of data too.

    Proceeding on serverside:

    <?php
    //....
    for($i=0;$i<count($_POST['user']);$i++)
    {
          print $_POST['user'][$i];
    }
    //....
    ?>
    

      Also for future reference use double quotes rather than single when trying to do what you are currently attempting. But the above post is right it is the easier way.

        Write a Reply...