.submit() not working
.submit() not working
Hi all,
I'm hitting a roadblock in validating my form with PHP so have attempted to use jQuery which I understand... better. I hoped!
.submit() doesn't run the script, nor does it prevent the form being submitted to the PHP.
However, if I use .click in place of .submit, the script runs.
I've bounced in and out of Google and people are saying wrap the function inside document.ready, which I have.
They've also said make sure none of the name attributes are called "submit". They aren't... what now!!
The jQuery is in the head of my HTML and the form elements in the body. document.ready should make it unnecessary to have the jQuery before the closing body tag?
I'm learning jQuery in the mornings on the bus and PHP in thevenings on the bus, so maybe I'm too rookie or getting confused... please help :)
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function($) {
$('#sendform').submit(function(event) {
if ($('input:text').val().length > 0) {
// validate if at least one form field is populated
alert("Well done, at least one form field is populated!");
event.preventDefault(); // to be removed when validation passed
}
else {
// all form fields empty, don't submit
alert("All form fields empty!");
event.preventDefault();
}
});
});
</script>
- <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" name="myForm">
-input form fields here-
<input type="submit" id="sendform" name="something" value="Submit" />
</form>