HTML
<form method='POST' id='signup_form'>
<label for='email'>E-mail:</label><br />
<input type='email' name='email' /><br /><br />
<label for='username'>Username:</label><br />
<input type='text' name='username' /><br /><br />
<label for='password'>Password:</label><br />
<input type='password' name='password' /><br /><br />
<label for='confirmpassword'>Confirm Password:</label><br />
<input type='password' name='confirmpassword' /><br /><br />
<input type='submit' name='signup' />
</form>
JavaScript
const signup_form = document.getElementById('signup_form');
signup_form.addEventListener('submit', function() {
event.preventDefault();
let missing_input = false;
for (let i = 0; i < this.children.length; i++) {
if (this.children[i].type !== undefined && this.children[i].type !== 'submit')
if (this.children[i].value === '') {
missing_input = true;
break;
}
}
if (!missing_input)
this.submit();
});
So I'm trying to do some validation in my JavaScript before I submit my form to PHP. If the user tries to submit the form, I check the inputs to see if they're blank and if any of them are then I don't submit the form. That functionality works fine, but when the form submits, the <input type='submit' name='signup' />
doesn't show up in _POST
. All the other inputs do. It was working fine before I implemented the event listener.
This is what $_POST
looks like after successfully submitting:
Array ( [email] => eric@live.ca [username] => ericsartor [password] => asdasdasd [confirmpassword] => asdasdasd)
Have I done something incorrectly? It definitely works fine if I remove the event listener, but I don't understand why that if preventing the submit button from being included in the request...
EDIT: I found the solution thanks to a link Andre posted. I posted my own answer, but I can't accept it yet.