PHP里面的JavaScript函数 - php

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center。

7年前关闭。

我想做的是在脚本标记内在php循环内运行javascript。
例如:

<script>
$('#mydiv').mouseover(function(){
time();
function time(){
<?php  
    $secs = 45; 
    $secs--;
if($secs <= 25){
?>//javascript code here 
}
});
</script>

我的主要目的是当用户将鼠标悬停在div上时,运行javascript函数,并且该javascript函数中有php(如果有条件)。如果时间少于25,则执行指定的javascript代码。如果时间少于10,则执行另一种javascript函数。任何类型的帮助将不胜感激。谢谢

参考方案

JavaScript是客户端脚本,PHP是服务器端脚本。因此,您可以对服务器进行AJAX调用,也可以仅对纯JS进行调用。

AJAX + PHP:

<script>
    $('#mydiv').mouseover(function(){
        $.ajax({
            url: 'secs.php',
            success: function(data) {
                var secs = parseInt(data);

                if(secs <= 10) {
                    // do stuff
                }
                else if(secs <= 25) {
                    // do stuff
                }
            },
        });
    });
</script>

secs.php:

<?php
    session_start();

    if(!isset($_SESSION['secs'])) $_SESSION['secs'] = 45;
    else echo $_SESSION['secs'] --;
?>

纯JS:

<script>
    var secs = 45;

    $('#mydiv').mouseover(function(){
        secs --;

        if(secs <= 10) {
            // do stuff
        }
        else if (secs <= 25) {
            // do stuff
        }
    });
</script>

php Singleton类实例将在多个会话中保留吗? - php

举一个简单的例子,如果我想计算一个不使用磁盘存储的脚本的命中次数,我可以使用静态类成员来执行此操作吗?用户1:<?php $test = Example::singleton(); $test->visits++; ?> 用户2:<?php $test = Example::singleton(); $test->visits+…

PHP strtotime困境 - php

有人可以解释为什么这在我的服务器上输出为true吗?date_default_timezone_set('Europe/Bucharest'); var_dump( strtotime('29.03.2015 03:00', time()) === strtotime('29.03.2015 04:00�…

PHP-全局变量的性能和内存问题 - php

假设情况:我在php中运行一个复杂的站点,并且我使用了很多全局变量。我可以将变量存储在现有的全局范围内,例如$_REQUEST['userInfo'],$_REQUEST['foo']和$_REQUEST['bar']等,然后将许多不同的内容放入请求范围内(这将是适当的用法,因为这些数据指的是要求自…

php-casperjs获取内部文本 - php

我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…

PHP-将日期插入日期时间字段 - php

我已在数据库中使用datetime字段存储日期,使用PHP将“今天的日期”插入该字段的正确方法是什么?干杯, 参考方案 我认为您可以使用php date()函数