Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6873441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:04:07+00:00 2026-05-27T04:04:07+00:00

Im having a issue, I need to combine 2 scripts together. One of which

  • 0

Im having a issue, I need to combine 2 scripts together. One of which is a validation and the other is variables/ajax script. I tried but i cannot get it to work. I put it within the script under the area that checks if it has the needfilled element attached however it submits without executing the ajax call.

Script 1:

$(document).ready(function(){
 $("#loading").hide();

// Place ID's of all required fields here.
required = ["parentFirstName", "parentLastName", "parentEmailOne", "parentZip"];
// If using an ID other than #email or #error then replace it here
email = $("#parentEmailOne");

errornotice = $("#error");
// The text to show up within a field when it is incorrect
emptyerror = "Please fill out this field.";
emailerror = "Please enter a valid e-mail.";




$("#theform").submit(function(){    
    //Validate required fields


    for (i=0;i<required.length;i++) {
        var input = $('#'+required[i]);
        if ((input.val() == "") || (input.val() == emptyerror)) {
            input.addClass("needsfilled");
            input.val(emptyerror);
            errornotice.fadeIn(750);
        } else {
            input.removeClass("needsfilled");


        }
    }
    // Validate the e-mail.
    if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
        email.addClass("needsfilled");
        email.val(emailerror);
    }

    //if any inputs on the page have the class 'needsfilled' the form will not submit
    if ($(":input").hasClass("needsfilled")) {
        return false;
    } else {
        errornotice.hide(); 
        return true;
    }
});


// Clears any fields in the form when the user clicks on them
$(":input").focus(function(){       
   if ($(this).hasClass("needsfilled") ) {
        $(this).val("");
        $(this).removeClass("needsfilled");
   }


});
}); 

Script 2:

// Form Varables
var parentFirstNameVal = $("#parentFirstName").val();
var parentLastNameVal = $("#parentLastName").val();
var emailaddressVal = $("#parentEmailOne").val();
var parentPhoneVal = $("#parentPhone").val();
var parentAddressVal = $("#parentAddress").val();
var parentAddressContVal = $("#parentAddressCont").val();
var parentCityVal = $("#parentCity").val();
var parentStateVal = $("#parentState").val();
var parentZipVal = $("#parentZip").val();   
var parentListenVal = $("#parentListen").val();     
var codeVal = $("#code").val();     
var getUpdateVal = $("#getUpdate").val();       

            input.removeClass("needsfilled");
            $("#message-space").html('<br /><br /><span class="greenText">Connected to Facebook.</span><br />');
            $("#loading").show();


            var counter = 0,
                divs = $('#div1, #div2, #div3, #div4');

            function showDiv () {
                divs.hide() 
                    .filter(function (index) { return index == counter % 3; }) 
                    .show('fast'); 

                counter++;
            }; 

            showDiv();

            setInterval(function () {
                showDiv();
            }, 10 * 600); 

            alert(parentFirstNameVal);

            $.ajax({
                type: "POST",
                url: "includes/programs/updateEmailsTwo.php",
                data: "parentFirstName="+parentFirstNameVal+"&parentLastName="+parentLastNameVal+"&UserEmail="+emailaddressVal+"&parentPhone="+parentPhoneVal+"&parentAddress="+parentAddressVal+"&parentAddressCont="+parentAddressContVal+"&parentCity="+parentCityVal+"&parentState="+parentStateVal+"&parentZip="+parentZipVal+"&parentListen="+parentListenVal+"&code="+codeVal+"&getUpdate="+getUpdateVal+"&ref=<?php echo $_SESSION["refid"]; ?>",
                success: function(data){
                $("#message-space").html('<br /><br /><span class="greenText">Complete</span><br />');
                divs.hide() 

        }
     });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:04:08+00:00Added an answer on May 27, 2026 at 4:04 am

    In addition to the suggestions that @JeffWilbert gave, I am going to follow it up with some more suggestions to make your code a bit more cleaner and efficient.

    First, just like you did in script 1, where you have an array of field names, you can do the same for script 2. Below is an example of what you can do make your code a bit more readable.

    var fields = ['parentFirstName', 'parentLastName', 'parentEmailOne', 'parentPhone'];
    var fieldsValue = [], dataString;
    
    for(i = 0; i < fields.length; i++){
         fieldsValue.push(fields[i] + "Val=" + $('#' + fields[i]).val());
    }
    
    dataString = fieldsValue.join("&");
    

    Second, If Script 2 is not dependent on any variable declared from Script 1, I would convert Script 2 into its own function and call it from Script 1. I think adding all that code inside the else like Jeff suggested is not best.

    function Script2(){
         //Script 2 Code
    }
    
    $("#theform").submit(function(){   
         //Call Script 2
    });
    

    And Third, If you are going to submit the form via AJAX and not through its default method, I would recommend using .preventDefault and then handle the flow of the submission inside the event handler function.

    $("#theform").submit(function(e){   
         e.preventDefault();
         //rest of your code here.
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having an issue here that I have tried everything I can think of but
I am having an issue where I need to be able to specify the
I need to get 2 summed figures however im having issues as one will
I'm having an issue with a Flash/Flex erroring in Firefox but not IE. I
Having some issue with Threadpooling here that I need some help with please. I
I'm having a JavaScript issue where I need a function to run and complete
I'm having an issue where I need to get the sum of a group
I’m having an issue where I need to be able to change a series
I am having an issue where I need to be able to delete multiple
I am having an issue crafting a good query for what I need. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.