2

Basically, is there a way for this to be done?

I'm trying to create a site where when the correct "password" is entered, it takes the user to a page. I have this all working with the following code;-

<div class="row">
    <center>
        <form name="login" style="margin: 0px">
            <INPUT TYPE="text" NAME="pass" size="17" onKeyDown="if(event.keyCode==13) event.keyCode=9;" style="width: 152px; margin: 5px;"><br>
           <input type="button" value="Click to submit!" style="width : 150px; margin: 3px" onClick="TheLogin(this.form)">
        </form>
   </center>
</div>

And this script:-

<script language="JavaScript" type="text/javascript">

function TheLogin() {

    var password = 'Mehra';

    if (this.document.login.pass.value == password) {
        top.location.href="loginyes.html";
    }
    else {
        location.href="loginno.html";
    }
}

</script>

However I'm trying to get it so that if the user inputs something other than the password, it logs this data to a text file for example. So if I type "Mehra" it takes me to loginyes.html and if I type anything else such as "password", it takes me to loginno.html but creates a file called password.txt on my server.

Thanks in advance :)

Beri
  • 11,470
  • 4
  • 35
  • 57
Anil
  • 21
  • 1
  • 3
  • JS is client-side and cannot be used to create a file on the server. This kind of logic should be entirely server-side. As it stands, your code is about as secure as a Sony server. – Rory McCrossan Nov 25 '14 at 11:02
  • You have to use server side scripting like php to do this. – Gowri Nov 25 '14 at 11:02
  • 1
    Validation for Login in JavaScript itself is a blunder. Anyway, every experts are beginners Once :). Try to get some basic knowledge on Server side and client side coding. May be [this](http://programmers.stackexchange.com/questions/171203/what-are-the-difference-between-server-side-and-client-side-programming) will help you – Gopichandar Nov 25 '14 at 11:16
  • Thanks guys, although maybe some context might help. This isn't for a password, I'm basically making a "question-answer" website for a single end user. All I want is to somehow be able to see a list of wrong answers that are input by the answer, and am just trying to figure out the best way to go about doing this. Are there any methods I'm unaware of other than potentially logging this data into a text file with PHP? – Anil Nov 25 '14 at 11:22

2 Answers2

0
  1. Architecture of web prevents from doing any client side manipulations on his machine. It is for security reasons only. If it was possible, getting a virus would be easy.
  2. In this cenario you need to send this request to the server, generate a file and return it to the client or save on server side. It depends on your requiremenrts.

So you need PHP, Java, Groovy or Python web application to do this.

Friendly link:

Local file access with javascript

Community
  • 1
  • 1
Beri
  • 11,470
  • 4
  • 35
  • 57
  • Thanks for the advice! However maybe some context might help. This isn't for a password, I'm basically making a "question-answer" website for a single end user. All I want is to somehow be able to see a list of wrong answers that are input by the answer, and am just trying to figure out the best way to go about doing this. Are there any methods I'm unaware of other than potentially logging this data into a text file with PHP? – Anil Nov 25 '14 at 11:22
  • Add a PHP tag then, because here you just mentiones web part, and you need to specify backend langulate/technology, you are using. http://php.net/manual/en/function.file-put-contents.php – Beri Nov 25 '14 at 12:14
0

Have a look into Local Storage if you absolutely cannot use server side. W3Schools local storage; You should put your styles in css, and the center tag is deprecated.

if(typeof(Storage) !== "undefined") {
     
  var passw=document.getElementById('passwordfield').value;
  localStorage.setItem("passw", passw);
 } else {
      alert("Sorry your browser does not support this script");
 }
 <form name="login" style="margin: 0px">
           <INPUT TYPE="text" NAME="pass" size="17" onKeyDown="if(event.keyCode==13) event.keyCode=9;" id="passwordfield" style="width: 152px; margin: 5px;"><br>
           <input type="button" value="Click to submit!" style="width : 150px; margin: 3px" onClick="TheLogin(this.form)">
           </form>
Billy
  • 2,448
  • 1
  • 10
  • 13
  • Thanks, I will look into this, however does local storage mean that only the user who inputs the data can view that data? Or would I be able to view data that has been stored by any other users machine? – Anil Nov 25 '14 at 11:23
  • read the link, you can get the values if you're on the same domain as the one that set the local storage. It is supposed to be secure, but you could hash it just in case. The best would be to use Server side like php – Billy Nov 25 '14 at 11:26