如何通过jQuery AJAX传递PHP变量

时间:2022-10-15 15:13:22

Okay so I'm trying to follow this tutorial but instead of storing it in a database I'm gonna store it somewhere else.

好的,所以我正在尝试按照本教程,但不是将其存储在数据库中,而是将其存储在其他地方。

One problem though I have no clue how to append PHP variables with jQuery to the body!

一个问题,虽然我不知道如何将jQuery的PHP变量附加到正文!

Here is index.php

这是index.php

<html>

<head>
    <title>Store Your Browser Resolution!</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="js/main.js"></script>
</head>

<body>
    <button id="btn-POST">CLICK</button>
</body>

Here is Main.js

这是Main.js

$(document).ready(function(){
$("#btn-POST").click(function(){
   $.ajax({
    type: 'POST',
    url: '/index.php',
    data: {
        width        : $(window).width(),
        height       : $(window).height(),
        screen_width : screen.width,
        screen_height: screen.height
    },
    success: function( data )
    {
        console.log('SUCCESS!');
        $("body").append($('<?php $width = $_POST["width"]; $height = $_POST["height"]; $screen_width = $_POST["screen_width"]; $screen_height = $_POST["screen_height"]; ?>'));
        $("body").delay(800).append($('<?php echo  ?>'))
    }
    }); 
});
console.log('Test!');
});

Any help would be appreciated :)

任何帮助,将不胜感激 :)

-Alex

-Alex

2 个解决方案

#1


2  

Once the user has loaded the HTML / PHP site from your server it's impossible to execute more PHP commands.

一旦用户从您的服务器加载了HTML / PHP站点,就无法执行更多的PHP命令。

This is because PHP is server sided, the user requests the document, your server executes all php commands and give the user the resulting HTML document.

这是因为PHP是服务器端,用户请求文档,您的服务器执行所有php命令并为用户提供生成的HTML文档。

Adding PHP code via JavaScript is clientside, so it won't work.

通过JavaScript添加PHP代码是客户端,所以它不起作用。

#2


0  

You wont be able to use php in your javascript file, but you could always declare you javascript variable in your index.php file and call those javascript objects in your javascript file.
ex:

你不能在你的javascript文件中使用php,但是你总是可以在index.php文件中声明你的javascript变量并在你的javascript文件中调用那些javascript对象。例如:

//index.php
<script>
    var width = '<?php echo $_POST["width"]; ?>';
    ....
<script>

now just call the variables in your javascript file.

现在只需调用javascript文件中的变量即可。

//Main.js
$("body").append(width+....);

#1


2  

Once the user has loaded the HTML / PHP site from your server it's impossible to execute more PHP commands.

一旦用户从您的服务器加载了HTML / PHP站点,就无法执行更多的PHP命令。

This is because PHP is server sided, the user requests the document, your server executes all php commands and give the user the resulting HTML document.

这是因为PHP是服务器端,用户请求文档,您的服务器执行所有php命令并为用户提供生成的HTML文档。

Adding PHP code via JavaScript is clientside, so it won't work.

通过JavaScript添加PHP代码是客户端,所以它不起作用。

#2


0  

You wont be able to use php in your javascript file, but you could always declare you javascript variable in your index.php file and call those javascript objects in your javascript file.
ex:

你不能在你的javascript文件中使用php,但是你总是可以在index.php文件中声明你的javascript变量并在你的javascript文件中调用那些javascript对象。例如:

//index.php
<script>
    var width = '<?php echo $_POST["width"]; ?>';
    ....
<script>

now just call the variables in your javascript file.

现在只需调用javascript文件中的变量即可。

//Main.js
$("body").append(width+....);