1

If I have a table like this in an html form,

<form method="POST" action="a.php">
<table name="dataTable">
 <tr>
  <td>row one col one</td>
  <td>row one col two</td>
  <td>row one col three</td>
 </tr>
 <tr>
  <td>row two col one</td>
  <td>row two col one</td>
  <td>row two col one</td>
 </tr>
</table>
</form>

how can I access the table data when the form is submitted to p.php?

$dataList = $_POST['dataTable']; ???????
1
  • 2
    Sorry but the table is not a form element. You can't get table data with php only. You need some jQuery in there. Commented Nov 25, 2011 at 10:52

4 Answers 4

2

A straightforward work-around would be to add hidden fields to all your cells:

<form method="POST" action="a.php">
<table name="dataTable">
 <tr>
  <td>row one col one<input type="hidden" name="row_one_cell_one" value="row one col one" /></td>
  <td>row one col two<input type="hidden" name="row_one_cell_two" value="row one col two" /></td>
  etc...

It leaves your view unchanged, but you can acces the cells in your php script with this simple code:

$cellData = $_POST['row_one_cell_one'];
Sign up to request clarification or add additional context in comments.

Comments

1

without any workarounds and conversions, you can't. What you can do is converting your table in a serialized array with JavaScript before submitting and setting the result in a hidden input. Then you can read the value in PHP and de-serialize the array, getting all the values. If you do it right, you can even iterate over the rows and columns.

Comments

1

You can make use on JavaScript to render the table DOM,
and set it to a hidden field right before you do the submission,
so, PHP able to interpret is part of the $_POST

<form method="post" onSubmit="set_value()">
<table name="dataTable">
...
</table>
<input type="hidden" value="" id="hidden_table" name="hidden_table" />
</form>
<script>
function set_value()
{
     var obj = document.getElementById("hidden_table");
     var tbl = document.getElementsByName("dataTable")[1]
     obj.value = tbl.innerHTML;
}
</script>

However, when using JavaScript DOM, it will return full specification of TABLE elements
include THEAD, TBODY ... etc

Comments

0

First link using the topic as keywords in google shows up this: http://wonshik.com/snippet/Convert-HTML-Table-into-a-PHP-Array

I'm sure you can adapt it to your problem.

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.