Skip to content
Advertisement

How to get data from html-table via php

I have html-table and three buttons:

<form action="test.php" method="post">
<table id="table"></table>

<input type="button" id="add" value="Add">
<input type="button" id="delete" value="Delete">
<input type="submit" id="save" value="Save">
</form>

You can see there are no rows in it by default.

In javascript I add rows and cells by clicking the button “Add”. Also I can delete them by clicking the button “Delete”.

When I finish adding rows and cells I click the button “Save”. And by clicking “Save” I want to save the information in txt-file, for example. I know how. As I am new in php, I am not an IT-guy, I do not want to work with MySQL, ajax, etc.

How is it possible to just take the information for html-table by php. In test.php I wrote:

<?php
print_r ($_POST);
?>

to check whether I get the information or not. So I do not.

If you ask yourself why someone do not want to work with mysql and want to save the information in txt-file I can answer: it is probably easier for me as there is not so much information I will work with and I will not have to learn how to work with MySQL.

Advertisement

Answer

In order to pass along the data to PHP the HTML your using must have items in the form with names. Here is how that could look for you.

index.html

<form action="test.php" method="post">
    <table id="table">
        <input type="text" name="rows[1][item1]" value="123test">
        <input type="text" name="rows[2][item1]" value="321test">
    </table>

    <input type="button" id="add" value="Add">
    <input type="button" id="delete" value="Delete">
    <input type="submit" id="save" value="Save">
</form>

test.php

<?php


var_dump($_POST);

/* output of $_POST above
array (size=1)
  'rows' =>
    array (size=2)
      1 =>
        array (size=1)
          'item1' => string '123test' (length=7)
      2 =>
        array (size=1)
          'item1' => string '321test' (length=7)
*/

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement