我试图将一个简单的变量从我的 jQuery 传递到我的 php,然后在我的 HTML 上回显它。不过好像没啥作用。
我无法让变量 $result 显示在我的页面上。有什么想法吗? Ajax POST 似乎工作正常。问题似乎出在 php 和 HTML 之间的通信。
index.html:
<!DOCTYPE html>
<html>
<head>
<title>My Site</title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type='text/javascript' src='front.js'></script>
</head>
<body>
<?php echo $result; ?>
</body>
</html>
front.js:
$(document).ready(function() {
$.post('/back.php', {name: "Bob"}, function(data) {
});
});
返回.php:
<?php $result = $_POST['name']; ?>
最佳答案
试试这个
index.html:
<!DOCTYPE html>
<html>
<head>
<title>My Site</title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type='text/javascript' src='front.js'></script>
</head>
<body>
</body>
</html>
front.js:
$(document).ready(function() {
$.post('/back.php', {name: "Bob"}, function(result) {
$('body').html(result);
});
});
back.php:
<?php echo $_POST['name']; ?>
关于php - 使用 php 的简单 Ajax 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20920951/