我的代码在困扰我几天的时候遇到了这个问题。我正在尝试做一个项目,我的用户可以请求交货,他可以根据需要选择一组项目。问题是,当我将数据传递到数组时,它仅捕获第一行,而忽略其余的行。

由于我在这里,如果可能的话,任何人都可以帮助我找到一种方法,将我获取的数据插入sql。这是我目前的代码

<?php
$sql="select * from Material_Equipamento";
$res=mysql_query($sql);
$sql2="select * from Fornecedores";
$res2=mysql_query($sql2);
?>
<body>

<div class="container">
<h4 align="center"> Formulário Encomenda</h4>
<form  class="form-inline" role="form" align="center" method="POST" action="index.php?cmd=ins-enc">
<div class="form-group">
<label class="control-label " for="dp">Data do Pedido:</label>
<div class="col-sm-10"><input type="text" class="form-control" name="dp" value="" placeholder="DD/MM/AAAA">
</div>
</div>
<div class="form-group">
<label class="control-label " for="de">Data de Entrega:</label>
<div class="col-sm-10"><input type="text" class="form-control" name="de" value="" placeholder="DD/MM/AAAA">
</div>
</div>
<div class="form-group">
<label class="control-label " for="f">Fornecedor :</label>
<select class="form-control" id="forn" name='forn'>
<?php
while ($lin2=mysql_fetch_array($res2, MYSQL_ASSOC)){
  echo "<option value='";
  echo $lin2['n_Fornecedor'];
  echo "'>";
  echo $lin2['Nome'];
  echo "</option>";
  }
?>
</select>
</div>
<br><br>
<div class="row clearfix">
    <div class="col-md-12 table-responsive">
        <table class="table table-bordered table-hover table-sortable" id="tab_logic">
            <thead>
                <tr >
                    <th class="text-center">Material</th>
                    <th class="text-center">Quantidade</th>
                    <th class="text-center">Valor previsto por Unidade</th>
                    <th class="text-center" style="border-top: 1px solid #ffffff; border-right: 1px solid #ffffff;">
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr id='addr0' data-id="0" >
                    <td data-name="mat">
                        <select class="form-control" name="mat[]">
                            <?php
                            while ($lin=mysql_fetch_array($res, MYSQL_ASSOC)){
                            echo "<option value='";
                            echo $lin['n_Mat_Equip'];
                            echo "'>";
                            echo $lin['Nome'];
                            echo "</option>";
                         }
?>
                        </select>
                    </td>
                    <td data-name="quant">
                        <input type="text" name='quant[]' placeholder='Quantidade' class="form-control"/>
                    </td>
                    <td data-name="Valuni">
                        <input type="text" name='valuni[]' placeholder='Valor Previsto por Unidade' class="form-control"/>
                    </td>
                    <td data-name="del">
                        <button nam"del0" class='btn btn-danger glyphicon glyphicon-remove row-remove'></button>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
    <a id="add_row" class="btn btn-default pull-right">Outro item</a>

<br><input type="submit" value="Adicionar Material">
<input type="reset" value="Limpar ecrã">
</form>
</div>
</body>
</html>


这就是从表中获取内容的表单和php,我也正在运行此脚本,以便用户可以根据需要添加更多项。

<script>
$(document).ready(function() {
$("#add_row").on("click", function() {
    // Dynamic Rows Code

    // Get max row id and set new id
    var newid = 0;
    $.each($("#tab_logic tr"), function() {
        if (parseInt($(this).data("id")) > newid) {
            newid = parseInt($(this).data("id"));
        }
    });
    newid++;

    var tr = $("<tr></tr>", {
        id: "addr"+newid,
        "data-id": newid
    });

    // loop through each td and create new elements with name of newid
    $.each($("#tab_logic tbody tr:nth(0) td"), function() {
        var cur_td = $(this);

        var children = cur_td.children();

        // add new td and element if it has a nane
        if ($(this).data("name") != undefined) {
            var td = $("<td></td>", {
                "data-name": $(cur_td).data("name")
            });

            var c =     $(cur_td).find($(children[0]).prop('tagName')).clone().val("");
            c.attr("name", $(cur_td).data("name") + newid);
            c.appendTo($(td));
            td.appendTo($(tr));
        } else {
            var td = $("<td></td>", {
                'text': $('#tab_logic tr').length
            }).appendTo($(tr));
        }
    });

    // add delete button and td
    /*
    $("<td></td>").append(
        $("<button class='btn btn-danger glyphicon glyphicon-remove row-remove'></button>")
            .click(function() {
                $(this).closest("tr").remove();
            })
    ).appendTo($(tr));
    */

    // add the new row
    $(tr).appendTo($('#tab_logic'));

    $(tr).find("td button.row-remove").on("click", function() {
         $(this).closest("tr").remove();
    });
});




// Sortable Code
var fixHelperModified = function(e, tr) {
    var $originals = tr.children();
    var $helper = tr.clone();

    $helper.children().each(function(index) {
        $(this).width($originals.eq(index).width())
    });

    return $helper;
};

$(".table-sortable tbody").sortable({
    helper: fixHelperModified
}).disableSelection();

$(".table-sortable thead").disableSelection();



$("#add_row").trigger("click");
});
</script>


我不知道问题出在哪里= /我知道很多代码,对不起,如果您最终阅读全部内容。但是是的,问题是它仅读取您在进入页面时立即显示的预定义行,而不读取用户添加的其他行。谢谢您的合作=)。顺便说一句,我在视觉上使用引导程序以防万一

最佳答案

复制字段时,您的JavaScript代码中缺少方括号。尝试这个 :



// loop through each td and create new elements with name of newid
    $.each($("#tab_logic tbody tr:nth(0) td"), function() {
        var cur_td = $(this);

        var children = cur_td.children();

        // add new td and element if it has a nane
        if ($(this).data("name") != undefined) {
            var td = $("<td></td>", {
                "data-name": $(cur_td).data("name")
            });

            var c = $(cur_td).find($(children[0]).prop('tagName')).clone().val("");
			//Test if the initial field have brackets and is define...
			if($(cur_td).find($(children[0]).prop('tagName')).attr('name') != undefined){
                //Is there bracket?
				if( $(cur_td).find($(children[0]).prop('tagName')).attr('name').indexOf('[') != -1){
					c.attr("name", $(cur_td).data("name") + newid + '[]');
				}else{
					c.attr("name", $(cur_td).data("name") + newid);
				}
			}else{
				c.attr("name", $(cur_td).data("name") + newid);
			}

            c.appendTo($(td));
            td.appendTo($(tr));
        } else {
            var td = $("<td></td>", {
                'text': $('#tab_logic tr').length
            }).appendTo($(tr));
        }
    });

关于javascript - 将数组插入SQL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36472749/

10-12 07:33