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?
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:
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
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
(although GET
would work, too, it's not best practice_
You will find all of your form data in the $_POST
superglobal.
First 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.