简体   繁体   English

如何在php中引用HTML元素?

[英]How to reference an HTML element in php?

I have a HTML page. 我有一个HTML页面。 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). 单击页面内的一个链接可运行php函数,该函数将HTML表添加到页面中(通过AJAX实现,php函数位于单独的php文件中)。

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. 现在,我在页面上有一个按钮,该按钮应该运行另一个php函数,该函数应该从表中获取数据并对其进行处理。 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. 我不知道如何通过php引用(获取)HTML元素。
  2. My php function is in a separate file. 我的php函数在一个单独的文件中。

What if I put everything from the table in POST or GET arguments? 如果我将表中的所有内容都放在POST或GET参数中怎么办? 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. 这不是PHP的工作,您应该使用Javascript捕获表内容,然后以任意方式对其进行操作。 If on the other hand the table contents never change then you can just put them into your PHP function. 另一方面,如果表的内容永不改变,则可以将其放入PHP函数中。

You can pull the values of the table by first setting an ID for the table like so: 您可以通过首先为表设置ID来提取表的值,如下所示:

<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: 然后,您可以使用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. 然后,您可以将其放入AJAX请求中,并将其发送到PHP函数进行处理。

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. 这将向yourfile.php发送GET或POST请求,然后您可以按照任意方式操作单元格行,并通过在PHP文件中回显结果来发送回结果。 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. 我不知道如何通过php引用(获取)HTML元素。

Use JavaScript to get the data from the table. 使用JavaScript从表中获取数据。

My php function is in a separate file. 我的php函数在一个单独的文件中。

Create another AJAX request to get it processed by PHP, 创建另一个AJAX请求以使其通过PHP处理,

You cannot reference an HTML Table from PHP. 您不能从PHP引用HTML表。 Your JS should just send the table data, extracted out of the html table, as JSON. 您的JS应该只将从html表中提取的表数据作为JSON发送。 Your PHP can respond with some more JSON that your JS can use to manipulate the existing table. 您的PHP可以使用其他JSON进行响应,您的JS可使用这些JSON来处理现有表。

A php file that is used as the response to an XHR should not generate JS. 用作XHR响应的php文件不应生成JS。 Just JSON. 只是JSON。

Yes you have to put the data in POST arguments. 是的,您必须将数据放入POST参数中。 Do not use GET since the table might get to big. 不要使用GET,因为表可能会变大。 You can use javascript to get all the data from the elements. 您可以使用javascript从元素中获取所有数据。 Send the data with ajax to a php page. 使用ajax将数据发送到php页面。 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). 使用php无法直接获取它的原因是因为html和javascript是由浏览器(客户端)运行的,而php是在网络服务器(服务器端)中运行的。 You need some sort of communications between these machines to use each others resources. 您需要这些机器之间进行某种形式的通信,才能使用彼此的资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM