doutandusegang2961 2013-07-29 13:47 采纳率: 0%
浏览 15
已采纳

PHP仅通过电子邮件发送填充值

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).

  • 写回答

3条回答 默认 最新

  • duanou9758 2013-07-29 14:04
    关注

    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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输