[英]Passing JavaScript variables to PHP as a hidden input
I'm using CodeIgniter and in a single PHP file with JavaScript inside, I want to pass a JavaScript variable to the body (PHP) and make it a hidden input. 我正在使用CodeIgniter,并且在一个包含JavaScript的单个PHP文件中,我想将一个JavaScript变量传递给主体(PHP)并使其成为隐藏的输入。 But whenever I use the controller to post the value (where the JavaScript variable is), it returns none.
但是,每当我使用控制器发布值(JavaScript变量所在的位置)时,它都不会返回任何值。 Here are some parts of the code:
以下是部分代码:
JS: JS:
function pass() {
//some code
document.getElementById('yes').innerHTML = yes; //where yes is a var
}
HTML (PHP): HTML(PHP):
<form action="search">
<input type="hidden" name="yes" value="<?php $yes= "<p id='yes'> </p>"; echo $yes;?>" />
<input type="submit" name="yes" value="Done" />
</form>
So whenever I post the yes
in the controller $yes = $this->input->post('yes');
因此,每当我在控制器中发布
yes
,即$yes = $this->input->post('yes');
it returns nothing. 它什么也不返回。
How can I pass the JavaScript variable so I can use it again in the next file? 如何传递JavaScript变量,以便可以在下一个文件中再次使用它? Thank you!
谢谢!
Your submit
button and your hidden
field have the same name
yes . 您的
submit
按钮,您的hidden
字段具有相同的name
是 。
You try to access your hidden
input by id
yes , and your input does not have this id , use getElementByName('yes')
instead or give your hidden field id='yes'
. 您尝试通过
id
yes访问您的hidden
输入,并且您的输入没有此ID,请改用getElementByName('yes')
或为您的隐藏字段提供id='yes'
。
innerHtml
which only sets or returns the inner HTML of an element,it should be value
. innerHtml
仅设置或返回元素的内部HTML,它应该是value
。 HTML CODE: HTML代码:
<form action="search">
<input id='yes' type="hidden" name="yes" value="<?php $yes= "<p id='yes'> </p>"; echo $yes;?>" />
<input type="submit" name="yess" value="Done" />
</form>
JS : JS:
document.getElementById('yes').value = yes;//yes is a variable
You have to set the value
property of the <input>
, not the innerHTML
. 您必须设置
<input>
的value
属性,而不是innerHTML
。 You also need to give the <input>
a different name than other fields or the "submit" button. 您还需要给
<input>
一个不同于其他字段的名称或“提交”按钮。 Finally, you have to give your <input>
an "id" property so that you can actually get it with getElementById()
. 最后,必须为您的
<input>
一个“ id”属性,以便实际上可以使用getElementById()
获得它。
You should be setting the value
of the hidden input, not the innerHTML
. 您应该设置隐藏输入的
value
,而不是innerHTML
。 This code should work: 此代码应工作:
function pass() {
//some code
document.getElementById('yes').value = yes; //where yes is a var
}
Another problem, as noted by Pointy, is that the hidden input doesn't actually have an id, so you should give it an id (in this case the id should be yes
). 正如Pointy指出的,另一个问题是隐藏的输入实际上没有id,因此您应该为其指定一个id(在这种情况下,id应该为
yes
)。
Something you should also do is escape the html you are inserting into the hidden input with PHP, so it doesn't accidentally get parsed. 您还应该执行的操作是使用PHP将要插入的html逸出到隐藏的输入中,以免意外解析。 You can do this with
htmlspecialchars()
: 您可以使用
htmlspecialchars()
做到这一点:
<form action="search">
<input type="hidden" id="yes" name="yes" value="<?php $yes= htmlspecialchars("<p id='yes'> </p>"); echo $yes;?>" />
<input type="submit" value="Done" />
</form>
You did'nt set the form method so it defaults to GET
您没有设置form方法,因此默认为
GET
You should set 你应该设置
<form action="search" method="POST">
try 尝试
JS : JS:
var yes = "<?php echo $_POST['yes']; ?>";
document.getElementById('yes').innerHTML = yes;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.