0

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?

3
  • 3
    This 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/… Commented Jun 6, 2012 at 13:11
  • For a quick how-to you may also want to read this w3schools.com/php/php_mysql_insert.asp Commented 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. Commented Jun 6, 2012 at 13:19

3 Answers 3

5

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:

  1. I will assume that the rows are equivalent to the data you have on a per record basis
  2. 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

Sign up to request clarification or add additional context in comments.

3 Comments

This does help, very much, thank you. Just for clarity, the correct thing to do is update the hidden input fields when I modify the table cells using Javascript, is that correct? Or, rather, there is no way to bind the hidden input with the table cells?
They're not bound per se, you would have to reload the content (i.e. html); in other words both the form(with it's fields) and the table to reflect any new data or changes as the case may be. Consider the form's hidden fields as something only you the programmer has access to while the table is to tell the user what is saved there
Thanks. Your answer helped me solve it and get things working.
1

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.

2 Comments

Thanks. Could you by any chance edit your code to put things in line and make it more readable? Also, you are using the method post but accessing get...?
MirroredFate...I fixed the form method. Also, is there a tutorial for formatting code snippets properly on Stack Overflow? For example, I had to add an extra space in order to get things to appear within the < > carrots.
0
  • 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.