There is a regular HTML table with input nested. When you click on the button "Add line", one more line "tr" is created each time. You need to know the number of lines after clicking on the "Send" button or on any new button. And further to work with this number in the php code. It’s easy to count strings in js. But it is not yet clear how to transfer this number to the php code or count the lines immediately in php.
- Well, do you send the data when you click on the "Send" button? Most likely on the back, i.e. in php. There also send another variable with the number of lines. If the data is sent by an array, then it will be easier to back them up and count them without bothering with the counting using js. - Eleferen
- How are you going to send data to the back? - Stepan Kasyanenko
- Initially 4 tr lines are created using php. Then, when you click on the "Add line" button, one more line is added via js. How to calculate these lines on the back can not understand - Sherzod Norov
|
2 answers
Everything is much simpler. For this you will need to form the correct form. Your every line from input should be approximately the same, where [0] is the line number:
<tr> <td> <input type="text" name="array[0][title]"> </td> <td> <input type="text" name="array[0][description]"> </td> </tr> When transferring data to the backend, the $ _POST variable will have the following:
["array"]=> array(1) { [0]=> array(2) { ["title"]=> string(2) "11" ["description"]=> string(2) "11" } } In order to get the number of rows in the backend, you only need to do count($_POST['array']) .
|
Thank you all for the answers. Did a little differently. Created 1 more input in the same form. On js, I count the number of lines and add this number to this input with each click. And then through POST I send to the server and continue to work with this number in php.
admin.php
<form method="POST" action="add.php"> <table class="products__table"> <tbody id="tblist"> <tr> <td><input id="id" name="id[0] " value="1"></td> <td><input id="name" name="name[0]" type="text" value="ar11"></td> <td><input id="size" name="size[0]" value="123"></td> <td><input id="meters_tonne" name="meters_tonne[0]" value="111"></td> <td><input id="manufacturer" name="manufacturer[0]" value="uzb"></td> <td><input id="unit" name="unit[0]" value="tn"></td> <td><input id="price_transfer" name="price_transfer[0]" value="23525425"></td> <td><input id="price_cash" name="price_cash[0]" value="2523542"></td> </tr> <tr> <td><input id="id" name="id[1] " value="2"></td> <td><input id="name" name="name[1]" type="text" value="ar22"></td> <td><input id="size" name="size[1]" value="234"></td> <td><input id="meters_tonne" name="meters_tonne[1]" value="222"></td> <td><input id="manufacturer" name="manufacturer[1]" value="ger"></td> <td><input id="unit" name="unit[1]" value="tn"></td> <td><input id="price_transfer" name="price_transfer[1]" value="523455452"></td> <td><input id="price_cash" name="price_cash[1]" value="15341234"></td> </tr> <tr class="send_button_tr"> <td><input type="submit" name="button" id="button" value="Отправить"></td> <td><input id="hide_count_field" name="hide_field" value=""></td> </tr> </tbody> </table> </form> admin.js
var matches = document.querySelectorAll("tr").length - 1; $('#hide_count_field').attr('value','' + matches + ''); add.php
$countOfStrokes = $_POST['hide_field']; |
