dongxindu8753 2013-10-08 00:55
浏览 10

包含php的js函数在未调用时执行

I have the following code within the tag of my page:

<script>
    function LogInOut()
    {
        // Get the current login status
        alert("executing LogInOut");
        $loginStatus = "<?php echo $_SESSION['login']; ?>";
        if ($loginStatus == "true")
        {
            <?php
                echo "<br />script function";
                $_SESSION['login'] = "false";
                session_destroy();
            ?>
            document.getElementById("loginState").innerHTML = "login";
        }
        else
        {
            window.location = 'login.php';
        }
    }

</script>

I find that the php code executes when the page loads. The function (for debugging) is NEVER called yet the php code executes while none of the rest of the script executes! Can anyone clarify why this could be happening?

thank you, Shimon

  • 写回答

2条回答 默认 最新

  • dongyied24121 2013-10-08 01:07
    关注

    Classic case of mixing Javascript, a client side language, with PHP, a server side language. They run at two different locations and that being said this will never be possible.

    PHP runs before javascript and if your trying to mix it with javascript, use it to echo dynamic data. eg:

    var logged_in = <?=($_SESSION['login'] ? 'true' : 'false')?> ;
    

    Javascript runs within the browser and after PHP, do not use php code thinking it will run inside of the browser

    评论

报告相同问题?

悬赏问题

  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?