如何将 JavaScript 变量用作 PHP 变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2379224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use a JavaScript variable as a PHP variable?
提问by Selom
I'm trying to include JavaScript variables into PHP code as PHP variables, but I'm having problems doing so. When a button is clicked, the following function is called:
我正在尝试将 JavaScript 变量作为 PHP 变量包含在 PHP 代码中,但我在这样做时遇到了问题。单击按钮时,将调用以下函数:
<script type="text/javascript">
function addTraining(leve, name, date)
{
var level_var = document.getElementById(leve);
var training_name_var = document.getElementById(name);
var training_date_var = document.getElementById(date);
<?php
$result = "INSERT INTO training(level, school_name, training_date) VALUES('level_var', 'training_name_var', 'training_date_var')" or die("Query not possible.");
?>
</script>
Is it possible?
是否可以?
回答by Gordon
PHP is run server-side. JavaScript is run client-side in the browser of the user requesting the page. By the time the JavaScript is executed, there is no access to PHP on the server whatsoever. Please read this article with details about client-side vs server-side coding.
PHP 在服务器端运行。JavaScript 在请求页面的用户的浏览器中在客户端运行。到 JavaScript 执行时,服务器上无论如何都无法访问 PHP。请阅读这篇文章,详细了解客户端与服务器端编码。
What happens in a nutshell is this:
简而言之,发生的事情是这样的:
- You click a link in your browser on your computer under your desk
- The browser creates an HTTP request and sends it to a server on the Internet
- The server checks if he can handle the request
- If the request is for a PHP page, the PHP interpreter is started
- The PHP interpreter will run all PHP code in the page you requested
- The PHP interpreter will NOT run any JS code, because it has no clue about it
- The server will send the page assembled by the interpreter back to your browser
- Your browser will render the page and show it to you
- JavaScript is executed on your computer
- 您在办公桌下的计算机上单击浏览器中的链接
- 浏览器创建一个 HTTP 请求并将其发送到 Internet 上的服务器
- 服务器检查他是否可以处理请求
- 如果请求是针对 PHP 页面,则启动 PHP 解释器
- PHP 解释器将运行您请求的页面中的所有 PHP 代码
- PHP 解释器不会运行任何 JS 代码,因为它对此一无所知
- 服务器会将解释器组装的页面发送回您的浏览器
- 您的浏览器将呈现页面并将其显示给您
- JavaScript 在您的计算机上执行
In your case, PHP will write the JS code into the page, so it can be executed when the page is rendered in your browser. By that time, the PHP part in your JS snippet does no longer exist. It was executed on the server already. It created a variable $resultthat contained a SQL query string. You didn't use it, so when the page is send back to your browser, it's gone. Have a look at the sourcecode when the page is rendered in your browser. You will see that there is nothing at the position you put the PHP code.
在您的情况下,PHP 会将 JS 代码写入页面,以便在您的浏览器中呈现页面时可以执行该代码。到那时,您的 JS 代码段中的 PHP 部分不再存在。它已经在服务器上执行了。它创建了一个$result包含 SQL 查询字符串的变量。你没有使用它,所以当页面被发送回你的浏览器时,它就消失了。当页面在浏览器中呈现时,请查看源代码。你会看到在你放置 PHP 代码的位置没有任何东西。
The only way to do what you are looking to do is either:
做你想做的事情的唯一方法是:
- do a redirect to a PHP script or
- do an AJAX call to a PHP script
- 重定向到 PHP 脚本或
- 对 PHP 脚本进行 AJAX 调用
with the values you want to be insert into the database.
使用要插入数据库的值。
回答by dg_0175
<script type="text/javascript">
var jvalue = 'this is javascript value';
<?php $abc = "<script>document.write(jvalue)</script>"?>
</script>
<?php echo 'php_'.$abc;?>
回答by Don Dickinson
You seem to be confusing client-side and server side code. When the button is clicked you need to send (post, get) the variables to the server where the php can be executed. You can either submit the page or use an ajax call to submit just the data. -don
您似乎混淆了客户端和服务器端代码。单击按钮时,您需要将变量发送(发布、获取)到可以执行 php 的服务器。您可以提交页面或使用 ajax 调用只提交数据。-大学教师
回答by Quentin
PHP runs on the server. It outputs some text (usually). This is then parsed by the client.
PHP 在服务器上运行。它输出一些文本(通常)。然后由客户端解析。
During and after the parsing on the client, JavaScript runs. At this stage it is too late for the PHP script to do anything.
在客户端解析期间和之后,JavaScript 会运行。在这个阶段,PHP 脚本做任何事情都为时已晚。
If you want to get anything back to PHP you need to make a new HTTP request and include the data in it (either in the query string (GET data) or message body (POST data).
如果您想将任何内容返回给 PHP,您需要发出一个新的 HTTP 请求并在其中包含数据(在查询字符串(GET 数据)或消息正文(POST 数据)中)。
You can do this by:
您可以通过以下方式执行此操作:
- Setting location (GET only)
- Submitting a form (with the
FormElement.submit()method) - Using the XMLHttpRequestobject (the technique commonly known as Ajax). Various libraries do some of the heavy lifting for you here, e.g. YUIor jQuery.
- 设置位置(仅限 GET)
- 提交表单(使用
FormElement.submit()方法) - 使用XMLHttpRequest对象(该技术通常称为 Ajax)。各种库在这里为您完成了一些繁重的工作,例如YUI或jQuery。
Which ever option you choose, the PHP is essentially the same. Read from $_GETor $_POST, run your database code, then return some data to the client.
无论您选择哪个选项,PHP 本质上都是相同的。从$_GET或 中读取$_POST,运行您的数据库代码,然后将一些数据返回给客户端。
回答by Sepehr
I had the same problem a few weeks ago like yours; but I invented a brilliant solution for exchanging variables between PHP and JavaScript. It worked for me well:
几周前我遇到了和你一样的问题;但我发明了一个绝妙的解决方案来在 PHP 和 JavaScript 之间交换变量。它对我很有效:
Create a hidden form on a HTML page
Create a Textbox or Textarea in that hidden form
After all of your code written in the script, store the final value of your variable in that textbox
Use $_REQUEST['textbox name'] line in your PHP to gain access to value of your JavaScript variable.
在 HTML 页面上创建隐藏表单
在该隐藏表单中创建一个文本框或文本区域
在脚本中写入所有代码后,将变量的最终值存储在该文本框中
在 PHP 中使用 $_REQUEST['textbox name'] 行来访问 JavaScript 变量的值。
I hope this trick works for you.
我希望这个技巧对你有用。
回答by mit
You can take all values like this:
您可以采用这样的所有值:
$abc = "<script>document.getElementByID('yourid').value</script>";

