I can't get the form to submit, the alert ($('#submit').val()); in the else with the submit comes up so the code is getting into the right place, but the form is not submitting.
Form has an id of othernames, submit button was called name and id of save at one point, currently both are set to submit.
any help appreciated.
$(document).ready(function(){
// force content into cnamex
$('#submit').click(function(event){
event.preventDefault(); // prevent submit
error = '';
for ( var i = 0; i < 10; i++ ) {
if ($('#cname' + i).length > 0) {
if ($('#cname' + i).val().length == 0) {
error = 'Names for those attending are required';
}
}
}
if (error != '') {
alert (error);
}
else {
$('#othernames').submit();
alert ($('#submit').val());
}
});
});
HTML is PHP generated, this is a version of it without any boxes to validate
<form enctype="multipart/form-data" name="othernames" id="othernames" method="post" action="" target="_self">
<div class="table">
<table width="100%" border="0">
<tr>
<td colspan="3" ><input name="submit" id="submit" type="submit" value="Confirm Order" ></td>
</tr>
</table>
</div>
</form>
This is with fields to validate:-
<form enctype="multipart/form-data" name="othernames" id="othernames" method="post" action="" target="_self">
<div class="table">
<table width="100%" border="0"><caption>Enter names for tickets, (if any)</caption>
<tr>
<th scope="col">Name</th>
<th scope="col">Email (Optional)</th>
<th scope="col">Club</th>
</tr><tr><td><input name="cID[]" type="hidden" value="11"><input name="cname[]" id ="cname0" type="text" size="15" maxlength="20"></td>
<td><input name="cemail[]" id="cemail0" type="text" size="15" maxlength="60"></td><td><input name="cclub[]" type="text" size="15" maxlength="25" value="VRA - United Kingdom"></td></tr><tr><td><input name="cID[]" type="hidden" value="11"><input name="cname[]" id="cname1" type="text" size="15" maxlength="20"></td>
<td><input name="cemail[]" id="cemail1" type="text" size="15" maxlength="60"></td><td><input name="cclub[]" type="text" size="15" maxlength="25" value="VRA - United Kingdom"></td></tr>
<tr>
<td colspan="3" ><input name="submit" id="submit" type="submit" value="Confirm Order" ></td>
</tr>
</table>
</div>
</form>
Problem Solved using reyaner's suggestion, thank you. JQuery now reads, and html uses a submit button
$(document).ready(function(){
// force content into cnamex
$('#submit').click(function(event){
error = '';
for ( var i = 0; i < 10; i++ ) {
if ($('#cname' + i).length > 0) {
if ($('#cname' + i).val().length == 0) {
event.preventDefault(); // prevent submit
error = 'Names for those attending are required';
}
}
}
if (error != '') {
alert (error);
}
});
});
enter code here
$(this).closest('form').get(0).submit();BTW, IDs must be unique on context pageevent.preventDefault();to stop the default functionality, why not simply use a regular button and add this functionality?$("form").submit(function(e){ //do stuff, prevent submit or dont if its ok.. });