Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 16th, 2006, 2:51 AM   #1
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 149
Rep Power: 3 Writlaus is on a distinguished road
javascript and sql?

I'm fluent in PHP, but not in javascript. Could anyone tell me if it's possible for a javascript function to get new information from an sql database? I need it to be able to get a new, random row from a table in the database.

If javascript cannot do it, is there some other method that could quickly get the information without going to another web page? Something that looks smooth.
Writlaus is offline   Reply With Quote
Old Mar 16th, 2006, 4:13 AM   #2
tsofras
Programmer
 
tsofras's Avatar
 
Join Date: Jul 2005
Location: Athens,Greece
Posts: 39
Rep Power: 0 tsofras is an unknown quantity at this point
i don't know about javascript, but for sure u can use servlets
tsofras is offline   Reply With Quote
Old Mar 16th, 2006, 5:01 AM   #3
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Writlaus
I'm fluent in PHP, but not in javascript. Could anyone tell me if it's possible for a javascript function to get new information from an sql database?
No. At least, not directly. Javascript is purely client side, and your database is server side. You need an intermediate way of passing information between the two.
Quote:
Originally Posted by Writlaus
If javascript cannot do it, is there some other method that could quickly get the information without going to another web page? Something that looks smooth.
You probably want AJAX. AJAX stands for Asynchronous JavaScript And XML, which sounds complicated, but the process involved is actually rather simple. In all major browsers, there's a javascript function called XmlHttpRequest (IE uses an ActiveX object). This function will fetch an XML page through HTTP without the need to refresh the current page. In this manner, one can request data from the server without reloading the page. Google maps and GMail use AJAX quite a lot.

There are hundreds of guides to AJAX online, and many libraries that will bridge the gap between AJAX and a server-side language like PHP. SAJAX is one such library. SAJAX allows you to get the output of a PHP function from Javascript, without needing to reload the page. In your case, you could have your PHP function return the results from the SQL query, and have Javascript update the page dynamically.
Arevos is offline   Reply With Quote
Old Mar 16th, 2006, 6:28 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
The XmlHttpRequest is rather new (and quite useful -- Arevos' mention of Google Maps is one of my favorite references, also). There are other ways, one of which is (was) the only really defensible use of frames, in my opinion. One may make a hidden frame and transact requests there. This precludes having to reload heavy pages. I would never use it with XmlHttpRequest available.

If you want DB information available to script and only need to provide it once, you can dynamically generate script variables and they become part of the page. Something like this, a 2D array (anottagonna test this, just fingertip activity).
[php]
<?php
$jsProducts = 'var products = new Array (';
...
...
// In a loop,
$jsProducts .= "\n".'new Array(';

$jsProducts .= '"'.$cart [$i]['ID'].'", ';
$jsProducts .= '"'.$cart [$i]['Item'].'", ';
$jsProducts .= '"'.$cart [$i]['Description'].'", ';
$jsProducts .= '"'.$cart [$i]['Color'].'", ';
$jsProducts .= '"'.$cart [$i]['Size'].'", ';
$jsProducts .= '"'.$cart [$i]['Qty'].'", ';
$jsProducts .= '"'.$cart [$i]['Price'].'", ';
$jsProducts .= '"'.$cart [$i]['Total'].'", ';
$jsProducts .= '"'.$cart [$i]['OnHand'].'", ';
$jsProducts .= '"'.$cart [$i]['OfferQty'].'"';
$jsProducts .= ')';
if($i < $rows-1) $jsProducts .= ', ';
...
...
// outside the loop
$jsProducts .= ');';
...
...
?>
<!-- HTML here...
...
...
<script....>
<?php echo $jsProducts;?>
...
</script>
...
[/php]
This is a One Time Thang, so to speak, but gives you client-side access to, say, a shopping cart. The cart will reflect the realities of inventory at the time the client hit the site. You can manipulate and maintain the cart client side without making repeated trips to the server.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 16th, 2006, 9:21 AM   #5
Agent 47
Hobbyist Programmer
 
Agent 47's Avatar
 
Join Date: Nov 2005
Posts: 122
Rep Power: 3 Agent 47 is on a distinguished road
Rather than using 2d arrays, it's much easier to use objects
in JavaScript to store the data - it means you can access it
easier. For example, a variation on DaWei's code as an
object in JS:
var products={
	product_1:
		{'id':'23', 'name':'foo', 'desc':'some info about "foo"'},
	product_2:
		{'id':'47', 'name':'bar', 'desc':'some more info about "bar"'}
};
You can then access the individual details using code like:
alert(products.product_1.name);
alert(products.product_2.desc);
//etc.
--47.
__________________
"I'm going to become rich and famous when I invent a device that allows you to stab people in the face over the internet"
Agent 47 is offline   Reply With Quote
Old Mar 16th, 2006, 3:09 PM   #6
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 149
Rep Power: 3 Writlaus is on a distinguished road
Okay, thanks. I'm looking into it, and it sounds like exactly what I need =)
Writlaus is offline   Reply With Quote
Old Mar 16th, 2006, 3:18 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Let me just tell you that I know Agent47 from other places and he's one of the most knowledgeable people I know in the discipline. I've learned more from him in a dozen posts than in a year of struggling to catch up with 5-6 years of hiatus from web design.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 16th, 2006, 3:41 PM   #8
Agent 47
Hobbyist Programmer
 
Agent 47's Avatar
 
Join Date: Nov 2005
Posts: 122
Rep Power: 3 Agent 47 is on a distinguished road
Quote:
Originally Posted by DaWei
Let me just tell you that I know Agent47 from other places and he's one of the most knowledgeable people I know in the discipline. I've learned more from him in a dozen posts than in a year of struggling to catch up with 5-6 years of hiatus from web design.
Hah, where's the :blushing: smiley...?
__________________
"I'm going to become rich and famous when I invent a device that allows you to stab people in the face over the internet"
Agent 47 is offline   Reply With Quote
Old Mar 17th, 2006, 11:35 PM   #9
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 149
Rep Power: 3 Writlaus is on a distinguished road
I'm now able to get information into the page without refreshing it; thank you very much =D

Now I'm wondering if there is some way to effect the database table in a similar way? I know I'd have to have the page request another php page that does the work, but I want that second mentioned page to be unavailable if the user were to just type in the second page's address.
Writlaus is offline   Reply With Quote
Old Mar 18th, 2006, 2:53 AM   #10
Agent 47
Hobbyist Programmer
 
Agent 47's Avatar
 
Join Date: Nov 2005
Posts: 122
Rep Power: 3 Agent 47 is on a distinguished road
Erm, no, I can't think of any way you could do that. If you can
load the page in a hidden frame to do the work, you can load it
in a normal window.

You could perhaps look into setting a session variable when you
load the main page, and check for the existance of that var in
the page that loads in the frame, but I'm not sure how reliable
that would be...
__________________
"I'm going to become rich and famous when I invent a device that allows you to stab people in the face over the internet"
Agent 47 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:50 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC