Capture Html Form Data With External Js File
I am trying to create a form on an HTML page (that also has Bootstrap code) and when the user clicks the submit button I want the data to be captured and processed by an external j
Solution 1:
You have to change
<button type="submit" class="btn btn-default" method="POST" onclick="process()">Submit</button>
to
<button type="button" class="btn btn-default" onclick="process()">Submit</button>
Also
functionprocess(){
var q1 = document.getElementById("firstquartergrade").value;
var q2 = document.getElementById("secondquartergrade").value;
var result = q1+q2;
document.getElementById("result").innerHTML = result;
}
to
functionprocess(){
var q1 = parseInt(jQuery("#firstquartergrade").val());
var q2 = parseInt(jQuery("#secondquartergrade").val());
var result = q1+q2;
jQuery('#result').html(result);
}
Solution 2:
You can achieve it using ajax.
Here is small demo your can refer :
Note : Change actionUrl as per your requuirement
test.Html:
<formaction='actionUrl'class='ajax'method='post'><inputtype='text'name='name'placeholder='Enter Name'/><inputtype='submit'value='submit'/></form><divid='somediv'></div><scriptsrc="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><scriptsrc='main.js'></script>
Here is global method for ajax.You can use it for all.
Main.js :
$('form.ajax').on('submit',function(){
var vForm=$(this),
url=vForm.attr('action'),
type=vForm.attr('method'),
data={};
vForm.find('[name]').each(function(){
var vInput=$(this),
name=vInput.attr('name'),
value=vInput.attr('value');
data[name] = value;
});
$.ajax({
url:url,
type:type,
data:data,
success:function(response){
$('#somediv').html(response);
}
});
returnfalse;
});
Post a Comment for "Capture Html Form Data With External Js File"