尝试使用javascript添加新表单行并插入数据库

时间:2014-09-10 22:58:36

标签: javascript php jquery mysql

添加新行有效(好吧它会添加一个新行),但是当我尝试将数据提交到数据库时,它会返回数组。我尝试了它而没有在表单中的名称标签的末尾放置[],但它提交的所有内容都是第一行。

很抱歉提出一个愚蠢的问题,但我一直绞尽脑汁想知道如何让它发挥作用。

继承我的代码:

<?php
    if($user->data()->username = $res->username) {
        if(isset($_POST['results'])) {
            $submitres = DB::getInstance()->insert('results', array(
                'torneyid'      => Input::get('torneyid'),
                'buyin'         => Input::get('buyin'),
                'torneydesc'    => Input::get('description'),
                'finish'        => Input::get('finish'),
                'profit_loss'   => Input::get('profit'),
                'eventid'       => $res->id
                    ));
        }
        echo '<form action="" method="post">
        <table id="mytable" class="table table-striped">
        <thead>
            <tr>
                <th>Torney ID</th>
                <th class="hidden-sm">Torney Description</th>
                <th>Buy-In</th>
                <th>Finish Pos</th>
                <th>Profit/Loss</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><input name="torneyid[]" type="text" value="" class="form-control" /></td>
                <td class="hidden-sm"><input name="description[]" type="text" value="" class="fm-control" /></td>
                <td><input name="buyin[]" type="text" value="" class="form-control" /></td>
                <td><input name="finish[]" type="text" value="" class="form-control" /></td>
                <td><input name="profit[]" type="text" value="" class="form-control" /></td>
            </tr>
        </tbody>
    </table>
    <button type="submit" name="results" class="btn btn-primary">Submit Results</button>
    <a id="add"><button class="btn btn-primary">Add Row</button></a>
        </form>';
    } 

&GT;

我的java脚本是:

<script type="text/javascript">
    $(document).ready(function() {
        $("#add").click(function() {
        $('#mytable tbody>tr:last').clone(true).insertAfter('#mytable tbody>tr:last');
    return false;
    });
    });
</script>

感谢您的帮助!

固定 - 将添加我是如何做的,以防万一它可以帮助其中的其他人:

将代码从if(isset)更改为:

if(isset($_POST['results'])) {
    for ( $row = 0; $row < count( $_POST["torneyid"] ); ++$row ) {
        $tid = $_POST["torneyid"][$row];
        $desc = $_POST["description"][$row];
        $buy = $_POST['buyin'][$row];
        $fin = $_POST['finish'][$row];
        $prof = $_POST['profit'][$row];

    $submitres = DB::getInstance()->insert('results', array(
        'torneyid'      => $tid,
        'buyin'         => $desc,
        'torneydesc'    => $buy,
        'finish'        => $fin,
        'profit_loss'   => $prof,
        'eventid'       => $res->id

        ));
}

0 个答案:

没有答案