简体   繁体   English

页面加载时php获取值

[英]php get a value when the page loads

In my website I have a tag like this: <input name="centro-med" type="hidden" value="prova" /> . 在我的网站中,我有一个像这样的标签: <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. 在php代码(在同一页面中编写)中,我需要获取该标记的值,并将其用于在页面加载时执行的查询。 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. 我需要php在页面首次加载时已经在变量中具有该值,以便我可以将其用于查询。 I basically need something like this: 我基本上需要这样的东西:

<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. 编辑:我需要在wordpress页面中使用它,由于wordpress不允许您在页面中编写php,因此我使用了一个可以编写php代码片段的插件。 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. 我现在不明白这是否可以理解,但是我有理由不仅仅在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. 无论如何,我都阅读了答案,我认为唯一的方法就是使用Ajax。

In order for PHP code to read a variable from HTML, it must be posted to the server. 为了使PHP代码能够从HTML读取变量,必须将其发布到服务器。 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.. HTML代码在用户浏览器中运行,与服务器端代码(PHP)通信的唯一方法是提交表单或ajax调用等。

Is it only one input element named "centro-med" ? 仅仅是一个名为“ centro-med”的输入元素吗? Try XPath if you want the initial value... 如果需要初始值,请尝试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将完成运行。

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

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