Reputation: 1144
I have this function in the javascript
txJ$(document).ready(function () {
//txJ$(".submit").closest("form").submit(function (e) {
txJ$(".submit").closest("form").submit(function (event) {
//check for encryption key
{ TxEncrypt(event); }
});
});
function TxEncrypt(event)
{ //perform encryption of token data, then submit the form like normal
//obtain public key and initial JSEncrypt object
var txPubKey = '123';
var txEncrypter = new JSEncrypt();
txEncrypter.setPublicKey(txPubKey);
//get Data and encrypt it
var txData = '{}';
var txCryptData = '';
if(txJ$(".data").length > 1)
{ //if there are more than one element with this class, convert it to json string
txData = txJ$(".data").serializeObject();
txCryptData = txEncrypter.encrypt(JSON.stringify(txData));
}
else
{ //else, just encrypt the value
txData = txJ$(".data").val();
txCryptData = txEncrypter.encrypt(txData);
}
dataString = txCryptData;
var xhr = new XMLHttpRequest();
var params=dataString;
var token;
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status==200) {
token=xhr.responseText;
alert(token);
//add value/field to form
txCvv = txJ$(".cvv").val();
var MyForm = txJ$(".submit").closest("form");
txJ$('<input type="hidden">').attr({
id: 'token',
name: 'token'
}).val(token).appendTo(MyForm);
txJ$('<input type="hidden">').attr({
id: 'cvv',
name: 'cvv'
}).val(txCvv).appendTo(MyForm);
//scrub data that we do not want to post
txJ$(".data").removeAttr('name');
txJ$(".cvv").removeAttr('name');
}
}
xhr.open('POST', 'tokenize.php', true);
xhr.send(params);
event.preventDefault();
}
The form is
<form method="POST" action="pp.php">
<input type="text" class="data" name="ccnumber" value="4242424242424242" />
<input type="text" class="cvv" name="cvv" />
<input type="submit" class="submit" value="tokenize" />
</form>
What the script does is,get the value from the input field with data as class and process it from xmlhttpsrequest and get a token from the server and append the token as hidden field in the form and then submits the forms.This is what I wanted to do.
when I click the submit button , all of this happens.
But obviously as there is event.preventDefault();
the form will not get submitted to action as normal.But if I remove event.preventDefault();
,the hidden fields are not getting appended,more over xmlhttprequest is not being sent.
I tried MyForm.submit();
inside the if (xhr.readyState == 4 && xhr.status==200) {
,but what it does is call the same function again and again.
Is there some way I can submit the form to the action (not call this function) after the function has run?
Upvotes: 0
Views: 146
Reputation: 13728
i am giving you code example you need to refine and do your stuff with code
First add an id="myform"
to form
$(document).ready(function () {
$("#myform").submit(function (e) {
e.preventDefault(); // stop refresh form
// get your data and do your stuff with that
$.ajax({
type: "POST",
url: "tokenize.php",
// send your data will be post
data: {
name: "Rakesh",
time: "2pm"
},
success: function (data) {
// remove elements from form
$('.data').remove();
$('.cvv').remove();
// adding a hidden field to form
$('<input />').attr('type', 'hidden').attr('name', "something").attr('value', "something").appendTo($('.submit').closest('form'));
}
});
});
});
Upvotes: 1