开发者

How to reference an HTML element in php?

开发者 https://www.devze.com 2023-01-30 03:50 出处:网络
I have a HTML page. Clicking on one link within the page runs a php function which adds a HTML table to the page (implemented with AJAX, php function is in a separate php file).

I have a HTML page. Clicking on one link within the page runs a php function which adds a HTML table to the page (implemented with AJAX, php function is in a separate php file).

Now, I have a button on the page which is supposed to run another php function which should pick up the data from the table and do something with it. I don't know how to get the data from the table because:

  1. I don't know how to reference (get) a HTML element through php.
  2. My php function is in a separate file.
开发者_Python百科

What if I put everything from the table in POST or GET arguments? Is there a way to reference the table and iterate its rows and get the data that way? Thanks.


This is not a job for PHP, you should use Javascript to capture the table contents and then manipulate them whatever way you would like. If on the other hand the table contents never change then you can just put them into your PHP function.

You can pull the values of the table by first setting an ID for the table like so:

<table id="someTable">
<tr id="someRow">
    <td>Data</td>
    <td>Data2</td>
</tr>
</table>

Then you can call the values from the table using JavaScript:

var row = document.getElementById("someRow");
var cellValues = row.getElementsByTagName("td");

This will put all of the cells into an array. Then you can put them into variables.

var firstCell = cellValues[0].innerText;
var secondCell = cellValues[1].innerText;

Then you can put it into an AJAX Request and send it to your PHP function to be processed.

This will send a GET or POST request to yourfile.php and then you can manipulate the cell rows in whatever manner you would like and send back a result by echoing it out in the PHP file. You can retrieve the results by making the callback function.

Hope this helps, Chris


I don't know how to reference (get) a HTML element through php.

Use JavaScript to get the data from the table.

My php function is in a separate file.

Create another AJAX request to get it processed by PHP,


You cannot reference an HTML Table from PHP. Your JS should just send the table data, extracted out of the html table, as JSON. Your PHP can respond with some more JSON that your JS can use to manipulate the existing table.

A php file that is used as the response to an XHR should not generate JS. Just JSON.


Yes you have to put the data in POST arguments. Do not use GET since the table might get to big. You can use javascript to get all the data from the elements. Send the data with ajax to a php page. Now you can get the post data and so somehting with it (For example persisting).

The reason that you can't get it directly with php is because html and javascript are run by the browser (client side) while php runs in the web server (Server side). You need some sort of communications between these machines to use each others resources.

0

精彩评论

暂无评论...
验证码 换一张
取 消