将一个php变量传递给ajax [英] Passing a php variable to ajax
本文介绍了将一个php变量传递给ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将一个变量从php传递给ajax。我已经尝试了很多东西,但没有成功。
我的ajax函数:
$(document).ready(function(){function getLog(){ms = Date.now(); var chatlink =<?php echo $ clink;?>; $ .ajax({url:chatlink + ms,dataType:'text',success:function(text){$(#chat)。text(text,< br / >); setTimeout(getLog,500); //每半秒刷新一次}})} //更多代码<?php $ clink ='foo.php';?>
你有一个想法如何做到这一点简单?
<如何将PHP变量传递给JavaScript
建议:
>
将您必须使用PHP生成的内容部署为全局变量,并在您的脚本中使用它们以保持源代码清洁。 $ b
PHP文件中的HTML
<?php
error_reporting(-1);
$ foo ='bar'; #1
$ arr_foo = array('bar','baz','blong'); #2
$ arr_js = array(); #3 |开始收集
$ arr_js ['foo'] = $ foo; #3 |扩展集合...
$ arr_js ['arrFoo'] = $ arr_foo; #3
$ arr_js ['null'] = null; #3
$ arr_js ['bool'] = true; #3
$ arr_js ['int'] = 0; #3
$ arr_js ['float'] = 0.123; #3
echo< script>
/ *如果您还没有* /
,请确保声明您的名称空间window.yourApp = window .yourApp || {};
$ b $ / *#1将您的PHP变量作为标记进行部署
如果需要nessecary * /
,则不要忘记正确地包装变量window.yourApp.foo ='$ foo';
/ *#2如果您不确定PHP变量是否包含必须转义的符号
或者具有非基元类型(如数组),那么JSON就在那里以拯救* /
window.yourApp.arrFoo =。 json_encode($ arr_foo)。 ;
/ *#3如果你有很多想要转移到JS的变量,你可以将
收集到一个混合数组中,然后将它们全部部署到一个
如上所述* /
window.yourApp =。 json_encode($ arr_js)。 ; / *最方便的方式恕我直言* /
console.log(window.yourApp);
< / script>;
JavaScript文件
console.log(window.yourApp);
如何通过ajax获取PHP变量
PHP文件中的JSON编码PHP数组
<?php $ arr_js = array(
'foo'=>'bar',
'arrFoo'= >数组('bar','baz','blong'),
'null'=> null,
'bool'=> true,
'int'=> ; 0,
'float'=> 0.123,
'双向魔法:)'=> $ _POST ['yourApp'] ['来自发件人']
);
echo json_encode($ arr_js);
//假设这个文件的名字是ajax-json.php
JavaScript文件
//必须位于与此设置中的ajax-json.php相同的目录中
//确保jQuery可用!
jQuery.ajax({
method:'POST',
url:'ajax-json.php',
dataType:'json',
data:{ yourApp:{'from sender':'pure awesomeness'}},
success:function(response){
console.log(response)
}
})
参考文献
上面的解释是实际上是一个编辑!
请参阅下面的代码段,它是标记为接受的原始答案。
<! - PHP文件中的HTML - > <?php $ clink ='foo.php'; ?>< script> $(document).ready(function(){function getLog(){ms = Date.now(); var chatlink =<?php echo $ clink;?>; $。 ajax({url:chatlink + ms,dataType:'text',success:function(text){$(#chat)。text(text,< br />); setTimeout(getLog,500) ; // refresh every half second}})}}< / script>
i'd like to pass a variable from php to ajax. I've tried a lot of things now, but without success.
My ajax function:
$( document ).ready(function() {
function getLog() {
ms = Date.now();
var chatlink = "<?php echo $clink; ?>";
$.ajax({
url: chatlink+ms,
dataType: 'text',
success: function(text) {
$("#chat").text(text, "<br />");
setTimeout(getLog, 500); // refresh every half second
}
})
}
// some more code here
<?php $clink = 'foo.php'; ?>
Do you have an idea how to do this simple?
解决方案
How to pass PHP variables to JavaScript
Recommendation:
Deploy the content you must generate with PHP as global variables and use them in your scripts to keep your source code clean.
HTML in PHP file
<?php
error_reporting(-1);
$foo = 'bar'; #1
$arr_foo = array( 'bar', 'baz', 'blong' ); #2
$arr_js = array(); #3 | start collection
$arr_js['foo'] = $foo; #3 | extend collection ...
$arr_js['arrFoo'] = $arr_foo; #3
$arr_js['null'] = null; #3
$arr_js['bool'] = true; #3
$arr_js['int'] = 0; #3
$arr_js['float'] = 0.123; #3
echo "<script>
/* make sure to declare your name space if you haven't already */
window.yourApp = window.yourApp || {};
/* #1 deploy your PHP variable as markup
don't forget to wrap variables properly if nessecary */
window.yourApp.foo = '$foo';
/* #2 if you are not sure if the PHP variable contains signs that must be escaped
or have a non primitive type such as arrays then JSON is there to the rescue */
window.yourApp.arrFoo = " . json_encode( $arr_foo ) . ";
/* #3 if you have a lot of vars that you want to transfer to your JS you could
also collect them in an assoziative array and then deploy them all in one
as described above */
window.yourApp = " . json_encode( $arr_js ) . "; /* most convenient way IMHO */
console.log( window.yourApp );
</script>";
JavaScript file
console.log( window.yourApp );
How to get PHP variables via ajax?
JSON encoded PHP array in PHP file
<?php $arr_js = array(
'foo' => 'bar',
'arrFoo' => array( 'bar', 'baz', 'blong' ),
'null' => null,
'bool' => true,
'int' => 0,
'float' => 0.123,
'two way magic :)' => $_POST['yourApp']['coming from sender']
);
echo json_encode( $arr_js );
// lets assume the name of this file is "ajax-json.php"
JavaScript file
// must be located in the same directory as "ajax-json.php" in this setup
// make sure jQuery is available!
jQuery.ajax({
method: 'POST',
url: 'ajax-json.php',
dataType: 'json',
data: {yourApp: {'coming from sender': 'pure awesomeness'}},
success: function( response ) {
console.log( response )
}
})
References
The explanations above are actually an edit!
See snippet below which is the original answer that is marked as accepted.<!-- HTML in a PHP file -->
<?php $clink = 'foo.php'; ?>
<script>
$( document ).ready(function() {
function getLog() {
ms = Date.now();
var chatlink = "<?php echo $clink; ?>";
$.ajax({
url: chatlink+ms,
dataType: 'text',
success: function(text) {
$("#chat").text(text, "<br />");
setTimeout(getLog, 500); // refresh every half second
}
})
}
}
</script>
这篇关于将一个php变量传递给ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文