Issue
I have html form:
<form action="file.php" method="post">
<input name="formName" type="text" />
<input name="formEmail" type="email" />
<input name="formSubmit" type="submit" value="Submit Me!" />
</form>
So how to use Fetch API in order to get those values and send them to file.php file using ajax?
Solution
Using Fetch API
function submitForm(e, form){
e.preventDefault();
fetch('file.php', {
method: 'post',
body: JSON.stringify({name: form.formName.value, email: form.formEmail.value})
}).then(function(response) {
return response.json();
}).then(function(data) {
//Success code goes here
alert('form submited')
}).catch(function(err) {
//Failure
alert('Error')
});
}
<form action="file.php" method="post" onsubmit="submitForm(event, this)">
<input name="formName" type="text" />
<input name="formEmail" type="email" />
<input name="formSubmit" type="submit" value="Submit Me!" />
</form>
Answered By - Muthu Kumaran
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.