I read this post and assumed the technique in the answer would work with ajax calls. I have my ajax and php code below but it does not work.The client does not recognize the 'passed' variable. I do not know why nor how to remedy this.
我读了这篇文章并假设答案中的技术可以使用ajax调用。我有我的ajax和PHP代码,但它不起作用。客户端无法识别'传递'变量。我不知道为什么也不知道如何解决这个问题。
Javascript
使用Javascript
var irrelevant = 'irrelevant';
$('body').click(function(){
$.ajax({
type: 'POST',
url: 'test.php',
data: {mydata: irrelevant},
success: function(){
console.log('worky');
alert(myvar); // NOT worky!
}
});
});
PHP File
PHP文件
<?php
$thing = 10;
?>
<script>
var myvar = "<?php echo $thing; ?>";
</script>
2 个解决方案
#1
4
try this in your ajax.success
在你的ajax.success中试试这个
success: function(data){
console.log('worky');
alert(data); // It should now, worky!
}
and in you php
在你的PHP
<?php
echo 10;
?>
#2
0
try this in php
在php中尝试这个
<?php $thing = 10; ?>
<script>
var myvar = "<?php echo $thing; ?>";
</script>
javascript
JavaScript的
$('body').click(function(){
$.ajax({
type: 'POST',
url: 'test.php',
data: {mydata: irrelevant},
success: function(data){
$("#hiddendiv").html(data);
alert(myvar);
}
});
});
#1
4
try this in your ajax.success
在你的ajax.success中试试这个
success: function(data){
console.log('worky');
alert(data); // It should now, worky!
}
and in you php
在你的PHP
<?php
echo 10;
?>
#2
0
try this in php
在php中尝试这个
<?php $thing = 10; ?>
<script>
var myvar = "<?php echo $thing; ?>";
</script>
javascript
JavaScript的
$('body').click(function(){
$.ajax({
type: 'POST',
url: 'test.php',
data: {mydata: irrelevant},
success: function(data){
$("#hiddendiv").html(data);
alert(myvar);
}
});
});