Take data from a dynamic table and save to mysql

Asked

Viewed 254 times

0

good night. I haven’t programmed in a long time! I’m rusty, rsrs

I need help. I’m making a system to control budgets.

my problem is that I need to take this product information and play in a mysql table(tb_orcings). I don’t know how to do this. =\

picture below: criação de pedido de compra

Code to display form fields:

<tr class='tr_input'>
                <form method="post" action="cad-novo-pdc.php">
                    <td><center><input type='text' class='username' id='username_1' name="username_1" placeholder='Informe o produto'></center></td>
                    <td><center><input type='text' class='id' id='id_1' name="id_1" readonly></center></td>
                    <td><center><input type='text' class='name' id='name_1'  name="name_1" required></center></td>
                    <td><center><input type='text' class='salary' id='salary_1' name="salary_1" onKeyUp="moeda(this);" required></center></td>

                <td><center><button type="button" class="btn btn-warning" onclick="remove(this)">Excluir</button></center></td>

            </tr>

JS code to give the functionality "add item":

$('#addmore').click(function(){

                // Get last id 
                var lastname_id = $('.tr_input input[type=text]:nth-child(1)').last().attr('id');
                var split_id = lastname_id.split('_');

                // New index
                var index = Number(split_id[1]) + 1;

                // Create row with input elements
                var html = "<tr class='tr_input'>\n\
                            <td><center><input type='text' class='username' id='username_"+index+"' placeholder='Informe o produto'></center></td>\n\
                            <td><center><input type='text' class='id_' id='id_"+index+"' readonly></center></td>\n\
                            <td><center><input type='text' class='name' id='name_"+index+"' ></center></td>\n\
                            <td><center><input type='text' class='salary' id='salary_"+index+"' onKeyUp='moeda(this);'  ></center></td>\n\
                            <td><center><button type='button' class='btn btn-warning' onclick='remove(this)'>Excluir</button></td></center></tr>";

                // Append data
                $('tbody').append(html);

            });

I created a post method in the form, but it only sends the first product line inserted. Now, I don’t know what to do to get one or 50 items that are typed in the budget. Can you give me a hand? xD

The save function must go into action when you click on the "finish request button".

I’ve really been over 5 years without picking up a code =\

greetings to all the/

1 answer

0


Missing only add attribute name fields dynamically generated by your JS code. To resolve, add the attribute name in fields generated via javascript.

Example: next to id='username_"+index+"', add name='username_"+index+"', and so on for all 4 fields.

Explanation: Without the attribute name, the form field is not sent via POST by the browser. And that’s why only the first line is being sent, because in the HTML set to item 1 you put the name correctly. The problem is only in the dynamic part (line 2 onwards).

Once you have done this, you will be able to access all variables via POST via PHP. To test that all values are coming in correctly, replace the file contents Cad-new-Pdc.php, provisionally, by <?php var_dump($_POST); ?> and submit the form.

  • didn’t work my friend.

  • To help you debug, do the following: Enter the dynamically generated fields (using the Google Chrome browser’s Devtools tool, for example by pressing the browser’s F12 key), and see if the 2nd line fields will match the correct ids and Names.

  • Another problem I identified: its tag <form> is within the tag <tr>. In order for all information to be submitted, the form must be outside the <table>. It must be something like this (just exemplifying the structure): <form><table><tr><td></td><td></td></tr><tr><td></td><td></td></tr></table></form>.

  • thanks friend! the output got: array (size=8)&#xA; 'username_1' => string 'calha dutotec' (length=13)&#xA; 'id_1' => string '2' (length=1)&#xA; 'name_1' => string '3' (length=1)&#xA; 'salary_1' => string '1000.24' (length=7)&#xA; 'username_2' => string 'fita isolante' (length=13)&#xA; 'id_2' => string '15' (length=2)&#xA; 'name_2' => string '10' (length=2)&#xA; 'salary_2' => string '17.80' (length=5) but when I put the page Cad-new-Pdc displays only the first, example of how it is: $razao = $_POST['username_1'];&#xA;$fantasia = $_POST['name_1'];&#xA;$endereco = $_POST['salary_1'];

  • That which is hardcoded (username_1, etc.), you will have to leave dynamic. As your frontend has no record limiter, you have to make an infinite loop and force the stop when you realize you have reached the end. Example: <?php $i = 1; while (true) { if (!isset($POST["username$i"])) { break; } $reason = $POST["username$i"]; /* etc */ } ?>

  • My friend, thank you very much, but I can’t get past here...I don’t know what to do. I put that while you asked on the page Cad-novo-Pdc but it is in loop loading....

  • @Ronierickholanda saw that you are a new Stackoverflow user, welcome! I’ll give you some tips that will help you answer your questions. This is a direct, objective question and answer site, so we need to avoid chatting through the comments. If we are having a lot of conversation by the comments, it means that your question is very wide-ranging.

  • I recommend you do the following: edit your current question, and make it very objective by decreasing the scope. Remove the part about database from the question. As my answer has already solved your problem from the front end to the $_POST, I recommend that you mark my answer as accepted, and ask another question asking for help to make a loop on the $_POST data. This way, I and other users can help you, in a more organized way, with the rest of your question. That is, always break your problem into small questions, and we solve one thing at a time.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.