www.webdeveloper.com
+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2012
    Posts
    2

    Question Help Using Javascript for formhashing...

    Hello I'm working through a tutorial right now and there is a vague portion of the article regarding applying JavaScript to the form. I was hoping someone here could shine some light on the subject.

    Basically I have registration form that is supposed to have its password hashed by JavaScript then handed off to my PHP code to pass the hashed password into MySQL.

    The error I get is "Undefined index: p"

    Here's my javascript:
    Code:
    function formhash(form, password) {
       // Create a new element input, this will be out hashed password field.
       var p = document.createElement("input");
       // Add the new element to our form.
       form.appendChild(p);
       p.name = "p";
       p.type = "hidden"
       p.value = hex_sha512(password.value);
       // Make sure the plaintext password doesn't get sent.
       password.value = "";
       // Finally submit the form.
       form.submit();
    }

    Here's the PHP:

    PHP Code:
    <?php

    // The hashed password from the form
    $password $_POST['p']; 
    // Create a random salt
    $random_salt hash('sha512'uniqid(mt_rand(1mt_getrandmax()), true));
    // Create salted password (Careful not to over season)
    $password hash('sha512'$password.$random_salt);
     
    // Add your insert to database script here. 
    // Make sure you use prepared statements!
    if ($insert_stmt $mysqli->prepare("INSERT INTO members (username, email, password, salt) VALUES (?, ?, ?, ?)")) {    
       
    $insert_stmt->bind_param('ssss'$username$email$password$random_salt); 
       
    // Execute the prepared query.
       
    $insert_stmt->execute();
    }
    ?>
    Here's the HTML:

    HTML Code:
    <form action="process_login.php" method="post" name="login_form">
    <table width="335" border="0" align="center">
      <tr>
        <td>Madisontshirt ID:</td>
        <td ><input type="text" name="email" style="width: 225px;" /></td>
      </tr>
      <tr>
        <td>Password:</td>
        <td><input type="password" name="password" id="password" style="width: 225px;" /></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td><div align="right">
          <input type="button" value="Login" onclick="formhash(document.createElement("input");" />
        </div>
    </form></td>
      </tr>

  2. #2
    Join Date
    Sep 2012
    Posts
    2
    sorry I got it to run by replacing the onclick with the proper js function name and by turning my registration into a PHP function and changing my HTML form value to that function name.

    It's running error free now, but now i am working on getting the code to actually enter something into mysql

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



Recent Articles