php 使用ajax更改php变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20572601/
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
change php variable with ajax
提问by shivaP
I have an php variable like this:
我有一个像这样的 php 变量:
PHP Code:
PHP代码:
$php_value = 'Am from PHP';
And I want to be able to change this variable with jQuery and the jQuery is on the same page?
我希望能够使用 jQuery 更改此变量并且 jQuery 位于同一页面上?
回答by Quentin
You can't.
你不能。
By the time the page has been delivered to the browser and the JavaScript has run, the PHP program that generated the page will have finished running and the variable will no longer exist.
当页面被传送到浏览器并且 JavaScript 已经运行时,生成页面的 PHP 程序将完成运行并且变量将不再存在。
JavaScript will allow you to send new data to the server (Ajax), where the server could store the data somewhere(a database is usual), and read the response.
JavaScript 将允许您将新数据发送到服务器 (Ajax),服务器可以在其中将数据存储在某处(通常是数据库),并读取响应。
JavaScript will also allow you to modify the page in in the browser (DOM) (including with the data included in the response for an Ajax request).
JavaScript 还允许您在浏览器 (DOM) 中修改页面(包括包含在 Ajax 请求响应中的数据)。
回答by Digital Chris
PHP code is run server-side, and jQuery runs on the client. The way to update a PHP variable from jQuery is to have a jQuery call which submits to the PHP page, and have the PHP look for it:
PHP 代码在服务器端运行,jQuery 在客户端运行。从 jQuery 更新 PHP 变量的方法是让 jQuery 调用提交到 PHP 页面,并让 PHP 查找它:
$php_value = 'Am from PHP';
if exists($_POST['php_value_from_jquery']) {
$php_value = $_POST['php_value_from_jquery'];
}
回答by cssyphus
If I understand your question correctly, AJAX cannot post data to PHP code on the same page. I've been told that it can, but it is not trivial - still, I cannot imagine how that is possible. At any rate, AJAX is easy if a secondary PHP file is used.
如果我正确理解您的问题,AJAX 无法在同一页面上将数据发布到 PHP 代码。有人告诉我它可以,但这并不是微不足道的 - 尽管如此,我无法想象这怎么可能。无论如何,如果使用辅助 PHP 文件,AJAX 很容易。
Here is an example of what I mean. If you try this:
这是我的意思的一个例子。如果你试试这个:
<?php
echo 'Hello';
?>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: 'POST',
url: '',
success: function(data) {
alert(data);
}
});
}); //END $(document).ready()
</script>
</head>
<body>
</body>
</html>
The popup will contain the HTML for the page.
弹出窗口将包含页面的 HTML。
However, if you use two files:
但是,如果您使用两个文件:
file1.php
文件1.php
<?php
echo 'Hello';
?>
file2.php
文件2.php
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: 'POST',
url: 'file1.php',
success: function(data) {
alert(data);
}
});
}); //END $(document).ready()
</script>
</head>
<body></body>
</html>
The popup will contain only the word "Hello".
弹出窗口将只包含单词“Hello”。
To use ajax, you must call an external PHP file.
要使用 ajax,您必须调用外部 PHP 文件。
After considering the above, note that Quentin's answer is important -- even if you use AJAX to set a PHP variable on the server, that variable disappears after the AJAX completes -- just like the PHP variables all disappear after your index.phphas finished rendering the DOM and presenting it to the visitor's browser.
考虑到上述内容后,请注意 Quentin 的回答很重要——即使您使用 AJAX 在服务器上设置 PHP 变量,该变量在 AJAX 完成后也会消失——就像在您index.php完成渲染 DOM后 PHP 变量全部消失一样并将其呈现给访问者的浏览器。
So, what's to be done? Two options.
那么,该怎么办呢?两种选择。
(1) As Quentin points out, you can store values permanently in a database, or
(1) 正如 Quentin 指出的,您可以将值永久存储在数据库中,或者
(2) You can use a PHP superglobal, such as a $_SESSIONvariable. For example:
(2) 您可以使用 PHP 超全局变量,例如$_SESSION变量。例如:
Client side: file2.php
客户端:file2.php
var storeme = "Hello there";
$.ajax({
type: 'POST',
url: 'file1.php',
data: 'stored_on_server=' +storeme,
success: function(data) {
alert(data);
}
});
file1.php
文件1.php
<?php
session_start();
$SESSION['a_variable_name'] = $_POST['stored_on_server'];
You can later retrieve that variable value thus:
您可以稍后检索该变量值,因此:
$.ajax({
type: 'POST',
url: 'file3.php',
success: function(data) {
alert(data); //a popup will display Hello There
}
});
file3.php
文件3.php
<?php
session_start();
echo $SESSION['a_variable_name'];
回答by Krish R
You can't able to change the php value using javascript. i.e Server scripts runs first after that client side script will take effect in that case you cant able to modify the same, since they already rendered in browsers
您无法使用 javascript 更改 php 值。即服务器脚本首先运行,然后客户端脚本将生效,在这种情况下,您无法修改相同的内容,因为它们已经在浏览器中呈现
回答by sqdge
If jQuery is going to be processing the data, then you can assign the PHP variable to a jQuery variable like this:
如果 jQuery 将要处理数据,那么您可以将 PHP 变量分配给一个 jQuery 变量,如下所示:
<script>
var jquery_value = <?php echo $php_value; ?>
</script>
As far as I know, because jQuery is client-side and php is server side, it's not possible to assign a jQuery variable back to PHP.
据我所知,因为 jQuery 是客户端而 php 是服务器端,所以不可能将 jQuery 变量分配回 PHP。

