Page 1 of 1
Javascript Coffee with PHP Donuts...
Posted: Mon Jun 27, 2005 5:34 am
by harrisonad
I have read some articles encouraging the use of Javascript together with PHP.
One of which is by reading MySQL data by PHP and store it to javascript variables. In this way, we could use the capability of javscript to instantly manipulate HTML forms, such as 'auto-complete' functions.
Code: Select all
echo "
<SCRIPT language='javascript'>
var AllItems = new Array();";
$query = "SELECT ItemCode,ItemName,CatergoryID,SellingPrice FROM tblItem";
$result = mysql_query($query);
while($data = mysql_fetch_array($result)){
echo "
var Item = new Array();
Item['item'] = $data[ItemName];
Item['price'] = $data[SellingPrice];
Item['catid'] = $data[CategoryID];
AllItems[$data[ItemCode]] = Item;
";
}
echo "
function AutoComplete(){
// will use AllItems array
}
function RecordNotExists(){
// will use AllItems array
}
function CalculatePrice(){
// will use AllItems array
}
</SCRIPT>
";
I have tried this and it works. But it has some drawbacks, such as insecurity and slow performance when dealing with large data.
Is it advisable to use? What is you views about this technique?
Any answer is acceptable.
Posted: Mon Jun 27, 2005 5:40 am
by Syranide
If it is going to reach any scale then it would without doubt be troublesome as having 1000 entries, a hundred bytes each starts eating alot of bandwidth, also, could be troublesome making the javascript do it quickly...
And really, is it necessary? I've browsed and bought many things over the net, and never has I cared about such things, however, people are different, but according to me it would be mostly cosmetic.
A thing I did in an experiment was using javascript, and making it update an iframe after 1 second (from the last keypress). Worked surprisingly good, perhaps not the perfect technique, but could very likely be tuned to make it nice. (that Iframe would perhaps be just below the textbox and 3 rows high, containing the 3 most suitable items (or so)) (which would allow you to show more indepth information about them or make them clickable too)
Posted: Mon Jun 27, 2005 5:44 am
by harrisonad
Posted: Mon Jun 27, 2005 6:33 am
by Chris Corbyn
You might want to look into AJAX or XMLhttp... you'll just transmit what you need that way rather than getting the full whack...
Check out the tutorials board on this forum... the last thread by Burrito covers XMLhttp... (You can basically communicate with the server through javaScript without leaving the current page)

Posted: Mon Jun 27, 2005 6:39 am
by phpScott
I 2 have done similar things but general I have a hidden Iframe that takes care of sending and recieving data from the server, much like Burrito's tutorial
viewtopic.php?t=34665
It you just have to be careful that there is an alternative way to view you pages if people have js turned off.
In your case if you page load times are slow maybe only load the first 25 of so records then use a hidden iframe to retrieve the rest then interact with it.
I think it makes pages look more like a desktop app without the page refreshing all the time but there can be draw backs.
Posted: Mon Jun 27, 2005 9:48 am
by CoderGoblin
So much depends on how much data is "initially loaded" and how often you would have to refresh/fetch component parts if things change? If the refresh traffic times the average number of "refreshes" is likely to be higher you would be far better to just load all the information in one go. This could potentially reduce bandwidth in the long run. AJAX/HttpRequest is a very useful technology (javascript normally required), and one I am likely to use in the near future, but you need to consider it's possible overhead and disadvantages, they do exist and you need to investigate rather than just jumping on a bandwagon
Iframes work well up to a point but with tabbed browsing make sure links do not assume the frame already exists.
As far as javascript, use it to enhance your site, not depend on it (unfortunately that means non-javascript users often need to refresh all of the page). Most webstats about the disabling of javascript (i've seen figure ranging from 0.5% to 15%) do not take into account that once a non-javascript user has left dur to lack of functionality, he will not come back.

I'll shut up now as I'm starting to ramble...
Posted: Wed Jun 29, 2005 8:41 pm
by harrisonad
Thanks for all you replies. And i am now begining to learn XML and how to read it by javascript.
One more question though:
Will I prepare the XML file for each form before running the application, or populate a single XML file it at runtime?