简体   繁体   English

如何将值从javascript生成的数据保存到PHP变量?

[英]How to save a value from javascript-generated data to PHP variable?

The reverse of this one: 这与之相反:

<script>
document.getElementById("jscript_element").value = '<?=$php_variable?>';
</script>

Is it possible? 可能吗?

Note: Post/Get must not be used since the script will be integrated only in an onChange event, then the rest is history. 注意:不得使用Post / Get,因为脚本将仅集成在onChange事件中,其余为历史记录。

Thanks in advance! 提前致谢!

You'll either have to write the value to an input with Javascript, and record the value when the form is posted, or use AJAX to send the value off to your server for processing. 您将不得不使用Javascript将值写入输入,并在表单发布时记录该值,或者使用AJAX将值发送给服务器进行处理。 Once the page is loaded, PHP is done, period. 页面加载后,PHP完成。

I think the answer to your question lies in the order in which the two types of script are interpreted. 我认为您的问题的答案在于两种脚本的解释顺序。

document.getElementById("jscript_element").value = '<?=$php_variable?>';

Anything in the <? ?> <? ?>任何内容<? ?> <? ?> tags is interpreted by the server as PHP and then written to the page. 服务器将<? ?>标记解释为PHP,然后将其写入页面。 This fully complete page is then transferred to the client. 然后,此完全完整的页面将传输到客户端。 Note that the PHP only runs on the server. 请注意,PHP仅在服务器上运行。

Once the page is loaded and presented to the client, the PHP code does not exist. 页面加载并呈现给客户端后,PHP代码不存在。 To set a PHP variable, you must transfer that data back to the server somehow. 要设置PHP变量,您必须以某种方式将该数据传输回服务器。

You specify that you cannot use any type of Post or Get. 您指定不能使用任何类型的发布或获取。 Whether synchronously via a form action, or asynchronously via AJAX, any type of communication will fall under the category of either a Post or Get. 无论是通过表单动作同步,还是通过AJAX异步,任何类型的通信都将属于“发布”或“获取”类别。

So, with that in mind: 因此,请牢记:

Is it Possible? 可能吗?

No. 没有。

Although setting javascript generated data into a php variable is not directly possible, you can follow the below method to achieve this if required. 尽管无法直接将javascript生成的数据设置到php变量中,但是如果需要,您可以按照以下方法来实现。

After getting the value that you want to be available in php, store the value in a cookie. 在获得想要在php中可用的值之后,将该值存储在cookie中。

<script type="text/javascript">
generated_value=document.getElementById("jscript_element").value;
document.cookie="cookie_name="+generated_value;
</script>

Then in your php script just read the value from $_COOKIE array. 然后在您的PHP脚本中,只需从$ _COOKIE数组中读取值即可。

<?php $generated_value = $_COOKIE['cookie_name']; ?>

It will require a page reload, but you will be able to use the javascript generated value. 它将需要重新加载页面,但是您将能够使用javascript生成的值。

Behind your question there is, apparently, confusing between Server-Side actions, and Client-Side actions. 在您的问题的背后,显然服务器端操作与客户端操作之间存在混淆。

You should read some info about this subject, like this article: http://www.virtualshowrooms.co.za/articlepage.php?cp=101 . 您应该阅读有关此主题的一些信息,例如本文: http : //www.virtualshowrooms.co.za/articlepage.php?cp=101

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

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