I have a basic form that is getting multiple form submissions, so I wanted to disable the submit button after the form was checked for all required fields in order to prevent that.
Here is my form code.
<script src="required_fields.js" type="text/javascript"></script>
<form action="mail.php" method="post" onsubmit="return formCheck(this);">
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td><strong>Company Name:</strong></td>
<td><label><span style="display: none;">Company Name</span><input type="text" name="companyname" size="30" /></label></td>
</tr>
<tr>
<td style="text-align: right;"><strong><span style="color: #990000;">*</span> First Name:</strong></td>
<td><label><span style="display: none;">First Name</span><input type="text" name="firstname" size="30" /></label></td>
</tr>
<tr>
<td style="text-align: right;"><strong><span style="color: #990000;">*</span> Last Name:</strong></td>
<td><label><span style="display: none;">Last Name</span><input type="text" name="lastname" size="30" /></label></td>
</tr>
<tr>
<td style="text-align: right;"><strong><span style="color: #990000;">*</span> Street Address:</strong></td>
<td><label><span style="display: none;">Street Address</span><input type="text" name="address1" size="30" /></label></td>
</tr>
<tr>
<td style="text-align: right;"><strong>Suite/Apt:</strong></td>
<td><label><span style="display: none;">Suite/Apt</span><input type="text" name="address2" size="30" /></label></td>
</tr>
<tr>
<td style="text-align: right;"><strong><span style="color: #990000;">*</span> City:</strong></td>
<td><label><span style="display: none;">City</span><input type="text" name="city" size="30" /></label></td>
</tr>
<tr>
<td style="text-align: right;"><strong><span style="color: #990000;">*</span> State:</strong></td>
<td><label><span style="display: none;">State</span><input type="text" name="state" size="2" /></label></td>
</tr>
<tr>
<td style="text-align: right;"><strong><span style="color: #990000;">*</span> Zip:</strong></td>
<td><label><span style="display: none;">Zip</span><input type="text" name="zip" size="4" /></label></td>
</tr>
<tr>
<td style="text-align: right;"><strong><span style="color: #990000;">*</span> Daytime Phone:</strong></td>
<td><label><span style="display: none;">Daytime Phone</span><input type="text" name="dayphone" size="30" /></label></td>
</tr>
<tr>
<td style="text-align: right;"><strong><span style="color: #990000;">*</span> Email Address:</strong></td>
<td><label><span style="display: none;">Email Address</span><input type="text" name="email" size="30" /></label></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submitButtonName" value="Submit Order" style="background-color: #012e63; color: white;" id="mysubmit"/></td>
</tr>
</table>
</form>
Here is my javascript that checks for the required fields.
function formCheck(formobj){
// Enter name of mandatory fields
var fieldRequired = Array("firstname", "lastname", "address1", "city", "state", "zip", "dayphone", "email");
// Enter field description to appear in the dialog box
var fieldDescription = Array("First Name", "Last Name", "Street Address", "City", "State", "Zip", "Daytime Phone", "Email Address");
// dialog message
var alertMsg = "Please complete the following fields:\n";
var l_Msg = alertMsg.length;
for (var i = 0; i < fieldRequired.length; i++){
var obj = formobj.elements[fieldRequired[i]];
if (obj){
switch(obj.type){
case "select-one":
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "select-multiple":
if (obj.selectedIndex == -1){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "text":
case "textarea":
if (obj.value == "" || obj.value == null){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
default:
}
if (obj.type == undefined){
var blnchecked = false;
for (var j = 0; j < obj.length; j++){
if (obj[j].checked){
blnchecked = true;
}
}
if (!blnchecked){
alertMsg += " - " + fieldDescription[i] + "\n";
}
}
}
}
if (alertMsg.length == l_Msg){
return true;
}else{
alert(alertMsg);
return false;
}
}
Now this works fine, the problem is when I try to add in javascript to disable the submit button after my fields are checked to be required.
I have found a script that does disable the submit button, but when I try to "combine" the two javascripts to achieve what I need, I can't get it to work.
Here is the code that disables the submit button.
How do I make this work with the javascript that I am already using?
functionformCheck(formobj){
varmsg='';
if(msg!=''){alert(msg);}
else{
document.getElementById('mysubmit').value='Submitting,pleasewait...';
document.getElementById('mysubmit').disabled=true;
formobj.submit();
}
returnfalse;//Returnfalsepreventsitfrom
//submittingintheeventoferror
}
Your help is appreciated, thank you.