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

  • SEARCH
  • Home
  • 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 8377653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:43:39+00:00 2026-06-09T15:43:39+00:00

Good evening everyone, I am currently trying to cleanup my inline JS and break

  • 0

Good evening everyone,

I am currently trying to cleanup my inline JS and break it up into its own .js file, as well as breaking up some of my code into functions. Below I provide my code, I have an HTML file with an empty div named #main. On document ready I want to call my firstLoad function. It simply calls $("#main").load("login.php"); Seems simple enough, however my next step is that on submit of the form I want to serialize the submitted data, turn to string and submit via post. This for some reason will work if I hard code the form into the index.php file, but not if I use .load to fill in #main. I can’t figure out why this is, I am sure it’s simple if someone could explain it little that would be wonderful. My code follows:

UPDATE
After more searching I came across this thread that says the following:

As it turns out, the jquery .load() function is working flawlessly,
and I’m approaching this wrong.

Once the .load() function completes successfully, it calls any
“callback” function included by the programmer, just like any other
jquery function that accepts a callback as one of its “arguments”. The
.load() function is complete once it either errors or successfully
begins the HTML replacement
and loading of new content
, but that is
IT! The content will then take however long it takes to load, but your
.load call is already complete before that
. Therefore, expecting the
callback to run after the .load content has loaded will only
disappoint you. 😉

I hope others can learn from this just as I did, including those who
thought what I thought was the case. Proof: as stated in the jquery
ajax .load page, the callback is executed when the request completes,
not when the load completes. Eureka. Whoops.

Which leads to the follow up question, how can I manipulate the form after the load content has been added to the DOM? This is surely a simple fix, but I am new to AJAX and could use a nudge in the right direction. I notice adding a document(ready) within the login.php script works properly as it is added with the html, but it doesn’t seem like the cleanest way of doing things, as I’m trying to keep out inline JS. Any more advice?

/UPDATE

PHP/HTML

index.php

<?php
    session_start();
    $sessionNumber = session_id();
?>
<!doctype html>
<!-- Conditional comment for mobile ie7 blogs.msdn.com/b/iemobile/ -->
<!--[if IEMobile 7 ]>    <html class="no-js iem7" lang="en"> <![endif]-->
<!--[if (gt IEMobile 7)|!(IEMobile)]><!--> <html class="no-js" lang="en"> <!--<![endif]-->

<head>
  <meta charset="utf-8">

  <title>MyMobile</title>
  <meta name="description" content="">

  <meta name="HandheldFriendly" content="True">
  <meta name="MobileOptimized" content="320">
  <meta name="viewport" content="width=device-width">

  <link rel="apple-touch-icon-precomposed" sizes="114x114" href="img/h/apple-touch-icon.png">
  <link rel="apple-touch-icon-precomposed" sizes="72x72" href="img/m/apple-touch-icon.png">
  <link rel="apple-touch-icon-precomposed" href="img/l/apple-touch-icon-precomposed.png">
  <link rel="shortcut icon" href="img/l/apple-touch-icon.png">

  <meta http-equiv="cleartype" content="on">

  <link rel="stylesheet" href="css/style.css">

  <script src="js/libs/modernizr-2.0.6.min.js"></script>
</head>

<body>

  <div id="container">
    <header id="header">
        <img alt="Logo" src="img/logo.png" />
        <div id="blackHead">Please sign-in to continue</div>
    </header>
    <div id="main" role="main">

    </div>

    <footer id="footer">
        <div id="greyFoot">
            &copy; 2012 ACME<br />
            <pre id="result"></pre>
        </div>
    </footer>
  </div> <!--! end of #container -->


  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  <script>window.jQuery || document.write('<script src="js/libs/jquery-1.7.1.min.js"><\/script>')</script>
  <script type="text/javascript" src="js/firstLoad.js"></script>

</body>
</html>

login.php

<?php session_start();
    $sessionNumber = session_id();
?>


<!-- begin login form -->
<?php if(isset($_SESSION['sessionemail'])){ ?>
    <a href="logout.php" id="logout">Logout</a>
<?php }else { ?>

<form id="logForm" name="login" method="post" action="#">
    <label for="sessionemail">Email</label><br />
    <input id="sessionemail" type="email" name="sessionemail" autofocus="autofocus" autocapitalize="off" maxlength="150" required="true" value="" class="inputText" /><br />
    <label for="password">Password</label>
    <input id="password" type="password" name="password" required="true" value="" class="inputText" /><br />
    <br />
    <input type="hidden" name="sessionid" id="sessionid" value="<?php echo $sessionNumber; ?>" />
    <input type="hidden" name="subtocall" id="subtocall" value="g2.web.login.sub" />
    <input type="submit" value="Sign-In" name="submit" class="submitBox" />

</form><!-- end login form -->
<?php } ?>

And finally, my JS/Jquery

firstLoad.js

//function serializes our object

(function($){
    $.fn.serializeObject = function()
    {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            if (o[this.name] !== undefined) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };
})(jQuery);


//firstLoad function runs on document ready
//it loads the login form into the main div and slides
//the container down
(function($){

    $.fn.firstLoad = function(){
        return this.each(function() {
            $("#container").slideUp("slow", function(){
                $("#main").load('./login.php', function(){
                    $("#container").slideDown("slow", function(){
                    });
                });
            });
        });
    };
})(jQuery);

//logParse takes the loginResponse from the server
//changes from string to object, runs a check for authentication then
//manipulates the object dropping excess keys and adding new relevant ones for
//the intial to do list call
(function($){
    $.fn.logParse = function(xml){
        return this.each(function() {
            //parse the JSON login check string from the XML response
            var loginResponse = $(xml).find('string').text();
            //setup isBad variable for error check
            var isBad = false;
            //convert to JSON object from parsed string data
            loginResponse = $.parseJSON(loginResponse);
            //check if the sessionID is correct and user authenticated properly
            if((loginResponse.SESSIONID != "<?php echo $sessionNumber; ?>") || (loginResponse.ISAUTHENTICATED == 0)){isBad = true;}
            //if error flag is raised alert and bounce back to login
            if(isBad){
                alert("Unauthorized connection, please try again.");
            }
            //if there are no errors
            else{
                alert("so far so good!");
                //set up a new JSON object for to do list passback
                //and import the values from the lognResponse object
                //var todoPost =
            }
        });
    };
})(jQuery);

$(document).ready(function(){
    //hide screen for slidedown
     //$("#container").addClass("noShow");
    //allow cross domain scripts
    $.support.cors = true;
    //call firstLoad function to slide in the login prompt
    $("#main").firstLoad(function(){


        //create JSON object to store form input for AJAX POST
        //create submit listener
        $("#logForm").submit(function(){

            alert("inside submit");
            //parse form into formObj for data passing and manipulation
            var formObj = JSON.stringify($("form").serializeObject());
            //output initial formObj into result pane
            $("#result").text(formObj);
            $("#main").text("submitted: " + formObj);

            //AJAX POST call
            $.ajax({
                //type of communication
                type: "POST",
                //action for form
                url: "http://mydomain.com/JSONService.asmx/G2WebRequest",
                //data to be passed
                data: "request="+ formObj,
                //type of data passed in
                contentType: "application/x-www-form-urlencoded; charset=utf-8",
                //enable cross domain
                crossDomain: "true",
                //data type returned from webservice
                dataType: "xml",

                //if login POST was successful
                success: function(xml){
                    alert("post successful");
                    $.logParse(xml);

                },
                //if login POST failed
                error: function(XMLHttpRequest, textStatus, errorThrown){
                    alert(errorThrown);
                }




            });
        });
    });
});
  • 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-06-09T15:43:40+00:00Added an answer on June 9, 2026 at 3:43 pm

    JQuery.on() solved this issue. Took me a while to figure that out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good evening guys. I'm currently trying to get started on development of a project
Good evening everyone, I am using a JavaScript to load/override content from an HTML-File
Good evening everyone. I am currently using MVC 3 and I have a viewmodel
Good evening, here is what I am trying to achieve, I currently have a
Good evening, people! I'm trying to solve a rather simple problem, but.. well, it
Good evening everyone, before explaining my problem, I should give you some explanation on
Good evening. Can You help me please with some batch file? I have an
Good evening guys, I'm currently trying to add the visualization functionality of d3 to
Good evening guys! I'm currently trying to put together a CloudFlare client for the
Good evening, everyone. I've now run into an odd warning, and for the life

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.