I am working on creating my first form that submits to the database and need a little help. I have most of it working but where I am stuck is on adding a new row. I have a javascript function that lets me add as many rows as I want, but there are 2 issues with it. The first is that using the Javascript, when I hit submit the php form doesn't seem to "see" the new fields that are added. The second issue is that I am setting a key:value relationship which I don't know how to setup in the javascript.
The issue is that as you can see in the code, I am setting up a table row with 2 fields, one is the key, the other the value, but how can you declare that when you don't know what will be typed in there?
<script type="application/javascript">
function addRow(S_ID, S_NAME){
var addRow ='<tr><td><input type="text" name="" value=""></td><td><input type="text" name="" value=""></td></tr>';
$("#applyTable").append(addRow);
}
</script>
<table width=400>
<tr><th>Setting</th><th>Value</th></tr>
</table>
<form action="functions.php?do=save" method="POST" id="saveSettings">
<table class="myTable" border="1px" style="width:400px;" >
<tbody id="applyTable">
<?
include 'db.php';
foreach($db->query('SELECT * from settings') as $row) {
print_r("<tr><td>" . $row[1] . "</td><td><input type=\"textbox\" name=\"
" . $row[1] . "\" value=\"" . $row[2] . "\"></td></tr>");
}
?>
</tbody>
</table>
<input type="submit">
<input type="button" value="Add" border="1px" onclick="addRow()" />
</form>
Here is the page it gets submitted to:
<?
if ($_REQUEST['do'] == 'save'){
include 'db.php';
foreach ($_POST as $key => $value)
{
$sql="UPDATE settings SET value = \"$value\" WHERE name = \"$key\"";
$db->query("$sql");
echo "$sql<br>";
};
};
?>
Any help would be appreciated.