Jump to content

Form Arrays


Chris1981

Recommended Posts

Hey All,

 

I need some help retrieving values from a form which i have created using an array.

 

below is the code i used to create the form:

 

<form action="test.php" method="post" name="form1" id="login" style="margin-top:0">        
<?php
$fieldCount = 20;
for($i = 0; $i < $fieldCount; $i++)
{
	?>
	<table width="100%" border="0" cellspacing="0" cellpadding="5" class="texts">
	<tr>
    <th width="10" scope="col">Round</th>
    <th width="10" scope="col">Date</th>
    <th width="10" scope="col">Time</th>
    <th width="10" scope="col">Opposition</th>
    <th width="10" scope="col">Bye</th>
    <th width="10" scope="col">Field</th>
  </tr>
  
  <tr>
  	<td><input name="drawRound[<?= $i ?>]" type="text" size="2" maxlength="4"></td>
        <td><input name="drawDate[<?= $i; ?>]" type="text" size="8" ></td>
        <td><input name="drawTime[<?= $i; ?>]" type="text" size="4" ></td>
        <td><input name="drawOpposition[<?= $i; ?>]" type="text" size="20"></td>
        <td><input name="bye[<?= $i; ?>]" type="checkbox" value="BYE"></td>
        <td><input name="drawField[<?= $i; ?>]" type="text" size="2"></td>
  </tr>  

</table>
<hr width="75%">  
<?php } ?>
        <table width="100%"  border="0" cellpadding="0" cellspacing="0">
                <tr align="center">
                  <td height="21" valign="bottom" align="center"><input name="imageField2" type="image" src="../Files/Buttons/submit.gif" alt="Submit & Continue" width="150" height="23" border="0"></td>
                </tr>
        </table>
<br />
</form>

 

As you can see i have made it so that the code repeats 20 times to make 20 lots of the same row of fields, except that the name of the object increases by one each time the code is repeated.

 

How would i extract those rows and turn them into variables to then be inserted into a database?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/73915-form-arrays/
Share on other sites

At first:

That code is wrong.

It should look like this:

<form action="test.php" method="post" name="form1" id="login" style="margin-top:0">        
<?php
$fieldCount = 20;
for($i = 0; $i < $fieldCount; $i++)
{
	?>
	<table width="100%" border="0" cellspacing="0" cellpadding="5" class="texts">
	<tr>
    <th width="10" scope="col">Round</th>
    <th width="10" scope="col">Date</th>
    <th width="10" scope="col">Time</th>
    <th width="10" scope="col">Opposition</th>
    <th width="10" scope="col">Bye</th>
    <th width="10" scope="col">Field</th>
  </tr>
  
  <tr>
  	<td><input name="drawRound[<? echo $i ?>]" type="text" size="2" maxlength="4"></td>
        <td><input name="drawDate[<? echo $i; ?>]" type="text" size="8" ></td>
        <td><input name="drawTime[<? echo $i; ?>]" type="text" size="4" ></td>
        <td><input name="drawOpposition[<? echo $i; ?>]" type="text" size="20"></td>
        <td><input name="bye[<? echo $i; ?>]" type="checkbox" value="BYE"></td>
        <td><input name="drawField[<? echo $i; ?>]" type="text" size="2"></td>
  </tr>  

</table>
<hr width="75%">  
<?php } ?>
        <table width="100%"  border="0" cellpadding="0" cellspacing="0">
                <tr align="center">
                  <td height="21" valign="bottom" align="center"><input name="imageField2" type="image" src="../Files/Buttons/submit.gif" alt="Submit & Continue" width="150" height="23" border="0"></td>
                </tr>
        </table>
<br />
</form>

 

And you can get them as this:

$_POST["drawRound"][0]

Just change variable name and array number.

Link to comment
https://forums.phpfreaks.com/topic/73915-form-arrays/#findComment-372996
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.