duanqu9292 2012-05-15 00:55
浏览 36

登录后在页面上显示全名

I need to display the user's full name somewhere on the webpage after the user has logged in (preferably in the the same sidebar). Have looked everywhere and am stuck as to where to start in javascript and html. Here is the code for the login form I have so far.

<div id="sidebar-wrapper">
     <div id="sidebar">
     <h3>Login</h3>
     <form name="form1" action="#">
     <ul id="sidenotes">
     <form name="form1" action="tryjsref_form_onsubmit.htm" onsubmit="return allnumeric(uage);greeting()" method="post">

First Name: <input type="text" name="fname" size="25"/><br/>
Last Name: <input type="text" name="lname" size="25" value= ""/><br/>
Age: <input type="text" name="text1" size="25" value=""/><br/>
     <p><input type="submit"name="submit"value="Submit"onclick="allnumeric(document.form1.text1); greeting()"/> 
 </p>
</form>
       </ul>
      </div> 
  • 写回答

1条回答 默认 最新

  • douzhan2027 2012-05-15 01:05
    关注

    I have no idea how to use Java as a preprocessor, but the basic idea is going to be this:

    1. Use Java to grab the information from POST
    2. Store this somewhere persistent (in PHP I'd use the session information, and Java would probably have something similar)
    3. Every time the user visits another page, pull their name from the persistent store and insert it into the sidebar.

    If you meant Javascript rather than Java than things will be different (I'm not sure there is a persistent store in Javascript), but I hope this helps. If you were doing it in PHP I'd be able to write a quick script to demonstrate. I still could if you'd like, it might be of some use.

    Edit:

    I'd add this to the comments, but I wouldn't have the space. Anyway, I wrote the PHP script (spent a good amount of time wondering why I was getting a 500 error, turns out I forgot a close parenthesis). This isn't javascript, but it will hopefully help you write your code since it's the same basic idea. You can find the script in production here. The source:

    <?php
    session_start(); // Start the session so we can use session variables
    
    if(isset($_POST['first_name'])) // Assume that if the user filled out their first name they filled out everything
    {
        // Assign values based off the POST values
        $_SESSION['first_name'] = $_POST['first_name'];
        $_SESSION['last_name'] = $_POST['last_name'];
        $_SESSION['age'] = $_POST['age'];
        $_SESSION['loggedin'] = true; // Used to tell if the user's logged in or not
    }
    
    $first_name = $_SESSION['first_name']; // Read the first name from session data
    $last_name = $_SESSION['last_name']; // Read the last name from session data
    $age = $_SESSION['age']; // Read the age from session data
    $loggedin = $_SESSION['loggedin'];
    
    if(!$loggedin) // If the user isn't logged in, display the login form
    {
    ?>
    <html>
    <head>
    <title>Test script</title>
    </head>
    <body>
    <form action="test.php" method="post">
    First Name: <input type="text" name="first_name" />
    Last Name: <input type="text" name="last_name" />
    Age: <input type="text" name="age" />
    <input type="submit" />
    </form>
    <?php
    }
    else // Otherwise, say hello!
    {
        echo "Hello, " . $first_name . " " . $last_name . "! You are " . $age . " years old!";
    }
    ?>
    </body>
    </html>
    

    What this does is it starts a session in PHP and stores the values there. You seem to be familiar with what POST is, so I'll just say that $_POST stores values sent via POST. $_SESSION is the array for session data. Both of these are associative arrays, so they store based off keys rather than locations (i.e. $a['hello'] vs $a[2]). After it starts the session it reads values from POST to session data and then from session data to local variables (not the best way to do things, but it works for a demonstration). Then it shows either the greeting or the login form based on if the user's logged in or not.

    This same idea can be applied to Javascript with cookies. I'm really not familiar with Javascript so I don't know how much help just code would be so I'll link you to This tutorial on cookies. The idea is to check the cookie when the page loads, set it if it isn't set, and load the data from it if it is set. It shouldn't be too hard to do, especially if you can use my code to help you along.

    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路