php在页面加载时获取值

时间:2022-08-24 17:41:22

In my website I have a tag like this: <input name="centro-med" type="hidden" value="prova" />. In the php code (written in the same page) I need to get the value of that tag and use it for a query that is executed when the page loads. The problem is that I can't find a way to get that variable without having to submit a form(and so reload the page). I need that the php has already that value in a variable when the page loads for the first time so that I can use it for the query. I basically need something like this:

在我的网站上,我有这样的标签:。在php代码中(在同一页面中编写)我需要获取该标记的值并将其用于页面加载时执行的查询。问题是我无法找到获取该变量的方法而无需提交表单(因此重新加载页面)。我需要在第一次加载页面时,php已经在变量中具有该值,以便我可以将其用于查询。我基本上需要这样的东西:

<input name="centro-med" type="hidden" value="prova" />

<?php
  $centro = $_POST['centro-med'];
  global $wpdb;
  $risultati = $wpdb->get_results("SELECT * FROM wp_table where centro_medico = '$centro'",ARRAY_A);
?>

I tried looking for other answers but nothing seems to have what I need. I hope you can help me

我试着寻找其他答案,但似乎没有什么我需要的东西。我希望你能帮帮我

EDIT: I need it in a wordpress page, and since wordpress doesn't allow you to write php in pages I use a plugin with which I can write php snippets. I use the same code for different pages, the only thing that changes is that value (prova). So if I could only change that value instead of creating a new snippet for each page I could save lot of time. I don't now if that's understandable but I have reasons to not write it just inside the php. There was no question like this answered. Anyway I read the answers and I think that the only way to do so is with Ajax.

编辑:我需要在wordpress页面,因为wordpress不允许你在页面中编写php我使用一个插件,我可以编写php片段。我对不同的页面使用相同的代码,唯一改变的是值(prova)。因此,如果我只能更改该值而不是为每个页面创建新的代码段,那么我可以节省大量时间。我现在不知道如果这是可以理解的,但我有理由不在php内部写它。毫无疑问,这回答了这个问题。无论如何,我阅读了答案,我认为唯一的方法是使用Ajax。

2 个解决方案

#1


4  

In order for PHP code to read a variable from HTML, it must be posted to the server. The HTML code is running in a users browser and the only way to communicate with the server-side code (PHP) is to submit a form, or an ajax call, etc..

为了使PHP代码从HTML读取变量,必须将其发布到服务器。 HTML代码在用户浏览器中运行,与服务器端代码(PHP)通信的唯一方法是提交表单或ajax调用等。

#2


0  

Is it only one input element named "centro-med" ? Try XPath if you want the initial value...

它只是一个名为“centro-med”的输入元素吗?如果你想要初始值,请尝试XPath ...

Something like:

$dom = new DOMDocument();
$dom->loadHTML('...');
$xp = new DOMXpath($dom);
$nodes = $xp->query('//input[@name="centro-med"]');
$node = $nodes->item(0);

$value = $node->getAttribute('value');

Of course that is the initial, default value when the page loads. This is not what the user has typed in the input box, as by the time the user types anything in the input box, PHP will have finished running.

当然,这是页面加载时的初始默认值。这不是用户在输入框中输入的内容,因为当用户在输入框中键入任何内容时,PHP将完成运行。

#1


4  

In order for PHP code to read a variable from HTML, it must be posted to the server. The HTML code is running in a users browser and the only way to communicate with the server-side code (PHP) is to submit a form, or an ajax call, etc..

为了使PHP代码从HTML读取变量,必须将其发布到服务器。 HTML代码在用户浏览器中运行,与服务器端代码(PHP)通信的唯一方法是提交表单或ajax调用等。

#2


0  

Is it only one input element named "centro-med" ? Try XPath if you want the initial value...

它只是一个名为“centro-med”的输入元素吗?如果你想要初始值,请尝试XPath ...

Something like:

$dom = new DOMDocument();
$dom->loadHTML('...');
$xp = new DOMXpath($dom);
$nodes = $xp->query('//input[@name="centro-med"]');
$node = $nodes->item(0);

$value = $node->getAttribute('value');

Of course that is the initial, default value when the page loads. This is not what the user has typed in the input box, as by the time the user types anything in the input box, PHP will have finished running.

当然,这是页面加载时的初始默认值。这不是用户在输入框中输入的内容,因为当用户在输入框中键入任何内容时,PHP将完成运行。