我有一个在线表格,列出了我孩子棒球联赛的投手信息。表格允许输入多个投手(未知数量)。我在循环查看结果时遇到问题,以便通过电子邮件发送给我。
以下是表单中的代码(这是针对pitcher1 - 每个表单最多10个投手):
<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>
这是我在php电子邮件功能中的内容:
$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;
}
我希望遍历表单并仅定位具有投手输入的行(相对于每次回显所有10行并且在非填充字段中具有空白/坏数据)。
答案 0 :(得分:1)
更改输入以使用[]
语法而不是设置数字:
<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>
然后从PHP中删除[]
,因为PHP会自动将这些解析为本机数组。如果投手是空白的,也要检查以便忽略该行。
$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;
}
}
答案 1 :(得分:0)
检查是否设置了$ _POST ['pitcher'] [$ i]的值,如果不是,则跳过该消息:
if( !isset($_POST['pitcher'][$i]} ) continue;
或者,对于您的代码:
$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;
}
答案 2 :(得分:0)
这是我从您嵌入的代码中编写的快速脚本。大约一小时前,当我的回答有限时,我看了你的问题,从那以后你似乎已经解决了问题。我没有抛弃这些代码,而是想提供它,以防它或其它人帮助你。
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>";