/    Sign up×
Community /Pin to ProfileBookmark

Using JS to query databases

This is what I have;

A MySQL database filled with ‘members’, each with a ‘score’.

This is what I want;

Users who have their own website to be able to display their ‘score’ on their HTML pages (not just providing a link to their ‘profile’ as such).

How I thought this should be done;

A bit of JavaScript on the HTML page (ala “Hot Or Not”), which points to a file that queries the database and retrieves the information for that user.

Am I on the right track?

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@matt_carter_25Nov 25.2004 — Javascript works on the client side (works in the browser),

what you need is server side processing (prcessed before the page is sent), somthing like ASP or Colfusion which will query the database and return results to the client or take information from the page and insert, update, delete to the database, you cant do this with javascript.

Probable the easiest language to use is Macromedia Coldfusion, its really quick to get the results you need.

Hope this helps

Matt
Copy linkTweet thisAlerts:
@ramccallauthorNov 26.2004 — Could I use PHP to do the server side processing?

If I used Coldfusion or PHP to do the server side processing, how could I allow users to put code on their own page to retreive that data?
Copy linkTweet thisAlerts:
@7studNov 26.2004 — Since you said you had a mysql database, the natural choice for a server side language would be php--not asp. However, something is confused here--at least I'm confused, see---> ?

While javascript is client side, which means it executes on the user's browser, and php is server side, which means it executes on the server, the whole page still gets sent by the server. Any javascript that is going to alter the page already has to be in the page or the page has to link to an external .js file on the server before it gets sent to the client's browser. Technically, I think you can make a dynamic XMLHttpRequest back to the server for a .js file after the page has loaded on the client's browser, but that still doesn't mean a user can write javascript to alter the page you loaded into their browser.
Copy linkTweet thisAlerts:
@ramccallauthorNov 26.2004 — I don't want the client to be able to alter anything, I just want them to be able to display information pulled from the database. Here's an example;

A member 'John' has a score of '9'. As people come to the site and vote on John, his score might fluctuate higher or lower.

I want John, to be able to put a little bit of HTML code on his own personal website, that displays his current score. It would say something like "my score is currently 9!"

As more people vote for John, his score goes up to 9.5. Now, without altering the code he has on his website, I want it to dynamically change to "my score is currently 9.5!"

Catch my drift? I'm positive it's possible to do this, with the client putting something like this in his HTML code;

<script language="javascript" type="text/javascript"

src="http://www.whatever.com/something/?user=john"></script>

And then I'd have the .js on the server side... the only problem is I don't have the know-how to write the JavaScript to perform such actions. That's why I've come here! (:
Copy linkTweet thisAlerts:
@baconbuttyNov 26.2004 — If I am reading you correctly, John's "web site" is nothing to do with the main web site. It is his own site, hosted wherever.

What you are looking at is either:-

  • 1. Doing something like an "ad" in John's web page which makes a call to your web site. You may want to look at how ad's work in such case.


  • 2. Providing on your web site a simple ASP or PHP web page, which John could say point to in an IFRAME in his web page. I only know ASP, so it could be something like:-


  • <IFRAME src="http://www.databasesite.com/myscore.asp?Name=John"></IFRAME>

  • 3. If you want to access the value in script, using the HTTPRequest object suggested by 7stud to make a call to the "myscore.asp" page and return the result to the HTTPRequest object, for dynamic insertion in John's web page.


  • Does this sound like it?
    Copy linkTweet thisAlerts:
    @senshiNov 26.2004 — ** Ahem

    For example, you can create a document that allows the user to query, display, and modify the content of a large, server-based database by combining the data binding features of DHTML with a data source object (DSO).

    maybe not directly in javascript but you can if you look find a soloution...

    [URL=http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/dhtml.asp]http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/dhtml.asp[/URL]
    Copy linkTweet thisAlerts:
    @ramccallauthorNov 27.2004 — [i]Originally posted by baconbutty [/i]

    [B]If I am reading you correctly, John's "web site" is nothing to do with the main web site. It is his own site, hosted wherever.[/B][/QUOTE]


    Correct. I'll give the IFRAME thing a try, although using frames wasn't really the solution I was looking for.
    Copy linkTweet thisAlerts:
    @7studNov 27.2004 — I don't want the client to be able to alter anything...[/quote]

    Well, then you don't need to do anything.?
    Copy linkTweet thisAlerts:
    @ramccallauthorNov 28.2004 — But they need to be able to display information on their own site that is stored in a database on my server...
    Copy linkTweet thisAlerts:
    @7studNov 28.2004 — Ok, I think I have this straight now. This is typically what happens:

    1) A user requests a page from another person's server.

    2) The server sends the user a page.

    4) When the page gets to the user's browser the javascript executes.

    If I understand you correctly, what you want is to allow some other person to be able to instruct their server(where the pages for their website reside) to request information from your server, so they can insert the information into a page on their server, and then send the page to their clients' browsers. That information exchange, if possible at all, is on the server side, so it would not have anything to do with client side javascript, which is the name on the door here.
    <i>
    </i> ____________________ ___________
    |[color="red"]Some Person's server[/color]|&lt;----info------|[color="red"]Your server[/color]|
    |____________________|----request---&gt;|___________|
    / |
    | html page
    request |
    | |
    | /
    [color="blue"]Client's Browser[/color]

    The other possibility is that Some Person sends a page from their server to a client's browser with some javascript on it, then some event on the page triggers the javascript to execute, and the javascript code sends out a request for information from your server.
    <i>
    </i> ____________________ ___________
    |[color="blue"]Some Person's server[/color]| |[color="red"]Your server[/color]|
    |____________________| |___________|
    / | /
    | html page /
    request | /
    | | /
    | / /
    [color="red"]Client's Browser[/color]-------------/


    Loading a page sent by your server into a hidden iframe might be a possibility, but I think there are security restrictions that prevent a client's browser from accessing a page loaded into a frame when it is from another domain. Another possibility is an XMLHttpRequest. See here:

    http://developer.apple.com/internet/webcontent/iframe.html

    http://jibbering.com/2002/4/httprequest.html#HEAD

    http://www.quirksmode.org/dom/importxml.html

    In any case, it seems to me that Some Person who has his website on the other server is going to have to know some advanced javascript to be able to get his client's browsers to retrieve information from your server.
    Copy linkTweet thisAlerts:
    @ramccallauthorNov 28.2004 — Either of those two methods is what I'm after. Okay I think we are much closer to finding a solution here (:

    I was thinking the method I wanted to use was the first that you suggested. If anyone could send me some JS code that 'Some Person' (aka 'John') would need so that he can display his 'score' on his own site (located on a seperate server to that which the database containing his 'score' is), that would be greatly appreciated.

    I don't really want to use iframes, so I'll have a look at this XMLHttpRequest thing. Cheers (:
    ×

    Success!

    Help @ramccall spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...