Ok, so basically I have data I have added to an html table and a save button. I want this button to submit the data in the table to a php function so that I can modify my mysql database using the data. I have the tools to modify the database, as long as I can get the data. However, I have no clue how to do this. What is the best way to accomplish this?
-
3This is very basic stuff; I recommend you work through a basic PHP/databases tutorial. All of them that I know cover this fully. See e.g. stackoverflow.com/questions/2119083/…Pekka– Pekka2012-06-06 13:11:49 +00:00Commented Jun 6, 2012 at 13:11
-
For a quick how-to you may also want to read this w3schools.com/php/php_mysql_insert.aspJan Petzold– Jan Petzold2012-06-06 13:14:26 +00:00Commented Jun 6, 2012 at 13:14
-
@Pekka You are likely right; I have been up all night working on a project, so I am currently really tired. I have been googling looking for a solution for a while now, and haven't found anything, so I figured I would ask here. Thanks for the link.MirroredFate– MirroredFate2012-06-06 13:19:54 +00:00Commented Jun 6, 2012 at 13:19
3 Answers
In Addition to what @John Conde said, for every record presented in the table, you should create it's respective hidden field i.e. a <input type="hidden"... />
Considering that tables have rows and columns, I will make some assumptions:
- I will assume that the rows are equivalent to the data you have on a per record basis
- That the columns are the fields you will update with data on a per record basis
Going on this assumption (let me assume 3 fields i.e. 3 columns and 2 records)
Field 1 Field 2 Field 3
Row 1 a b c
Row 2 d e f
For each of these we'll create corresponding input fields in the form we'll create to contain the submit button like so:
<form action="" method="post">
<!--Row 1-->
<input type="hidden" name="field_1[]" value="a" />
<input type="hidden" name="field_2[]" value="b" />
<input type="hidden" name="field_3[]" value="c" />
<!--Row 2-->
<input type="hidden" name="field_1[]" value="d" />
<input type="hidden" name="field_2[]" value="e" />
<input type="hidden" name="field_3[]" value="f" />
<!-- more rows if they exist -->
<input type="submit" name="my_btn" value="POST IT!" />
</form>
On the PHP side, you could then process the form fields, like so:
foreach($_POST["field_1"] as $id=>$field1_value){
$field2_value = $_POST["field_2"][$id];
$field3_value = $_POST["field_3"][$id];
.......
}
Hope this helps
3 Comments
hidden fields as something only you the programmer has access to while the table is to tell the user what is saved thereFirst put your table into a < form > with some action and a method of choice (GET, POST, etc). Like this:
<br>
< form id="tktk" name="tktk" method="GET" action=" < ? php echo $_SERVER['PHP_SELF'];?>?return=Yes"><br>
< table style="width: 98%;border:0px"><br>
< tr><br>
< td>etc.etc.<br>
Then you can submit it like this:
if(isset($_GET['return']) && $_GET['return'] == 'Yes'){
$sql = "UPDATE [table] SET [field-a] = 1 WHERE [field-b] = '" . $id . "'";<br>
$result = sqlsrv_query($link, $sql) or die('Errant query: '.$sql);<br>
}
But in general, I agree with others who recommend you step through a tutorial first to fully understand what you are coding.
2 Comments
The data in the table either needs to be in form fields such as
<input>,<textarea>, etc so they can be submitted when the form is submitted or the data can be in <input type="hidden"`> fields.Your form needs to set the method to
POST(althoughGETwould work, too, it's not best practice_You will find all of your form data in the
$_POSTsuperglobal.