2017-03-16 12 views
0

我是新手所以請原諒,我還在學習並想測試一些東西。使用HTML表單設置localSession的變量

我想爲我的網站製作註冊表格。我不希望有一個handler.php來處理由表單設置的數據;相反,我希望它被保存爲localStorage數據,這樣當用戶註冊時,它不會保存在我的服務器上,而是保存在它們的localStorage中。

這可能嗎?如果不是,謝謝你告訴我。

這裏是我的意思可視解釋:

A visual explanation

+2

這是可能的,但你不想知道什麼時候有人註冊到你的網站?如果不是的話,無論如何註冊的地方是什麼? – TZHX

回答

0

我沒有看到使用情況,但對於學習的目的下面是它可以實現:

<script> 

    function welcomeUser() { 

     // Check if we have a userName stored for this client. 
     if(localStorage.getItem("userName")) { 

      // Welcome the user if we do! 
      alert("Welcome back " + localStorage.getItem("userName")); 

     } 

    } 

    function saveData() { 

     // Get input values 
     var userName = document.getElementById('userName').value; 
     var email = document.getElementById('email').value; 

     // Save to localStorage  
     localStorage.setItem("userName", userName); 
     localStorage.setItem("email", email); 

    } 

    // Welcome our user. 
    welcomeUser(); 

</script> 

<form name="registerForm" action="javascript:void(0)" onsubmit="saveData()"> 
    <input type="text" id="userName"> 
    <input type="email" id="email"> 
    <input type="submit"> 
</form> 

小提琴:https://jsfiddle.net/a6t4wgzt/1/

參考:https://developer.mozilla.org/en-US/docs/Web/API/Storage/LocalStorage

+0

非常感謝,這確實有幫助。我對錶單的操作感到困惑。非常感謝您的幫助! –

+0

我還是JavaScript新手,只想爲我正在開發的練習項目做一個實驗性註冊表單。再次感謝你! –

+0

沒有問題的朋友,希望你有一個愉快的一天:-) @NabilTharwat –