andy
andy

Reputation: 459

Add Row To HTML Table & Add to Database After

I have a table that inserts rows into the database. It is currently limited to 2 rows:

 <form action='insert-expenses.php' method='post'>
 <input type='hidden' value='$taskid' name='taskid'>
 <table width='655' border='1'>
 <tr><td align='left' width='350'><input type='text' name='expense1' style='padding:2px;' size='80'></td><td align='center' width='100'><input type='text' name='expensecost1' style='padding:2px;' size='6'></td></tr>
 <tr><td align='left' width='350'><input type='text' name='expense2' style='padding:2px;' size='80'></td><td align='center' width='100'><input type='text' name='expensecost2' style='padding:2px;' size='6'></td></tr>
 <tr><td align='left' colspan='2'><input style='margin-top: 15px;' type='image' name='submit' src='http://bitrix24.co.uk/demo/report/add-week.jpg' ></td></tr>
 </table>

I would like a button to add a new row. I'm currently trying to use:

 <script>
 function displayResult()
 {
 var table=document.getElementById("editTable");
 var row=table.insertRow(0);
 var cell1=row.insertCell(0);
 var cell2=row.insertCell(1);
 cell1.innerHTML="New";
 cell2.innerHTML="New";
 }
 </script>

Then use the button:

 <button type="button" onclick="displayResult()">Insert new row</button>

The table would have the id = editTable. However, how would I get it to have the next numerical name, as I currently have expense1 and expense2 so would want the next to be expense3. I'm assuming I would need to do it as an array and have one row expense[] then on my insert:

sql3="INSERT into b_report_expenses (USER_ID,EXPENSE_ONE,EXPENSE_TWO,COST_ONE,COST_TWO)
VALUES
 ('$_POST[taskid]','$_POST[expense1]','$_POST[expense2]','$_POST[expensecost1]','$_POST[expensecost2]')";

So I would basically like the user to click a button and add a row to the html table and then press another button to add it to the database. I just don't know how the POST['inputname'] will be recognised for each row that's added.

Your help would be appreciated

Upvotes: 0

Views: 198

Answers (1)

developerCK
developerCK

Reputation: 4506

Please don't put a numerical name you should put an array in name like

<input name="expense[]".../>

then in php

ypu would get it like

$expense = $_POST['expense'];

where $expense will be an array having all the values of input in it's element.

Upvotes: 1

Related Questions