此代码无法正常运行。我想要的只是将变量$something
发送到page.php
这样做的正确方法是什么? :data: <? php $something; ?>,
脚本
$something = "text";
$.ajax({
url: "page.php",
type: "post",
dataType: "html",
data: <? php $something; ?>,
success: function (data) {
$('#total').load('xxx.php');
}
});
答案 0 :(得分:4)
myFile.php:
<?php $something = 'text'; ?>
<script>
$.ajax({
url: "page.php",
type: "post",
dataType: "html",
data: '<?php echo $something; ?>',
success: function (data) {
$('#total').load('xxx.php');
}
});
</script>
答案 1 :(得分:2)
首先,我认为你错误地混合了PHP和JavaScript。在您的代码中行:
$something = "text";
可以通过两种方式理解。如果这是您拥有的整个代码,那么您实际上正在初始化名为$something
的 JavaScript变量。稍后在代码中,您尝试使用名为$something
的 PHP变量的值。
您需要做的是将代码更改为(假设您要从PHP传递变量):
<?php $something = "text"; ?>
$.ajax({
url: 'page.php',
type: 'post',
dataType: 'html',
data: '<? php $something; ?>',
success: function (data) {
$('#total').load('xxx.php');
}
});
或者(假设你想要JS变量):
var $something = 'text';
$.ajax({
url: 'page.php',
type: 'post',
dataType: 'html',
data: $something,
success: function (data) {
$('#total').load('xxx.php');
}
});