Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jun 2009
    Posts
    121
    Thanks
    22
    Thanked 3 Times in 3 Posts

    script organizing question

    I'm building this rather complex order entering system, and I need to build a bunch of different AJAX scripts to do essentially the same thing, just calling to different PHP scripts depending on what is changing. I'm new to this and I'm not sure on the best way to set this up.

    which is the better option?

    1. saving a separate .js file for each different script.

    2. or is there a way to combine them all and just have on .js file called on the page?

    Thank you for your help!
    Last edited by Alith7; 07-17-2009 at 04:34 PM.
    I'll try again to find, the thing that was my mind...
    The only other sign, it says I've lost my mind.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    You can combine all the functions into one single .js file. There is no virtue in having multiple .js files.

    Remember that there must be no duplicaton of function or variable names.


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

  • #3
    Regular Coder
    Join Date
    Jun 2009
    Posts
    121
    Thanks
    22
    Thanked 3 Times in 3 Posts
    ok, here's the script that I have right now. The two parts highlighted in RED are what will change, the PHP file just contains the MySQL query and the formatting for the results. The ID is attached to the DIV tag that will have the replaced data.
    Code:
    var xmlhttp;
    
    function updateStock(str)
    {
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
    	{
    	alert ("Browser does not support HTTP Request");
    	return;
    	}
    var url="/scripts/getstock.php";
    url=url+"?q="+str;
    url=url+"&sid="+Math.random();
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }
    
    function stateChanged()
    {
    if (xmlhttp.readyState==4)
    {
    document.getElementById("getWeight").innerHTML=xmlhttp.responseText;
    }
    }
    
    function GetXmlHttpObject()
    {
    if (window.XMLHttpRequest)
    	{
    	// code for IE7+, Firefox, Chrome, Opera, Safari
    	return new XMLHttpRequest();
    	}
    if (window.ActiveXObject)
    	{
    	// code for IE6, IE5
    	return new ActiveXObject("Microsoft.XMLHTTP");
    	}
    return null;
    }
    I'm assuming that what I need to have unique between the two functions is what I changed to the GREEN text. the stateChanged function being called in the updateStock function. both containing unique information. So, as long as the Green and red items are unique, I can just repeat the two functions in the same script.

    Is that right? sorry to be a pain, but I think I jumped in a bit over my head and I'm trying to do this all correctly so that I can easily change things down the road.

    Thank you!
    I'll try again to find, the thing that was my mind...
    The only other sign, it says I've lost my mind.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Looks OK to me.

  • Users who have thanked Philip M for this post:

    Alith7 (07-17-2009)

  • #5
    Regular Coder
    Join Date
    Jun 2009
    Posts
    121
    Thanks
    22
    Thanked 3 Times in 3 Posts
    Thanks!!
    I'll try again to find, the thing that was my mind...
    The only other sign, it says I've lost my mind.

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,851
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    You can combine all the functions into one single .js file. There is no virtue in having multiple .js files.
    ...and by reducing the number of linked files, you can reduce the the number of server requests.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    Alith7 (07-17-2009)

  • #7
    Regular Coder
    Join Date
    Jun 2009
    Posts
    121
    Thanks
    22
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by abduraooft View Post
    ...and by reducing the number of linked files, you can reduce the the number of server requests.
    that's what I thought, but I wasn't 100% sure on how to repeat the function without running into an issue.

    Thanks both of you for your help!!

    my next fun is going to be to figure out how to submit to ALL the relational databases at once. This may take a bit.
    I'll try again to find, the thing that was my mind...
    The only other sign, it says I've lost my mind.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •