[Help] Динамично въвеждане на редове

bLacksn0w

Member
Joined
Mar 23, 2016
Messages
32
Reaction score
3
Както е на снимката (приложена), бих искал неограничен брой редове да се въвеждат наведнъж в датабазата:
ssa.png
PHP:
function dostavka(){
if (isset($_POST["dostavka"])){
$nomer_protokol = secure($_POST['nomer_protokol']);
$dostavchik = secure($_POST['dostavchik']);
$kod = secure($_POST['kod']);
$naimenovanie = secure($_POST['naimenovanie']);
$marka = secure($_POST['marka']);
$edinica = secure($_POST['edinica']);
$kvo = secure($_POST['kvo']);
$edcena = secure($_POST['edcena']);
$krcena = secure($_POST['krcena']);
$regtime = date('Y-m-d H:i:s');
$protokol_check = mssql_query("SELECT nomer_protokol FROM dostavki WHERE nomer_protokol='$nomer_protokol'");
$protokol_check = mssql_num_rows($protokol_check);
$broiki_check = mssql_query("SELECT * FROM article WHERE article='$kod'");
$broiki = mssql_fetch_array($broiki_check);
$new_broiki = $broiki['stock'] + $kvo;
//if($_POST["kod"] != "" &&
//$_POST["naimenovanie"] != "" &&
//$_POST["marka"] != "" &&
//$_POST["edinica"] != "" &&
//$_POST["kvo"] != "" &&
//$_POST["edcena"] != "" &&
//$_POST["krcena"] != "")
if (empty($dostavchik) || empty($kod) || empty($nomer_protokol) || empty($naimenovanie) || empty($marka) || empty($edinica) || empty($kvo) || empty($edcena) || empty($krcena)) { echo "<br><table class=ranktable align=center width=90%  cellpadding=2 cellspacing=2><tr class=head><td><img src=images/~style/cross_circle.png /><font color='red'>Моля попълнете всички полета!</font></fieldset></td></tr></table><br />"; $error=1;}
elseif($protokol_check > 0) { echo "<BR><table class=ranktable align=center width=90%  cellpadding=2 cellspacing=2><tr class=head><td><img src=images/~style/cross_circle.png />Протокол с номер: <font color=red>$nomer_protokol</font> вече съществува!</fieldset></td></tr></table><br />"; $error=1;}
if ($error!=1){
$a = mssql_query("INSERT INTO dostavki (nomer_protokol,dostavchik, kod, naimenovanie, marka, edinica, kvo, edcena, krcena,data)
VALUES ('$nomer_protokol','$dostavchik','$kod','$naimenovanie','$marka','$edinica','$kvo','$edcena','$krcena','$regtime')");
$b = mssql_query("UPDATE article set [stock]='$new_broiki' WHERE article ='$kod'");
echo "<BR><table class=ranktable align=center width=90%  cellpadding=2 cellspacing=2><tr class=head><td><img src=images/~style/tick_circle.png />Постъпление с номер: <font color='#39b44a'>$nomer_protokol</font> беше въведенo успешно!</fieldset></td></tr></table><br />";
}
}}

JavaScript:
<script type="text/javascript">

$(document).ready(function(){
 
var rows = 1;
$("#createRows").click(function(){
var tr = "<tr>";
tr = tr + "<td><input type='text' name='kod' id='kod' size='15'></td>";
tr = tr + "<td><input type='text' name='naimenovanie' id='naimenovanie' size='25'></td>";
tr = tr + "<td><input type='text' name='marka' id='marka' size='10'></td>";
tr = tr + "<td><input type='text' name='edinica' id='edinica' size='2'></td>";
tr = tr + "<td><input type='number' name='kvo' id='kvo' size='3'></td>";
tr = tr + "<td><input type='text' name='edcena' id='edcena' size='3'></td>";
tr = tr + "<td><input type='text' name='krcena' id='krcena' size='3'></td>";
tr = tr + "</tr>";
$('#myTable > tbody:last').append(tr);
 //"+rows+"
$('#dostavka').val(rows);
rows = rows + 1;
});
 
$("#deleteRows").click(function(){
if ($("#myTable tr").length != 1) {
$("#myTable tr:last").remove();
}
});
 
$("#clearRows").click(function(){
rows = 1;
$('#dostavka').val(rows);
$('#myTable > tbody:last').empty(); // remove all
});
 
});
</script>
 
Пробвай така:


JavaScript:
<script type="text/javascript">

$(document).ready(function(){
 
var rows = 1;
$("#createRows").click(function(){
var tr = "<tr>";
tr = tr + "<td><input type='text' name='kod[]' id='kod' size='15'></td>";
tr = tr + "<td><input type='text' name='naimenovanie[]' id='naimenovanie' size='25'></td>";
tr = tr + "<td><input type='text' name='marka[]' id='marka' size='10'></td>";
tr = tr + "<td><input type='text' name='edinica[]' id='edinica' size='2'></td>";
tr = tr + "<td><input type='number' name='kvo[]' id='kvo' size='3'></td>";
tr = tr + "<td><input type='text' name='edcena[]' id='edcena' size='3'></td>";
tr = tr + "<td><input type='text' name='krcena[]' id='krcena' size='3'></td>";
tr = tr + "</tr>";
$('#myTable > tbody:last').append(tr);
 //"+rows+"
$('#dostavka').val(rows);
rows = rows + 1;
});
 
$("#deleteRows").click(function(){
if ($("#myTable tr").length != 1) {
$("#myTable tr:last").remove();
}
});
 
$("#clearRows").click(function(){
rows = 1;
$('#dostavka').val(rows);
$('#myTable > tbody:last').empty(); // remove all
});
 
});
</script>
 
  • Like
Reactions: Dea7h
Може да пробваш да сложиш различни name/id за всеки нов ред:
ред 1: kod1
ред2: kod2..

и после в php погледни как съм ти направил $insert_values

нещо такова пробвай

JavaScript:
<script type="text/javascript">

$(document).ready(function(){
var rows = 1;
$("#createRows").click(function(){
var tr = "<tr>";
tr = tr + `<td><input type='text' name='kod${rows}' id='kod${rows}' size='15'></td>`;
tr = tr + `<td><input type='text' name='naimenovanie${rows}' id='naimenovanie${rows}' size='25'></td>`;
tr = tr + `<td><input type='text' name='marka${rows}' id='marka${rows}' size='10'></td>`;
tr = tr + `<td><input type='text' name='edinica${rows}' id='edinica${rows}' size='2'></td>`;
tr = tr + `<td><input type='number' name='kvo${rows}' id='kvo${rows}' size='3'></td>`;
tr = tr + `<td><input type='text' name='edcena${rows}' id='edcena${rows}' size='3'></td>`;
tr = tr + `<td><input type='text' name='krcena${rows}' id='krcena${rows}' size='3'></td>`;
tr = tr + "</tr>";
$('#myTable > tbody:last').append(tr);
$('#dostavka').val(rows);
rows = rows + 1;
});

$("#deleteRows").click(function(){
if ($("#myTable tr").length != 1) {
$("#myTable tr:last").remove();
}
});

$("#clearRows").click(function(){
rows = 1;
$('#dostavka').val(rows);
$('#myTable > tbody:last').empty(); // remove all
});

});
</script>


PHP:
<?php
function dostavka()
{
  if (isset($_POST["dostavka"])) {
    $redove = intval(secure($_POST["dostavka"]));
    $nomer_protokol = secure($_POST['nomer_protokol']);
    $dostavchik = secure($_POST['dostavchik']);
    $regtime = date('Y-m-d H:i:s');
    $protokol_check = mssql_query("SELECT nomer_protokol FROM dostavki WHERE nomer_protokol='$nomer_protokol'");
    $protokol_check = mssql_num_rows($protokol_check);
    $broiki_check = mssql_query("SELECT * FROM article WHERE article='$kod'");
    $broiki = mssql_fetch_array($broiki_check);
    $new_broiki = $broiki['stock'] + $kvo;
    $insert_values = "";
    for($x = 1; $x <= $redove; $x++) {
      if(isset($_POST["kod$x"]) && isset($_POST["naimenovanie$x"]) && isset($_POST["marka$x"]) && isset($_POST["edinica$x"]) && isset($_POST["kvo$x"]) && isset($_POST["edcena$x"]) && isset($_POST["krcena$x"])) {
        if($x !== 1) { $insert_values += ","; }
        $insert_values += "('$nomer_protokol', '$dostavchik', '" . secure($_POST["kod$x"]) . "', '" . secure($_POST["naimenovanie$x"]) . "', '" . secure($_POST["marka$x"]) . "', '" . secure($_POST["edinica$x"]) . "', '" . secure($_POST["kvo$x"]) . "', '" . secure($_POST["edcena$x"]) . "', '" . secure($_POST["krcena$x"]) . "', '$regtime')";
      }
    }

    if (empty($dostavchik) || empty($kod) || empty($nomer_protokol) || empty($naimenovanie) || empty($marka) || empty($edinica) || empty($kvo) || empty($edcena) || empty($krcena)) {
      echo "<br><table class=ranktable align=center width=90%  cellpadding=2 cellspacing=2><tr class=head><td><img src=images/~style/cross_circle.png /><font color='red'>Моля попълнете всички полета!</font></fieldset></td></tr></table><br />";
      $error = 1;
    } elseif ($protokol_check > 0) {
      echo "<BR><table class=ranktable align=center width=90%  cellpadding=2 cellspacing=2><tr class=head><td><img src=images/~style/cross_circle.png />Протокол с номер: <font color=red>$nomer_protokol</font> вече съществува!</fieldset></td></tr></table><br />";
      $error = 1;
    }
    if ($error != 1) {
      mssql_query("INSERT INTO dostavki (nomer_protokol,dostavchik, kod, naimenovanie, marka, edinica, kvo, edcena, krcena,data) VALUES $insert_values");
      mssql_query("UPDATE article set [stock]='$new_broiki' WHERE article='$kod'");
      echo "<BR><table class=ranktable align=center width=90%  cellpadding=2 cellspacing=2><tr class=head><td><img src=images/~style/tick_circle.png />Постъпление с номер: <font color='#39b44a'>$nomer_protokol</font> беше въведенo успешно!</fieldset></td></tr></table><br />";
    }
  }
}
 
Last edited:
да пробвай на роот изглежда по шукар (xax)
 
Интересно.. Пробвам с на @r00tme кода, и ми дава грешка в този ред на функцията:

Fatal error: Unsupported operand types

PHP:
$new_broiki = $broiki['stock'] + $kvo;
 
@Dea7h , с твоя код не ми разпознава примерно:
PHP:
name='kod${rows}' id='kod${rows}'
 
Интересно.. Пробвам с на @r00tme кода, и ми дава грешка в този ред на функцията:

Fatal error: Unsupported operand types

PHP:
$new_broiki = $broiki['stock'] + $kvo;
$broiki['stock'][0] = 1ви ред
$broiki['stock'][1] = 2ри ред
$broiki['stock'][2] = 3ти ред
etc..
 
$new_broiki = $broiki['stock'] + $kvo;

Предложението, което ти дадох е абсолютно валидно, но резултата е матрица или 2D арей или там както се нарича. Tака, че ти трябва цикъл за да достъпиш стойностите и ключовете от POST рекуест-а. Дали ще е foreach, for или каквото ползваш няма значение.
 
Last edited:
Това ми беше идеята.



PHP:
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<head runat="server">
    <title>Using jQuery UI from the CDN</title>
    <link rel="Stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/themes/redmond/jquery-ui.css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>   
        <asp:TextBox ID="txtStartDate" ClientIDMode="Static" runat="server" />
    </div>
    </form>
    <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.js"></script>
    <script src="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.22/jquery-ui.js"></script>



<div class="container">
<form  method="Post">
<table class="table" id="myTable">
<tbody></tbody>
</table>
<input name="dostavkaw" type="submit" id="dostavkaw" value="Dostavka">
</form>
<?php

    print("<pre>");
    print_r($_POST);
    print("</pre>");

?>
<div class="btn btn-primary" id="createRows" >Create Rows</div>
<div class="btn btn-primary" id="deleteRows" >Delete Rows</div>
<div class="btn btn-primary" id="clearRows">Clear Rows</div>

</div>
<script type="text/javascript">

$(document).ready(function(){
 
var rows = 1;
$("#createRows").click(function(){
var tr = "<tr>";
tr = tr + "<td><input type='text' name='kod[]' id='kod' size='15'></td>";
tr = tr + "<td><input type='text' name='naimenovanie[]' id='naimenovanie' size='25'></td>";
tr = tr + "<td><input type='text' name='marka[]' id='marka' size='10'></td>";
tr = tr + "<td><input type='text' name='edinica[]' id='edinica' size='2'></td>";
tr = tr + "<td><input type='number' name='kvo[]' id='kvo' size='3'></td>";
tr = tr + "<td><input type='text' name='edcena[]' id='edcena' size='3'></td>";
tr = tr + "<td><input type='text' name='krcena[]' id='krcena' size='3'></td>";
tr = tr + "</tr>";
$('#myTable > tbody:last').append(tr);
 //"+rows+"
$('#dostavka').val(rows);
rows = rows + 1;
});
 
$("#deleteRows").click(function(){
if ($("#myTable tr").length != 1) {
$("#myTable tr:last").remove();
}
});
 
$("#clearRows").click(function(){
rows = 1;
$('#dostavka').val(rows);
$('#myTable > tbody:last').empty(); // remove all
});
 
});
</script>