javascript - How to use Fetch API to retrieve and send data from form? -


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 use fetch api in order values , send them file.php file using ajax?

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>


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -