Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Software Development
Tag Cloud
access acer asus batch bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming gpu hard drive hardware hdmi internet laptop malware memory modem monitor motherboard network printer problem ram registry router slow software sound trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
Javascript function that submits to database?

Reply  
Thread Tools
BlackFrancis's Avatar
Computer Specs
Member with 147 posts.
 
Join Date: Oct 2006
Experience: Intermediate
25-May-2008, 04:32 PM #1
Question Javascript function that submits to database?
Hi. I'm looking for some help. I think the code for this capability is Javascript. I'm only coming out of my noob-ness lately with codes. So bare with me...

I'm using a CMS called Pligg. It's hard to orientate people towards a new site. So I thought I'd just add a function to my current (Conduit.com) toolbar.
I'd like my users to be able to click a button, and it submit the URL of the current page to my Pligg database.

OnlyWire do this and StumbleUpon. Digg etc do it within the page content too. And I run a forum, so I know well software can submit forms lol So I don't see any reason why I shouldn't be able to do it too!

If someone could teach me how to do this or make this object for me. I'm rich now. I can afford to compensate with Euro :P

Please help. The Internet is about dreams. I want to build mine

Jackiefrost9's Avatar
Computer Specs
Senior Member with 3,799 posts.
 
Join Date: Jun 2004
Location: Spokane, Wa
Experience: Advanced
25-May-2008, 06:04 PM #2
This is very easy, provided you already have a server side script to post to. You can use Ajax (asyncronous javascrip) to do this and it will work very well because the user doesn't experience any delay while you're posting the data. Without using prototype, it would look like this:

Code:
var ajax = new XMLHttpRequest(); 
ajax.onreadystatechange = function() { 
                       if (ajax.readyState == 4) { 
                                if (ajax.status == 200) { // 200 means request succeeded 
                                    do something with ajax.responseText; 
                                } else { 
                                    code to handle the error; 
                            } } }; 
ajax.open("GET", url, true); 
ajax.send(null);
As you can see, that's ugly. Using prototype's ajax function is much easier and better looking:
Code:
new Ajax.Request( "url", { method: post, onSuccess: function, parameters: { list, your, params, here}, onFailure: function } );

All of the options are opional. I think it defaults to method: get.
In order to use prototype's function you have to link to the prototype framework on your page:
http://www.prototypejs.org/
I would download it, host it on your site, then link to it in the website. I'm not sure how you would do it with a toolbar, unfortunatly. That's why I listed the messy code. One advantage with using prototype is that is takes care of almost all of the incompatabilities between browsers. So yeah, that's the basic format.
__________________
M.O.U.S.

I do things with hardware n stuff

I'm at the University of Washington
Computer Engineering
BlackFrancis's Avatar
Computer Specs
Member with 147 posts.
 
Join Date: Oct 2006
Experience: Intermediate
25-May-2008, 06:46 PM #3
Ok. I'm sorry. Most of this language goes right over my head.
Can you recommend any reading on this?

EDIT:

I got the javascript I dragged onto my FF toolbar from OnlyWire.

Code:
javascript:location.href='http://www.onlywire.com/b/bmnoframe?u='+encodeURIComponent(location.href)+'&t='+encodeURIComponent(document.title);
Save Page (std)
If you could teach me what this structure is saying, and give me some idea of what that file it refers to is doing, that'll open my eyes about this a bit more.

Last edited by BlackFrancis; 25-May-2008 at 06:50 PM.. Reason: New info
Jackiefrost9's Avatar
Computer Specs
Senior Member with 3,799 posts.
 
Join Date: Jun 2004
Location: Spokane, Wa
Experience: Advanced
25-May-2008, 08:30 PM #4
All that is doing it looks like is making a request to some server program. Let me break it up for you (// is how you comment in javascript. I'll use // to explain the parts):

javascript: // I think this is just telling it that it is javascript. I'm not sure what language the code you gave me is in.

location.href= // This is saying: "The following is the URL of my file"

'http://www.onlywire.com/b/bmnoframe?u=' // This looks like the begining of the URL

+encodeURIComponent(location.href)+ // The + designates adding variables to strings. The URL here is essentially a string, then they close the string with ' and add the plus in order to insert a variable into the middle of the string.

'&t=' //This is once again part of the string

+encodeURIComponent(document.title) //This is another variable just like the first one. Since it has no plus sign after it we know it's at the end of the string.

; // You use semi-colons to end almost every line in javascript.

The URL in that piece of code is passing two parameters with names u and t. bmnoframe is the name of the server side program that is going to accept the parameters u and t and then do something with them. The question mark after the name (bmnoframe?) designates that you are about to pass some variables (in this case u and t). If you wish to pass more than one variable, you seperate them with a &. For instance, if you were calling a program called example.php and passing it variables foo and bar it would look like this: www.mysite.com/example.php?foo=blah&bar=bleh. I don't know what the variables do. encodeURIComponent(document.title) and encodeURIComponent(location.href) are probably other functions in whatever javascript you have, but i'm not sure.

I don't know what the save page at the end is doing.
__________________
M.O.U.S.

I do things with hardware n stuff

I'm at the University of Washington
Computer Engineering
BlackFrancis's Avatar
Computer Specs
Member with 147 posts.
 
Join Date: Oct 2006
Experience: Intermediate
25-May-2008, 09:14 PM #5
Fascinating. Thanks for clearing that up. I think I might just have fun with this

May I refer to you if I get stuck somewhere?
Jackiefrost9's Avatar
Computer Specs
Senior Member with 3,799 posts.
 
Join Date: Jun 2004
Location: Spokane, Wa
Experience: Advanced
26-May-2008, 06:29 PM #6
Yeah sure, just post to this thread or send me a PM and I will try to help as best I can.
Reply

Tags
bot, javascript, stumbleupon

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 01:17 AM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.