-1

I need to md5 a password before sending it via post

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<input type="text" name="password" id="password"><input type="submit" name="submit" value="submit" onclick="encrpt()">

<script>
function encrpt(){
    var password = $("#password").val();
$("#password").val("<?php echo md5(1234); ?>");
    }
</script>

This code works fine and md5 value of 12345 set into password field but whenever I use jQuery password value it gives wrong value like

$("#password").val("<?php echo md5('<script>password</script>'); ?>");

I need to md5 value of the password which I entered into field.

halfer
  • 19,471
  • 17
  • 87
  • 173
Passionate Coder
  • 6,650
  • 2
  • 16
  • 36

3 Answers3

2

You cannot use PHP md5 in javascript function. You have to use external JS library for that. For example : https://github.com/blueimp/JavaScript-MD5

so your code should be like this :

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/blueimp-md5/2.5.0/js/md5.min.js"></script>

<input type="text" name="password" id="password"><input type="submit" name="submit" value="submit" onclick="encrpt()">

<script>
    function encrpt() {
        var password = $("#password").val();
        $("#password").val(md5(password));
    }
</script>
tejashsoni111
  • 1,405
  • 1
  • 17
  • 33
1

Check out the jQuery md5 library, since there's no way to do it using pure jQuery: https://github.com/placemarker/jQuery-MD5

Jamsheed Mistri
  • 379
  • 1
  • 3
  • 17
1

You can use CrypotJs for the purpose

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">      </script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/md5.js"></script>

<input type="text" name="password" id="password"><input type="submit" name="submit" value="submit" onclick="encrpt()">

  <script>
   function encrpt(){
   var password = $("#password").val();
   $("#password").val(CryptoJS.MD5(password).toString());
   }
</script>
geekbro
  • 1,197
  • 10
  • 13