提交多个动态创建的表单,但不是全部

时间:2014-08-27 16:36:09

标签: javascript php jquery forms

所以我们走了。我有一个页面列出了一堆未经训练的体育游戏。这是我运行生成页面的查询。

<div id="NFL">
    <?php
    foreach ($conn->query("SELECT * FROM game_data WHERE sport='NFL' AND awayscore IS NULL ORDER BY date DESC") as $NFL) {
    echo '<form method="post" action="update_score.php">
        <table class="table table-bordered">
            ';
            echo '
            <thead>
                <tr>
                    <th width="5%" class="head0">Rotation</th>
                    <th width="45%" class="head1">Team</th>
                    <th width="10%" class="head0">Money Line</th>
                    <th width="10%" class="head1">Spread</th>
                    <th width="10%" class="head0">Over/Under</th>
                    <th width="10%" class="head1">Score</th>
                </tr>
            </thead>';
            echo '
            <tr>
                <td colspan="6">
                    ';
                    $date = date_create($NFL['date']);
                    echo date_format($date, 'l F jS Y \@ g:iA');
                    echo '
                </td>
            </tr>';
            echo '
            <tr>
                <td>'.$NFL['awayrotation'].'</td>
                <td>'.$NFL['awayteam'].'</td>
                <td>'.$NFL['awaymoneyline'].'</td>';
                echo '
                <td>
                    ';
                    if ($NFL['awaymoneyline'] > 0) {
                    $line = $NFL['line'] * -1;
                    echo $line;
                    }
                    elseif ($NFL['awaymoneyline'] < 0) {
                    echo $NFL['line'];
                    }                       ;
                    echo '
                </td>';
                echo '
                <td>'.$NFL['total'].'</td>
                <td><input type="text" required name="awayscore"></input></td>
            </tr>';
            echo '
            <tr>
                <td>'.$NFL['homerotation'].'</td>
                <td>'.$NFL['hometeam'].'</td>
                <td>'.$NFL['homemoneyline'].'</td>';
                echo '
                <td>
                    ';
                    if ($NFL['homemoneyline'] > 0) {
                    $line = $NFL['line'] * -1;
                    echo $line;
                    }
                    elseif ($NFL['homemoneyline'] < 0) {
                    echo $NFL['line'];
                    }                       ;
                    echo '
                </td>';
                echo '
                <td>'.$NFL['total'].'</td>
                <td><input type="text" required name="homescore"></input></td>
            </tr>';
            echo '
            <tr><td colspan="6" align="right"><input type="hidden" name="id" value="'.$NFL['id'].'"><span style="padding-right:15px"><input type="submit" value="Submit Score"></span></td></tr>
        </table>
    </form>';
    }
    ?>
</div>

这就是我要做的事情,我希望有一个按钮来提交多个表单。只有他们在分数中放置了一个值。这可能吗?我已经阅读过基于名称的多个表单,但这些表单是动态创建的。我期待着你的见解。

1 个答案:

答案 0 :(得分:-1)

这里试试这个

我放置了一个计数器,每个SQL结果处理一个增量为1这也是我们稍后会使用的一个键

现在,计数器被添加到输入的名称字段的末尾(在&#34; - &#34;之后将被爆炸)

在脚本底部是一个小代码,我将如何处理后端信息

<?php 
        $count = "0";
        echo '<form method="post" action="update_score.php"><table class="table table-bordered">';
        foreach ($conn->query("SELECT * FROM game_data WHERE sport='NFL' AND awayscore IS NULL ORDER BY date DESC") as $NFL) {
            $count = $count+1;
            echo '<thead>
                    <tr>
                      <th width="5%" class="head0">Rotation</th>
                      <th width="45%" class="head1">Team</th>
                      <th width="10%" class="head0">Money Line</th>
                      <th width="10%" class="head1">Spread</th>
                      <th width="10%" class="head0">Over/Under</th>
                <th width="10%" class="head1">Score</th>
                    </tr></thead>';
            echo '<tr><td colspan="6">';
            $date = date_create($NFL['date']);
            echo date_format($date, 'l F jS Y \@ g:iA');
            echo '</td></tr>';      
            echo '<tr><td>'.$NFL['awayrotation'].'</td><td>'.$NFL['awayteam'].'</td><td>'.$NFL['awaymoneyline'].'</td>';
            echo '<td>';
            if ($NFL['awaymoneyline'] > 0) { 
                $line = $NFL['line'] * -1;
                echo $line;
            }
            elseif ($NFL['awaymoneyline'] < 0) {
                echo $NFL['line'];  
            }                       ;
            echo '</td>';
            echo '<td>'.$NFL['total'].'</td><td><input type="text" required name="awayscore-$count"></input></td></tr>';
            echo '<tr><td>'.$NFL['homerotation'].'</td><td>'.$NFL['hometeam'].'</td><td>'.$NFL['homemoneyline'].'</td>';
            echo '<td>';
            if ($NFL['homemoneyline'] > 0) { 
                $line = $NFL['line'] * -1;
                echo $line;
            }
            elseif ($NFL['homemoneyline'] < 0) {
                echo $NFL['line'];  
            }                       ;
            echo '</td>';
            echo '<td>'.$NFL['total'].'</td><td><input type="text" required name="homescore-$count"></input></td></tr>';
            echo '<tr><td colspan="6" align="right"><input type="hidden" name="id-$count" value="'.$NFL['id'].'"><span style="padding-right:15px">';
        }
        echo '<input type="hidden" name="count" value="$count">'
        echo '<input type="submit" value="Submit Score"></span></td></tr></table></form>'



        // back end

        $counter = 0;

        while ($counter++ < $_POST['count'])
        {
            $name = $_POST["name-$counter"];
            $awayscore = $_POST["awayscore-$counter"];
            $homescore = $_POST["homescore-$counter"];
            $id = $_POST["id-$counter"]; // considering you may need to get rid of the count

            $name = explode("-", $name);
            $name = $name[0];

            // ect ect

            // enter in to data base

            //clear all data in strings

        }
    ?>

很抱歉这是因为仇敌:|

更多的是,后端代码适用于您的传球手(表格指向的代码)

注意:您的PHP风格比现在大多数使用的都要早,接近我使用的 - 只需检查您使用的所有教程是否为您提供PHP 5.3的引用,如有疑问请访问php.net并检查函数调用版本使用 - 语法和备用选项