简体   繁体   English

在PHP数组中调用JavaScript函数

[英]Calling a JavaScript function inside PHP Array

While you can pass a PHP array to a JavaScript function , can you pass the opposite? 虽然可以将PHP数组传递给JavaScript函数 ,但可以传递相反的信息吗? (a JavaScript function into an PHP array). (将JavaScript函数转换为PHP数组)。

Consider this code. 考虑下面的代码。 I want to call a JavaScript timer inside a PHP array, as the array later randomizes messages for an echo. 我想在PHP数组中调用JavaScript计时器,因为该数组稍后会将消息随机化以进行回显。

$messages = array('How are you today?', 'Perfect day for flying, isnt it?', 'You have flown last on '.$lastflown.'.', 'The time is /* insert the JavaScript function to call a timer here */');
$key = array_rand($messages);

And of course I want to display the message: 当然,我想显示以下消息:

<?php echo $messages[$key]; ?>

Is this possible? 这可能吗? If not, how can this be bypassed? 如果没有,如何绕开? Perhaps a JavaScript randomizer instead? 也许用JavaScript随机化器代替?

Maybe this code help you. 也许这段代码可以帮助您。

$messages = array('How are you today?', 'Perfect day for flying, isnt it?', 'You have flown last on '.$lastflown.'.', 'The time is <script> document.write(new Date().toString());</script>');
$key = array_rand($messages);

<?php echo $messages[$key]; ?>

You need surround your JavaScript code with tags. 您需要在JavaScript代码中加上标签。 To show JavaScript output use document.write() function. 要显示JavaScript输出,请使用document.write()函数。

This string 'The time is <script> document.write(new Date().toString());</script>' will be show like The time is Wed Oct 21 2015 17:18:32 GMT+0300 (MSK) in browser. 此字符串'The time is <script> document.write(new Date().toString());</script>'将显示为The time is Wed Oct 21 2015 17:18:32 GMT+0300 (MSK)在浏览器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM