简体   繁体   中英

PHP a href link with print or echo don't work

I have question about a href link in php field. When I try to make a href link in php field by using echo or print, when i click the link it does not work the link. Could you see the code briefly? Thanks!

 <?php if (isset($_SESSION['name1'])) { echo '<li class="dropdown">'; echo '<a href="#" class="dropdown-toggle" data-toggle="dropdown">'; echo "Hi,"; echo $_SESSION['name1']; echo '<span class="caret"></span></a>'; echo '<ul id="logout-dp" class="dropdown-menu">'; echo '<li>'; echo '<div class="row">'; echo '<div class="col-md-12">'; echo '<div class="form-group">'; echo '<ul>'; echo '<li><div><a class="col" href="logout.php">Logout</a></div></li>'; echo '<li><div><a class="col" href="#">Account</a></div></li>'; echo '<li><div><a class="col" href="resetpassword.php">Reset password</a></div></li>'; echo '</ul>'; echo '</div>'; echo '</div>'; echo '</div>'; echo '</li>'; echo '</ul>'; echo '</li>'; } ?>

Your code is delimiting quotes which is not needed as you are using a different set of quotes to encapsulate the string already.

Change:

'<li><div><a class="col" href=\"logout.php\">Logout</a></div></li>';

to:

'<li><div><a class="col" href="logout.php">Logout</a></div></li>';

This should have stuck out if you inspected the source code you were outputting as then showing the following:

<a class="col" href=\"logout.php\">Logout</a>

Which is clearly not a valid hyperlink.

Edit: Your code in the question has now changed to remove the \\" . Not sure if that was you or someone "correcting" the question.

i modified your code because it was really difficult to follow with all those echo calls.

also made the logout and resetpass links point to the root of the domain, which is where i assume you have them. ex. domain.com/logout.php and domain.com/resetpassword.php

<?php
    if (isset($_SESSION['name1'])) {
        echo '<li class="dropdown">' .  
        '<a href="#" class="dropdown-toggle" data-toggle="dropdown">' . 
        "Hi," . 
        $_SESSION['name1'] .  
        '<span class="caret"></span></a>' . 
        '<ul id="logout-dp" class="dropdown-menu">' . 
         '<li>' . 
          '<div class="row">' . 
           '<div class="col-md-12">' . 
            '<div class="form-group">' . 
             '<ul>' . 
              '<li><div><a class="col" href="logout.php">Logout</a></div></li>' . 
              '<li><div><a class="col" href="#">Account</a></div></li>' . 
              '<li><div><a class="col" href="resetpassword.php">Reset password</a></div></li>' . 
             '</ul>' .                            
            '</div>' . 
           '</div>' . 
          '</div>' . 
         '</li>' .  
        '</ul>' .  
        '</li>';                        
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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