admin管理员组文章数量:1357387
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.
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
$thing = 10;
?>
<script>
var myvar = "<?php echo $thing; ?>";
</script>
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.
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
$thing = 10;
?>
<script>
var myvar = "<?php echo $thing; ?>";
</script>
Share
Improve this question
edited May 23, 2017 at 12:23
CommunityBot
11 silver badge
asked Oct 28, 2013 at 10:25
WilliamWilliam
4,58818 gold badges66 silver badges117 bronze badges
2
- 1 there is no accepted answer in the question you mention. – Michał Rybak Commented Oct 28, 2013 at 10:28
- Sorry I was reading the ments and didn't look for the green check – William Commented Oct 28, 2013 at 10:29
2 Answers
Reset to default 4try this in your ajax.success
success: function(data){
console.log('worky');
alert(data); // It should now, worky!
}
and in you php
<?php
echo 10;
?>
try this in php
<?php $thing = 10; ?>
<script>
var myvar = "<?php echo $thing; ?>";
</script>
javascript
$('body').click(function(){
$.ajax({
type: 'POST',
url: 'test.php',
data: {mydata: irrelevant},
success: function(data){
$("#hiddendiv").html(data);
alert(myvar);
}
});
});
本文标签: How to pass variables from PHP to Javascript using Ajax callsStack Overflow
版权声明:本文标题:How to pass variables from PHP to Javascript using Ajax calls - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743984330a2571213.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论