如何通过HTML标记的内容定义PHP变量? [英] How can i define a PHP variable by the content of an HTML tag?
问题描述
HTML
< p id =aname =a>< / p>
Javascript
的document.getElementById( A)的innerHTML =价格。
如果price是一个变量,我该如何制作一个php变量,在id为a的HTML中?
$ price = $ _POST ['a'];
当这个变量回显时这个变量是空白的,是的,我确实有一个表单设置,一个PHP页面。
这不是表单的工作方式。
浏览器只会发送表单控件的值(<输入>
, < select>
,< textarea>
)。
它赢得了' t发送任意命名元素的值。 (实际上, name =a
是无效的HTML)
相反,您应该使用< input type =hiddenname =avalue =/>
,并设置值
属性这个< input>
标记符合您希望PHP获得的值。
HTML
<p id="a" name="a"></p>
Javascript
document.getElementById("a").innerHTML=price;
If price is a variable how do I make a php variable that is = to the result that is placed in the HTML of id "a" i?
$price = $_POST['a'];
when echoed this variable comes up blank, and yes I do have a form set up that is handled by a PHP page.
That's not how forms work.
The browser will only send values of form controls (<input>
, <select>
, <textarea>
) to the server.
It won't send values of arbitrary named elements. (in fact, that name="a"
is invalid HTML)
Instead, you should use an <input type="hidden" name="a" value="" />
, and set the value
attribute of this <input>
tag to the value you want PHP to get.
这篇关于如何通过HTML标记的内容定义PHP变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!