I have a an online form which lists pitcher information for my child's baseball league. The form allows for multiple pitchers to be entered (an unknown quantity). I'm having problems looping through the results to get them emailed to me.
Here's the code from the form (this is for pitcher1 - up to 10 pitchers per form):
<tr bgcolor=#cfcfcf>
<td align=center><b>Team</td><td align=center><b>Pitcher Name</td>
<td align=center><b>Age</td><td align=center><b>Pitches</td></tr>
<tr>
<td><select name="pitcherteam1" size="1">
<option>PLEASE SELECT TEAM
<option>Team 1</option>
<option>Team 2</option>
<option>Team 3</option>
</select></td><td>
<input type=text size=50 name="pitcher1"></td>
<td><select name="pitcherage1" size="1">
<option>AGE
<option>8
<option>9
</select></td>
<td><input type="text" size=3 name="pitcherpitches1"></td></tr>
Here's what I have in my php email function:
$size_array = count($_POST['pitcher[]']);
for ($i=0; $i<$size_array; $i++){
$message .= <<<MESSAGE
<tr><td>$_POST['pitcherteam'][$i]</td>
<td><b>$_POST['pitcher'][$i]</td>
<td><b>$_POST['pitcherage'][$i]</b></td>
<td><b>$_POST['pitcherteam'][$i]</b></td></tr>
MESSAGE;
}
I'm hoping to loop through the form and target only rows that have a pitcher input (vs. echoing all 10 rows each time and having blank/bad data in the non-filled in fields).
Change your inputs to use the [] syntax instead of setting a number:
<tr bgcolor=#cfcfcf>
<td align=center><b>Team</td><td align=center><b>Pitcher Name</td>
<td align=center><b>Age</td><td align=center><b>Pitches</td></tr>
<tr>
<td><select name="pitcherteam[]" size="1">
<option>PLEASE SELECT TEAM
<option>Team 1</option>
<option>Team 2</option>
<option>Team 3</option>
</select></td><td>
<input type=text size=50 name="pitcher[]"></td>
<td><select name="pitcherage[]" size="1">
<option>AGE
<option>8
<option>9
</select></td>
<td><input type="text" size=3 name="pitcherpitches[]"></td></tr>
Then remove [] from the PHP because PHP automatically parses those into a native array. Also have a check so that you ignore the row if the pitcher is blank.
$size_array = count($_POST['pitcher']); // <-- notice there's no []
for ($i=0; $i<$size_array; $i++){
if($_POST['pitcher'][$i] != ''){
$message .= <<<MESSAGE
<tr><td>{$_POST['pitcherteam'][$i]}</td>
<td><b>{$_POST['pitcher'][$i]}</td>
<td><b>{$_POST['pitcherage'][$i]}</b></td>
<td><b>{$_POST['pitcherteam'][$i]}</b></td></tr>
MESSAGE;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With