Pass non-mandatory fields onsubmit even if empty (or tabs or blank spaces)

Hi.
I would like to passwith the help of javascript empty non-mandatory fields to my perl script (and replace them with a custom value). The form mailer script only passes the mandatory fields. When non mandatory fields are filled , these are added to the list of passed field values, otherwise the list includes only the mandatory ones.
I’d like to pass all values even the empty ones so that the person receiving the submitted data via email can easily export them to a database via a macro written by somebody else.
Another requirement from client: The predefined values must not show up in the form fields when loading the page with the form.

How can I check for empty values as well as for tabs or blank spaces and replace each of these fields with a value = “not available”?

I tried to do solve this for empty fields (no tabs, no blank spaces) but my solution does not work properly. It requires me to click the submit button 3 times before the form is submitted (because with every click the “not available” value is added in each empty field.

I have this function in my validation script:


      function isFieldBlank(field)
      {
        if(field.value == "")
        {
            field.value = "not available" ;
            return false;
        }
        else 
        {
          return true;
        }
      }

and then in the head of the page


function validform (myform) {
.... validation code .....    
           if(!(isFieldBlank(myform.value1)) return false;    
           if(!(isFieldBlank(myform.value2))) return false;    
           if(!(isFieldBlank(myform.value3))) return false;  
}  
 

and in the form tag:


<form action="/cgi-bin/formmailer.pl" method="post" onsubmit="return validform(this);">

What am I doing wrong? I understand i must be using a loop but I can’t get it to work. :rolleyes:

How to extend it to tabs and blank spaces?

Thanks in advance for the help!!!

You can do all of this without scripting.

Use a hidden field before an optional field of the same name, where the hidden field contains the default value when the text field is not filled in.


<input type="hidden" name="cheesepreference" value="none">
<input type="text" name="cheesepreference" value="">

When the text field is not filled out, the form will submit “none”
When the text field is filled out, the form will submit the filled out value.

Using javascript for this task only complicates the process.

Hi pmw57, thank you for the help!
Works fine. The only problem is that when the user fills out the text field -say with the value= “value1”- the form will submit:
“none value1”
Oh well, I guess this is the little price to pay for this simple yet brilliant solution.
Thanks again!

The browser will still send both key/value pairs for elements which have the same name. The server which receives the form submission may need additional logic to filter out the duplicates. Some serverside programming languages do it automatically(only the last value received per name is used). Your perl script obviously preserves the values of the duplicate names.

You could use javascript to do this, but you will have some users with js disabled, in which case you will get the duplicates anyway. Really, this issue is best solved in your perl script.

crmalibu: yes, the perl script I am using sends both values, hidden and visible. But how can I remove the duplicate value in the perl script?

How to handle in the same way empty textareas and radio buttons (for example radio buttons that show up later in the form only if a certain mandatory field has been selected? Using CSS?

Thanks!

pmw57: unfortunately the method you proposed does not work if you have the following scenario:
depending on the value of a mandatory radio button there is a whole bunch of fields that can be hidden or shown. And if these are shown, they become mandatory (they are validated via javascript). I can’t apply the solution of the hidden fields, because otherwise validation says that these value are filled.