将 js 值分配给 php [英] assigning js value to php

查看:42
本文介绍了将 js 值分配给 php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的 HTML 文件中有这个

so I have this in my HTML file

 <script type="text/javascript" src="load.js"> </script>    
    <script type="text/javascript" src="load2.php"> </script>

在某处看到您可以使用 js 脚本标记调用外部 php 文件,并且 php 文件中的任何 js 都将首先呈现.但我的问题是,我无法将 load.js 中的任何 JS 变量分配给 load2.php 中的 php 变量,但是,我可以使用 echo "alert(js variable);";所以这告诉我它找到了变量并且我可以使用它,但是我不能将它分配给一个 php 变量:/请问有什么帮助吗?

Saw somewhere you could call an external php file using the js script tag and whatever js in the php file would be rendered first. But my problem is, I can't assign no JS variable from load.js to a php variable in load2.php but, however, I could alert using echo "alert(js variable);"; So that tells me it finds the variable and I could use it, but I can't assign it to a php variable :/ any help please?

推荐答案

在 JavaScript 和 php 之间工作时,您必须记住一个是客户端 (JavaScript),另一个是服务器端 (php).因此,php 可以在将值发送到客户端然后呈现(处理)之前将值插入到 javascript 中.尽管相反的情况不会发生,因为 JavaScript 的范围"在客户端计算机上.

When working between JavaScript and php you must remember one is client side (JavaScript) and the other server side (php). Therefore php can insert values into javascript before it is sent to the client and then rendered (processed). Although the reverse cannot happen as the "scope" of JavaScript is on the clients computer.

解决这个问题的方法是使用来自客户端的 AJAX 请求返回到服务器,将您需要的数据/变量值发回.

The way around this is to use AJAX requests from the client back to the server sending back the data / variable values you need.

JQuery 提供了一个易于使用的具有 AJAX 功能的库.

JQuery provides an easy to use library with AJAX functionality.

请参阅http://api.jquery.com/jQuery.ajax/

这篇关于将 js 值分配给 php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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