Link to home
Start Free TrialLog in
Avatar of cappa74
cappa74

asked on

Launch pop up when successful submission

Hi,

Below is a great script from TallerMike and I hoping to use it on a feedback questionnaire I'm developing, I would like to know if and how it would be possible to call another script from within your script.  I have  a script that produces a pop up screen thanking the user for their submission, however I'm unsure how and where to call this script, I have only been able to call this script successfully whenever the submit button is pressed but this is of no use because it also calls it if there are empty fields, I only want it to call the script when all of the fields are completed.  Also I presume it will be possible to call another script when there are blank fields, to produce a pop up telling the user to fill in the missing fields.

I would appreciate any help thanks
Tony.


*** Script ***

function isRadio(inputObj)
    { return ( (inputObj.type == "radio") || (inputObj.type == "checkbox") ); }

function getRadioParent(inputObj)
    { return eval(inputObj.form.name + "." + inputObj.name); }

function highlight(inputObj,highlightColor)
    {
    if ( isRadio(inputObj) )
         {
         inputObj = getRadioParent(inputObj);
         if (inputObj.length)
              for (var optionIndex=0; optionIndex < inputObj.length; optionIndex++)
                   { inputObj[optionIndex].style.backgroundColor = highlightColor; }
         else
              { inputObj.style.backgroundColor = highlightColor; }
         }
    else
         { inputObj.style.backgroundColor = highlightColor; }
    }

function isRequired(inputObj)
    { return ( (typeof(inputObj.required) != 'undefined') && (inputObj.required != "false") ); }

function hasValue(inputObj)
    {
    obj_type = inputObj.type.toUpperCase();
   
    if (obj_type == "TEXT" || obj_type == "PASSWORD")
         { return (inputObj.value.length != 0); }
         
       else if (obj_type == "SELECT-ONE")
            { return (inputObj.selectedIndex > 0); }
         
       else if (obj_type == "SELECT-MULTIPLE")
            {
       for (var optionIndex=0; optionIndex < inputObj.length; optionIndex++)
              if (inputObj.options[optionIndex].selected)
                   { return true; }
           return false;
         }
         
    else if (obj_type == "RADIO" || obj_type == "CHECKBOX")
         {
         inputObj = getRadioParent(inputObj);
         if (inputObj.length)
              {
            for (var optionIndex=0; optionIndex < inputObj.length; optionIndex++)
                 if (inputObj[optionIndex].checked)
                        { return true; }
                return false;
              }
         else { return inputObj.checked; }
         }
    }
         
function check_form(formName)
    {
    var return_boolean = true;
    var processedFields = new Array();
   
    for(var elementIndex=0; elementIndex<formName.length; elementIndex++)
         {
         inputObj = formName.elements[elementIndex];
         if ( !(processedFields[inputObj.name] > 0) )
              {
              processedFields[inputObj.name] = 1;
              if (isRequired(inputObj) && !(hasValue(inputObj)))
                   {
                   return_boolean = false;
                   highlight(inputObj,"red");
                   }
              else
                   { highlight(inputObj,"white"); }
              }
         }
    return return_boolean;
    }

*** Script ***

Cheers
Avatar of TallerMike
TallerMike

How are you calling the script from your form? I think the original intention of the script was to script was to be called like so:

<form onsubmit="return check_form(this)">

This would then highlight any required fields that weren't entered that were required. I'm assuming that you've got this part working correct? And what you want to do is if there are fields that the user DIDN'T enter values in, you want to put a popup that will alert them to fill in those fields. Otherwise, if they filled in everything that's required, you want to do a popup that thanks them for submitting the form. Correct?
Avatar of cappa74

ASKER

Thats spot on.

Tony
ASKER CERTIFIED SOLUTION
Avatar of TallerMike
TallerMike

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cappa74

ASKER

God why didn’t I think of that!!  It nearly works 100% the only problem is that if the user doesn’t enter a value for a required field the error alert is output ok but when the user clicks ok it still goes to the server but I have made a few minor changes so it now stops and waits for the user to input the missing values.

function Submit_Form(FormName)
{var return_boolean = true;
     if (check_form(FormName))
     {
     alert("Thanks for submitting!");
     openindex()
     return_boolean = true;
     }
     else
     {
     alert("Please fill in all required fields");
     return_boolean = false;
     }
    return return_boolean;
}

openindex is a function that creates a html pop up.

Thanks for your assistance Mike.

Tony
Avatar of cappa74

ASKER

Thanks again
no problem, glad I could help.