简体   繁体   English

PHP变量与JavaScript回显

[英]php variable with javascript echoing

Im working on modifying my page once a user is logged in. (changing the login logo and display welcome username next to it). 我会在用户登录后修改页面。(更改登录徽标并在其旁边显示欢迎用户名)。 so far the img swapworks, but i cannot seem to add $userId into the script to display the sessions username, just the welcome back part works if i remove the $userId. 到目前为止,img swapworks仍然有效,但是我似乎无法在脚本中添加$ userId以显示会话用户名,如果我删除了$ userId,则仅欢迎使用后部。 Its weird because when i do this with PHP and HTML it works quite easily. 这很奇怪,因为当我使用PHP和HTML进行此操作时,它很容易工作。

<?php 
if (isset($_SESSION['logged_in'])) { 
    $userId = $_SESSION['username'];

    echo '<script>  
        var img = document.getElementById("imgSwap");
        img.src = "images/lockClosed.png"; 
        </script>';

    echo '<script>
        var userId = '."$userId".';
        var text = document.getElementById("loggedInMsg");
        text.innerHTML = "Welcome Back, + userId"; 
        </script>';        
}

If $userId is a string, it needs to be quotead in JS. 如果$userId是字符串,则需要在JS中将其引号引起来。

This should work: 这应该工作:

<?php 
if (isset($_SESSION['logged_in'])) { 
    $username = $_SESSION['username'];

    // Let's end the PHP block and output our JS
    ?>

    <script>  
        var img = document.getElementById("imgSwap");
        img.src = "images/lockClosed.png"; 

        var username = '<?= $username ?>';
        var text = document.getElementById("loggedInMsg");
        text.innerHTML = "Welcome Back, " + username; 
    </script>;        

    <?php
    // Now when we're done with the JS, open the PHP block again
}

In my opinion, it's way easier to end the PHP block instead of echoing HTML and JS using PHP, since you then get some proper syntax highlighting and don't need to care about escaping quotes etc. 我认为,结束PHP块比使用PHP回显HTML和JS更容易,因为这样您就可以正确地突出显示语法,而不必担心转义引号等问题。

(I also changed the variable $userId to $username , since it's the username we want to output, not the id) (我还将变量$userId更改为$username ,因为它是我们要输出的用户名,而不是id)

So, what was the actual issue? 那么,实际的问题是什么?

You have a couple of issues: 您有几个问题:

First issue 首要问题

'var userId = '."$userId".';`

Since $userId here is a string with the username, it wouldn't be quoted and result in: var userId = foo; 由于这里的$userId是带有用户名的字符串,因此不会被加引号并导致: var userId = foo; instead of var userId = 'foo'; 而不是var userId = 'foo'; .

You need to make quote it like this: 您需要像这样报价:

'var userId = "' . $userId . '";`

As you see, now the " is in the JS string instead of around the PHP variable. 如您所见,现在"位于JS字符串中,而不是PHP变量周围。

Second issue 第二期

`'text.innerHTML = "Welcome Back, + userId";` 

Here's you're not concatenating the strings, but are literally outputting "Welcome Back, + userId"; 这不是在串联字符串,而是从字面上输出"Welcome Back, + userId"; . For the strings to be concatenated, you need to end the first string: 对于要串联的字符串,您需要结束第一个字符串:

`'text.innerHTML = "Welcome Back, " + userId;` 

It could be the problem of the quotes. 这可能是报价问题。 you can try include quotes when echo out the value 您可以在回显值时尝试使用引号

<?php if(isset($_SESSION['logged_in'])){ 
        $userId = $_SESSION['username'];
        echo '<script>  
            var img = document.getElementById("imgSwap");
            img.src = "images/lockClosed.png"; 
            </script>';
        echo '<script>
            var userId = "'.$userId.'";
            var text = document.getElementById("loggedInMsg");
            text.innerHTML = "Welcome Back, + userId"; 
            </script>';        
}

If you want to concatenate variable to string in javascript you have to seperate it from string since + is just another ASCI character if it is in quotation marks and it does not concatenate : text.innerHTML = "Welcome Back, + userId"; 如果要在javascript中将变量连接为字符串,则必须将其与字符串分开,因为+只是另一个ASCI字符(如果用引号引起来,并且不连接): text.innerHTML = "Welcome Back, + userId"; should be text.innerHTML = "Welcome Back, "+ userId; 应该是text.innerHTML = "Welcome Back, "+ userId;

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

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