如何通过HTML标记的内容定义PHP变量? [英] How can i define a PHP variable by the content of an HTML tag?

查看:174
本文介绍了如何通过HTML标记的内容定义PHP变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆