Click to See Complete Forum and Search --> : Form verification and creating a hidden field on submit


silencer01
11-17-2003, 07:10 PM
Hi All,

I have a form that has a number of radio boxes and input fields for the user to select from. The form is part of a survey. The problem is that the form relies on a form field being populated once submitted. If the user decides that they don't want to answer the question (which is a valid option for the survey). It will cause a error when updating the database later in the process.

What i need the form to do is check to see if the user has input any information into the form field if not populate a hidden field.

It would be something like this:

<form name="form" method="post" action="question.cfm" onsubmit="return checkForm(this)">

<input type="radio" name="q1" value="yes">
<input type="radio" name="q1" value="No">
<textarea name="q1" cols="75" rows="5" wrap="virtual"></textarea>

<input type="submit" name="Submit" value="submit">

</form>

<!--- if form has been answered go to question.cfm ---->
<!--- if form has not been answered add --->
<input type="hidden" name="q1" value"no response">
<!--- then go to question.cfm ---->

Thanks

Jake A.

Jona
11-17-2003, 07:22 PM
Originally posted by silencer01
I have a form that has a number of radio boxes and input fields for the user to select from. The form is part of a survey. The problem is that the form relies on a form field being populated once submitted. If the user decides that they don't want to answer the question (which is a valid option for the survey). It will cause a error when updating the database later in the process.

You cannot rely on JavaScript as ~13% of the users on the Web do not have it enabled, not to mention many that the script may not work for. You should do all processing server side; and client-side processing should exclusively be used as "extra."

[J]ona

silencer01
11-17-2003, 07:31 PM
Thanks Jona,

I should have noted that the survey will be on an intranet therefore we can ensure everyone has javascript enable (i will look into a server side solutions though), but if anyone could help with a client side solution it would be appriecated.

Thanks

Jona
11-17-2003, 07:48 PM
<script type="text/javascript"><!--
/* Untested code */
function checkForm(f){
&nbsp; for(i=0; i<f.elements.length; i++){
&nbsp; &nbsp;if(f.elements[i].type == "checkbox" || f.elements[i].type == "radio"){
&nbsp; &nbsp; &nbsp;if(f.elements[i].checked != true){
&nbsp; &nbsp; &nbsp; &nbsp;addField(f);
&nbsp; &nbsp; &nbsp; &nbsp;return false;
&nbsp; &nbsp; }
&nbsp; &nbsp;} else {
&nbsp; &nbsp; &nbsp;if(f.elements[i].value == "" || f.elements[i].value == " "){
&nbsp; &nbsp; &nbsp; addField(f);
&nbsp; &nbsp; &nbsp; return false;
&nbsp; &nbsp;}
&nbsp; }
return true;
}

function addField(f){
var obj = document.createElement("INPUT");
obj.setAttribute("TYPE","HIDDEN");
obj.setAttribute("VALUE","no response");
obj.setAttribute("NAME","q1");
f.appendChild(obj);
}
//--></script>


[J]ona