I have a little java script snippet that will check to see if someone has entered data into the required fields. You put the following inside the form statement and include the script in the header of the page.
onSubmit=\"return checkrequired(this)\"
This is the req.js script
function checkrequired(which) {
var pass=true;
for (i=0;i<which.length;i++) {
var tempobj=which.elements[i];
if (tempobj.name.substring(0,8)=="required") {
if (((tempobj.type=="text"||tempobj.type=="textarea")&&
tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
tempobj.selectedIndex==0)) {
pass=false;
break;
}
}
}
if (!pass) {
shortFieldName=tempobj.name.substring(8,30).toUpperCase();
alert("The "+shortFieldName+" field is a required field.");
return false;
} else {
return true;
}
}
So this works great on regular forms but not at all on ajax forms. I have a page where I have ajax submit some simple preliminary bits to identify a person then it loads a regular form using that info. On the resulting form I have the bit mentioned above in the form statement and the req.js script is included in the header of the original page yet it won't work. Here is the form that is populated by the ajax request (middle bits of the form removed for brevity)
echo "<form onSubmit=\"return checkrequired(this)\" name=\"contract\" enctype=\"multipart/form-data\" action=\"".$_SERVER['PHP_SELF']."?p=u&sid=".$_GET['sid']."\" method=\"post\">";
echo "<br><div class=cont1>".cross_field("showinfo","id","agree","1",$_GET['sid'])."</div>\n";
echo "<br><div class=cont1><div class=ckbx><input type=\"checkbox\" name=\"requiredAgree\" value=\"y\">I Agree</div>\n";
echo "<div class=Clear></div>";
echo "<input type=\"submit\" name=\"submit\" value=\"Submit Contract\" />\n";
echo "<h4 class=org>After Submitting the Contract you will be taken to the page to upload photos and pay the application fee.</h4>\n</div>\n";
echo "</form>\n";
The "I Agree" Checkbox is the required field notice the name 'requiredAgree' the required bit is what is supposed to trigger the script to check that this field has been filled in or not and if not it pops up a message "The Agree Field is Required". Only it doesn't work when the form is generated as the result of an ajax request.
I reasoned perhaps the req.js would need to be included at the time the new form was generated and tried to insert it in a comment there, that didn't help.
Any Ideas why not or how to fix this either with other code or to make this code work? I need to be able to require people to enter certain bits of data on this form before going on to submit the form. This code has always worked but I'm not tied to it so any way to force them to enter data before submitting the form is acceptable. Other than that the forms are working beautifully.