gpt4 book ai didi

javascript - 如何通过 jQuery AJAX 传递 PHP 变量

转载 作者:行者123 更新时间:2023-11-30 15:59:44 26 4
gpt4 key购买 nike

好的,我正在尝试关注 this tutorial但我不会将其存储在数据库中,而是将其存储在其他地方。

有一个问题,虽然我不知道如何使用 jQuery 将 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>

这是 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!');
});

任何帮助将不胜感激:)

-亚历克斯

最佳答案

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

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

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

关于javascript - 如何通过 jQuery AJAX 传递 PHP 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37952012/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com