dro7152 2016-07-10 18:25
浏览 638


I have login button on my navbar, its opening login modal. I want to change login button with username who logged in that modal.

I'am a newbee in php and mysqli. One week and i'm here. Pls give me explained directions about my problem, else i'cant understand :)

Here is my codes;


    include 'config/setup.php'; //database connection here

<!doctype html>
            <!-- showing title dynamicly -->
    <title><?php echo $page['title'].' | '.$site_title; ?></title>

        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="shortcut icon" type="image/x-icon" href="img/favicon.png">

        <?php include "config/css.php"; ?>  <!-- bootstrap css links -->
        <?php include "config/js.php"; ?>   <!-- jquery javascript links -->      

<?php include (D_TEMPLATE.'/navigation.php'); ?>   <!-- including navbar -->

    <div class="container">
        <!-- body items will be here -->

<?php include (D_TEMPLATE.'/footer.php'); ?>     <!-- including footer-->




<?php include 'config/js.php'; ?>

<!-- Navbar -->
<nav class="navbar navbar-inverse">
    <div class="container">    
    <div class="navbar-header"><img src="img/brand.png"></div>

        <ul class="nav navbar-nav">
         <?php nav_main($dbc, $pageid);  ?> <!-- Im showing page links dynamicly in mysql database -->

        <form class="navbar-form navbar-left" role="search">
            <div class="form-group">
              <input type="text" class="form-control" placeholder="Search">
              <a href="#"> <i class="fa fa-search fa-lg" style="color:#FFFFFF;" aria-hidden="true"></i></a>              

<div class="navbar-form navbar-right">


    require 'config/dbc.php';
    $username = $_POST['username'];
    $password = $_POST['password'];
    $result = mysqli_query($dbc, "SELECT * FROM users WHERE username = '$username' AND password = '$password'");
    if(mysqli_num_rows($result) == 1){
        $_SESSION['username'] = $username;
        echo $username;
        echo "problem";

    <!-- Modal Login Button, i'm asking to erase this and write $username when user logged in, php codes is here because of this. In this situation, writing username WITH buttons, i want to erase that buttons after user logged in, it has to be write only username after login. I've tried echo this buttons after  }else{  code; but any buttons not shown before login.I'm trying trying trying -->
    <button id="login" name="login" class="btn btn-primary"><i class="fa fa-sign-in" aria-hidden="true"></i> Login</button> 

    <!-- It will be register modal button, i will work on it later -->
    <button id="register" name="register" class="btn btn-success"><i class="fa fa-user-plus" aria-hidden="true"></i> Register</button>


   <!-- Here is my Login Modal -->

<section id="modal" class="modal fade"> 
    <div class="modal-body">

        <form method="post" action="index.php">

        <div class="form-group">
            <h4><i class="fa fa-sign-in" aria-hidden="true"></i> Login to Your Account
            <a href="#"><span id="close" class="glyphicon glyphicon-remove-circle pull-right"></span></a></h4>

            <div class="form-group">
                <label for="username">Username</label>
                <input type="text" name="username" class="form-control" id="username" placeholder="Enter username">

            <div class="form-group">
                <label for="password">Password</label>
                <input type="password" name="password" class="form-control" id="password" placeholder="Enter password">

            <div class="checkbox">
                <label><input type="checkbox">Remember Me</label>
                <button class="btn btn-primary pull-right" data-dismiss="modal">Close</button>
                <button type="submit" name="login" class="btn btn-danger pull-right"><i class="fa fa-sign-in" aria-hidden="true"></i> Login</button><br><br>
                <a href="#"><label>Forgot password?</label></a>



<!-- It will be my Register Modal -->
<section id="modal_register" class="modal fade"> 
    <div class="modal-body">

        <form action="" method="post" role="form">

          Register Here

            <button id="submit" class="btn btn-danger">Login</button>


  • 写回答

1条回答 默认 最新

  • doushou5761 2016-07-10 18:34

    If Everything else is sorted out usually something like this can work for you

    <button id="login" name="login" class="btn btn-primary"><i class="fa fa-sign-in" aria-hidden="true"></i>  
        <?php if (isset($_SESSION['username'])) : ?>
           <?php echo $_SESSION['username'] ?>
        <?php else: ?>
        <?php endif ?>

    OR after removing the id

        <?php if (isset($_SESSION['username'])) : ?>
           <button class="btn btn-primary"><i class="fa fa-sign-in" aria-hidden="true"></i>  <?php echo $_SESSION['username'] ?> </button>
        <?php else: ?>
           <button id="login" name="login" class="btn btn-primary"><i class="fa fa-sign-in" aria-hidden="true"></i> Login</button>
        <?php endif ?>

    Let me know if you have any problems understanding this code

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 MATLAB代码求涡量
  • ¥100 采用栈和深度优先算法对我的代码进行修改显示路径
  • ¥15 pointnet2包安装
  • ¥20 射频功率问题,解答者有酬谢!
  • ¥80 构建降雨和积水的预测模型
  • ¥15 #Qt Transform setTransform()在鼠标拖动移动视角是一致在原地不动,无法变换视角(细微观察似乎视图有在原地抖动),无法变换视角(细微观察似乎视图有在原地抖动)
  • ¥50 如何利用无人机拍摄的数码照片测量鸟卵的长短径
  • ¥100 github贡献者给与奖励
  • ¥15 使用DS18B20+ESP8266获取温度数据返回-127.00
  • ¥15 odbc代码新增sqlserver数据源