Jump to content

Javascript + Mysql Query


unknown gamer

Recommended Posts

The short answer is no. Javascript affects the browser, on the user's desktop. MySql exists on a server. To talk to it, you need to go through a server language, like PHP or ASP.The long answer is you can have a server script that reacts to form requests or AJAX requests. So, yes it's possible, but not with a simple function call.

Link to comment
Share on other sites

Right. What are you trying to do?Do you want javascript to trigger a script to make a mysql query? You said something about changing a query. That totally threw me. Do you mean the script should make a query based on some data that javascript sends it? Like, click button A, make query A -- click button B, make query B. And then maybe post the results back to the browser?What server-side scripting language do you use? In what structure would you like the data returned? HTML? JSON? XML? Plain text?You probably want to begin with an AJAX tutorial. Try to build yourself a teeny-weeny system where you can post some data to a server script and have it posted right back to you. No DB, no files. Just catch it and send it back. Get that working first, as a separate project. After it works, you can think of expanding it so it does things to your DB and your javascript side can figure out what to do with the data you send back.

Link to comment
Share on other sites

Like, click button A, make query A -- click button B, make query B. that's exactly what i want done... I use PHP and HTML and I want it returned in HTML (In a form). And do you have any good ajax tutorials? I can't seem to find any.

Link to comment
Share on other sites

These are not ready for prime time, but they'll get you started. I'd send you to the school, but all the server-side examples use ASP.http://www.sophox.com/sophox/ajax/ajax1.htmlhttp://www.sophox.com/sophox/ajax/ajax2.htmlhttp://www.sophox.com/sophox/ajax/ajax3.htmlhttp://www.sophox.com/sophox/ajax/ajax4.html

Link to comment
Share on other sites

How about avoiding client-side script altogether and do it the easy way?

<a href="mypage.php?table=person">Get Person List</a><a href="mypage.php?table=order">Get Order List</a>

Then, in the PHP, look at the query string for the "table" key and, if the value is "person" run your query on the Persons table, otherwise run it on the Orders table.

Link to comment
Share on other sites

Then the page would re-load.... Edit: thanks for the tuts DD. I have successfully have that example working and understand it 75%. I want to 100% understand it then i'll edit it.

Link to comment
Share on other sites

The client-side way would be to move that exact functionality to AJAX and use it to send your request to "mypage.php?table=person" or "mypage.php?table=order". I'd suggest getting it to work without AJAX first, and moving the functionality over to the AJAX method once everything is hammered out.

Link to comment
Share on other sites

Okay this is the situation i'm in:I am 99% sure I got it working. I don't know though. I am using a not so great host, 1 Database. I need this host because I am using it to connect to with a dll called 39dll with Game Maker. Its the only host after 4 hours of searching that could be connected to with this dll and had a database. So I code this and everything, it turns out Javascript is not enabled by this host. So tomorrow I have to search for another few hours for a good host. I'll reply here when i've tested the code.

Link to comment
Share on other sites

Javascript is run on the client side (most of the time) not the host side, are you sure you need it to run on the host side? AJAX is just creating HTTP GET's for you on the clients machine through the web browser.

Link to comment
Share on other sites

When i try to past any code between a javascript code I get : Not AcceptableAn appropriate representation of the requested resource /cgi-bin/fmanager.cgi could not be found on this server.When i try to save...

Link to comment
Share on other sites

Im assuming the file manager... Also I found out, I can save the entire script but the ONLY code that does not work is: AJAX.onreadystatechange = my_ajax_callback;I don't know why. Also the entire script is: <meta http-equiv="content-type" content="text/html;charset=UTF-8"><script type="text/javascript">function my_ajax_post (data, url, callback, mode) {AJAX = window.XMLHttpRequest ? new XMLHttpRequest() : window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : null;AJAX.onreadystatechange = my_ajax_callback;AJAX.open ("post", url, mode);AJAX.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");AJAX.send (data);}function my_ajax_callback () {var p = document.getElementById("my_response");if (AJAX.readyState == 4){if (AJAX.status == 200) {p.innerHTML = AJAX.responseText;} else {p.innerHTML = "Sorry. The following server error occurred:" + AJAX.status;}}}function send_data () {var data = "table=";data += document.getElementById("tables").value;my_ajax_post (data, "test.php", my_ajax_callback, true);}function init () {document.getElementById("my_button").onclick = send_data;}window.onload = init;</script>

Link to comment
Share on other sites

Okay, I found a better host and fixed the problem. Now I want it to repeatedly recieve data from the database.Here is what I have:

<html><head><title>RGB</title><link rel="stylesheet" type="text/css" href="style.css"><meta http-equiv="content-type" content="text/html;charset=UTF-8"><script type="text/javascript">function my_ajax_post (data, url, callback, mode) {AJAX = window.XMLHttpRequest ? new XMLHttpRequest() : window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : null;AJAX.onreadystatechange = my_ajax_callback;AJAX.open ("post", url, mode);AJAX.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");AJAX.send (data);}function my_ajax_callback () {var p = document.getElementById("my_response");if (AJAX.readyState == 4){if (AJAX.status == 200) {p.innerHTML = AJAX.responseText;} else {p.innerHTML = "Sorry. The following server error occurred:" + AJAX.status;}}}function send_data () {var data = "table=";data += document.getElementById("tables").value;my_ajax_post (data, "get.php", my_ajax_callback, true);}function timer(){setInterval("my_ajax_callback",5000);}function init () {document.getElementById("tables").onChange = send_data;}window.onload = init;</script></head><body onLoad="timer();"><center><a href="index.html"><img border="0" src="/images/rgbheader600x200.gif" width="600" height="200" /></a><p style="font-family:verdana; color:00FF00; font-size:16px;"><a href="http://rgb.orgfree.com">Home Page</a></p><div><select name="table" id="tables" onChange="send_data();"><label>choose:</label><option value="table1" id="table_1">Unlimited</option><option value="table2" id="table_2">Timed</option></select></div><div id="my_response"><?phpinclude "connect.php";$i=1;echo "<br /><table><tr><th style='font-family:verdana; text-align:center; color:#FF0000; border:#000000;'>Rank</th><th style='text-align:center; font-family:verdana;  color:#00FF00; border:#000000;'>Name</th><th style='text-align:center; font-family:verdana; color:#0000FF; border:#000000;'>Score</th></tr>";$result = mysql_query("SELECT * FROM rgb order by score+1 DESC LIMIT 100");while($row = mysql_fetch_array($result)){  echo "<tr>";  echo "<td style='text-align:center; font-family:verdana; border:0px;color:#FF0000;'>" . htmlspecialchars($i) . "</td>";  echo "<td style='text-align:center; font-family:verdana; border:0px;color:#00FF00;'>" .htmlspecialchars($row[name]). " </td>";  echo "<td style='text-align:center; font-family:verdana; border:0px;color:#0000FF;'>" . htmlspecialchars($row['score']) . "</td>";  echo "</tr>";  $i++;}echo "</table>";?>  </div><p style="font-family:verdana; color:00FF00; font-size:16px;"><a href="http://willhostforfood.com/?Action=download&fileid=57397">Download it here!</a></p></center></body></html>

So as you can see, when the body loads it sets a setInterval, which calls: my_ajax_callback. I was hoping it called the data again but i guess not, so now i'm stumped. I was gonna make it call the send_data function but it wouldn't know which table to get so i got confused... I was going to use the things in brackets like: (table) and set that to whatever table but i'm not too good at that so i just left it..I was hoping someone could quickly fix the timer, i don't think it's overally that hard but i'm still learning.

Link to comment
Share on other sites

The callback is useless without sending data first. The callback just processes data from the response, if you haven't even sent a request there's not going to be a response. Have it run send_data and set a default value on the dropdown, so that even when the page loads something is already selected.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...