4

I have a form with a method post, but I have to encrypt some of the parameters to md5 before I post them in my javascript. How do I bind the values to that form.

html form:

<input id="sig"  name="signature" >signature <i class="fa fa-usd" aria-hidden="true">
</i></br>

javascript:

vm.sig_md5 = $.md5('value');
0

3 Answers 3

1

Use jQuery .submit() to modify the value at the time of form submission (value is modified before submit, then submit continues).

HTML:

<form action="somewhere.php" method="post">
    <input id="sig"  name="signature" >signature <i class="fa fa-usd" aria-hidden="true"></i></br>
</form>

jQuery/javascript:

$(document).ready(function(){
    $('form').submit(function(){
        var sig = $('#sig').val();
        sig_md5 = $.md5(sig);
        $('#sig').val(sig_md5);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

You can use getter if you don't care about IE<=9

var data = {}
Object.defineProperty(data, 'signature', { 
   get: function() { return document.getElementById('sig').value; },
   set: function(value) { /* set input value hereif needed */ }
})

read more on MDN

Comments

0

Using javascript:

<input id="sig"  name="signature" >signature <i class="fa fa-usd" aria-hidden="true">

<script>

    var vm = {};
    document.getElementById("sig").addEventListener("keyup", function(event) {
        vm.sig_md5 = event.target.value; console.log(vm.sig_md5);
    });

</script>

You can simply attach a keyup event listener and set your variable to it's value.

3 Comments

Thanks I'll try it out. But what if "signature" is a hidden value, do I still include the event listener?
or do I use .bind?
I'm not sure about whether it's hidden.. but I'm sure .bind would work just as well, I tested it in jsFiddle so I used javascript, but you don't have to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.