gpt4 book ai didi

javascript - 将 PHP 变量传递给 JavaScript 以在 jQuery.ajax 中使用

转载 作者:行者123 更新时间:2023-11-28 14:47:21 24 4
gpt4 key购买 nike

我想将一个变量从 PHP 传递到 JavaScript 以便在我的 jQuery.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); // refresh every half second
}
})
}


// some more code here


<?php $clink = 'foo.php'; ?>

最佳答案

如何将 PHP 变量传递给 JavaScript

建议:

将必须使用 PHP 生成的内容部署为全局变量,并在脚本中使用它们以保持源代码干净。


<小时/>

PHP 文件中的 HTML

<?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 文件

console.log( window.yourApp );


<小时/>

如何通过ajax获取PHP变量?

PHP 文件中 JSON 编码的 PHP 数组

<?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 文件

// 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 )
}
})


<小时/>

引用文献

<小时/>

上面的解释实际上是一个编辑!

请参阅下面的代码片段,这是标记为已接受的原始答案。

<!-- 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>

关于javascript - 将 PHP 变量传递给 JavaScript 以在 jQuery.ajax 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45900223/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com