I know this question has been asked a lot of times but I want to use axios to submit data from a form to an api but it's not working when I click the submit button, it clears and then submits empty fields to the api.
I know there is a event.preventDefault() method that should take care of this but I don't know where to put it in my code.
Here is my code:
window.onload = function submitData(){
const name = document.getElementById('name').value
const email = document.getElementById('email').value
const contacts = document.getElementById('contacts').value
const location = document.getElementById('location').value
const username = document.getElementById('username').value
const password = document.getElementById('password').value
axios.get('http://example.com/register', {
investorNames: name,
investorEmail: email,
investorContacts: contacts,
investorLocation: location,
username: username,
password: password
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
HTML
<form id='register-form'>
<input type="text" placeholder="name" required id="name">
<input type="email" placeholder="Email" required id="email">
<input type="contacts" placeholder="contacts" required id="contacts">
<input type="location" placeholder="location" required id="location" >
<input type="username" placeholder="username" required id="username">
<input type="password" placeholder="Password" required id="password">
<input class="button" name="submit" type="submit" value="submit" onclick="submitData()" />
</form>