...

View Full Version : Resolved script organizing question



Alith7
07-17-2009, 03:18 PM
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!

Philip M
07-17-2009, 04:11 PM
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.

Alith7
07-17-2009, 04:22 PM
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.

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!

Philip M
07-17-2009, 04:30 PM
Looks OK to me. :)

Alith7
07-17-2009, 04:34 PM
Thanks!! :D

abduraooft
07-17-2009, 04:41 PM
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.

Alith7
07-17-2009, 04:46 PM
...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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum