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. =\
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/
didn’t work my friend.
– Ronierick Holanda
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.
– pagliuca
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>
.– pagliuca
thanks friend! the output got:
array (size=8)
 'username_1' => string 'calha dutotec' (length=13)
 'id_1' => string '2' (length=1)
 'name_1' => string '3' (length=1)
 'salary_1' => string '1000.24' (length=7)
 'username_2' => string 'fita isolante' (length=13)
 'id_2' => string '15' (length=2)
 'name_2' => string '10' (length=2)
 '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'];
$fantasia = $_POST['name_1'];
$endereco = $_POST['salary_1'];
– Ronierick Holanda
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 */ } ?>
– pagliuca
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....
– Ronierick Holanda
@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.
– pagliuca
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.
– pagliuca