Reputation: 111
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).
Upvotes: 3
Views: 56
Reputation: 157
This is a quick script I wrote from the code you embedded. I looked at your question about an hour ago when there was limited response and since then you seem to have your problem solved. Instead of tossing this code, I figure I would just offer it in case it helps you or someone else.
test.php::
print "<html><head><title>title</title></head><body>";
$size_array = count($_POST['pitcher']); // <-- notice there's no []
for ($i=0; $i<$size_array; $i++){
if($_POST['pitcher'][$i] != ''){
$team = $_POST['pitcherteam'][$i];
$pitchername = $_POST['pitchername'][$i];
$age = $_POST['pitcherage'][$i];
$pitcherpitches = $_POST['pitcherpitches'][$i];
if ($pitchername) {
print "$team, $pitchername, $age, $pitcherpitches<br>";
}
}
}
$form_input_row = '<table><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=hidden size=50 name="pitcher[]" value="somedata"><input type=text size=50 name="pitchername[]"></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></table>';
print "<form method=\"POST\">";
for ($j = 0; $j<6; $j++) {
print $form_input_row;
}
print "<input type=\"submit\" value=\"submit\">";
print "</form></body></html>";
Upvotes: 0
Reputation: 64536
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;
}
}
Upvotes: 1
Reputation: 676
Check if the value of $_POST['pitcher'][$i] is set and skip the message if it isn't:
if( !isset($_POST['pitcher'][$i]} ) continue;
Or, for your code:
$size_array = count($_POST['pitcher[]']);
for ($i=0; $i<$size_array; $i++){
if( !isset($_POST['pitcher'][$i]} ) continue;
$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;
}
Upvotes: 0