![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
Join Date: Nov 2005
Posts: 149
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Jul 2005
Location: Athens,Greece
Posts: 39
Rep Power: 0
![]() |
i don't know about javascript, but for sure u can use servlets
|
|
|
|
|
|
#3 | ||
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
Quote:
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. |
||
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Nov 2005
Posts: 122
Rep Power: 3
![]() |
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"'}
};alert(products.product_1.name); alert(products.product_2.desc); //etc.
__________________
"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" |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Nov 2005
Posts: 149
Rep Power: 3
![]() |
Okay, thanks. I'm looking into it, and it sounds like exactly what I need =)
|
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#8 | |
|
Hobbyist Programmer
Join Date: Nov 2005
Posts: 122
Rep Power: 3
![]() |
Quote:
![]()
__________________
"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" |
|
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Nov 2005
Posts: 149
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Nov 2005
Posts: 122
Rep Power: 3
![]() |
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" |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|