0

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.

Alan
  • 2,046
  • 2
  • 20
  • 43
  • You are giving the same name `addRow` for variable and function, the function does contain two parameter which are not passed. – Suman Bogati Mar 30 '14 at 04:15
  • The reason your the PHP doesn't read the new inputs is because you haven't given them a name. You could use [HTML input array](http://stackoverflow.com/questions/1010941/html-input-arrays) for this. – AyB Mar 30 '14 at 04:19
  • Thank you Cheezburger, I think I can use your trick of the input array to also set a "type" (textbox, textarea, true / false) and also a "state" (actice / inactive). – Alan Mar 30 '14 at 22:58

1 Answers1

0

first of all, it seems that key and value are not set to name of input elements, I guess it should look like

   function addRow(S_ID, S_NAME){
       var addRow ='<tr><td><input type="text" name="'+s_ID+'" value=""></td><td><input type="text" name="'+S_NAME+'"    value=""></td></tr>';
       $("#applyTable").append(addRow);
    }

and parameters are not set for clicking on button, should it be something like:

<input type="button" value="Add" border="1px" onclick="addRow('mykey', 'myvalue')" />

the form will not post any fields to action script with the absence of NAMEs for input elements if all the keys and names are the same, just replace mykey and myvalue to mykey[] and myvalue[] to make them arrays

iwcoder
  • 144
  • 6
  • I added the code here and a couple things happened. For some reason it has modified the existing data that gets posted with a bunch of underscores before the $value so that the value would come back looking like "__________value" It also still ignores the new rows upon submit when I do a var_dump($_POST); I don't see the new row data. I may need to put it on a new page, I noticed that the SQL query wouldn't work even if the data was passed to the next page since the query is an UPDATE, but this new one would end up being INSERT. – Alan Mar 30 '14 at 22:54