PDA

View Full Version : How to post variables to a server without a form?



sniperman
07-06-2009, 12:09 PM
Hi everyone,

A niggling question arose this week when creating a javascript-based application. I basically made a game with a "top score" scoreboard and wonder, "is Javascript capable of posting/emailing/sending to a server-side database the contents of variables, without using a FORM?"

The variables are also randomly generated by the math object with no user input.

I have read there are methods via AJAX and PHP to have Javascript interact with the server to store data.

If people could enlighten me I would appreciate the heads up.

viscent
07-07-2009, 09:10 AM
Yes AJAX will do.
A 'basic' code would be something like:


function createXMLHTTPRequest(){
var http=null;
if(window.ActiveXObject){
var progIds=Ajax._PROG_IDS;
for(var i=progIds.length; i>=0; i--){
try{
http = new ActiveXObject(progIds[i]);
break;
}catch(e){}
}
}else{
try{
http = new XMLHttpRequest();
}catch(e){

}
}
return http;
};
function handleResponse(xmlhttp){
var res=xmlhttp.responseText;
//do other things
}
function postData(callback){
var http=createXMLHTTPRequest();
http.open("POST","/post.php",true);
http.onreadystatechange=function(){
if(4!==http.readyState){
return;
}
var status=http.status;
var isOK=(!status || (status >= 200 && status < 300));
if(isOK){
callback(http);
}else{
alert("An error occurred:\n"+http.responseText);

}

}
http.send("var1=value1&var2=value2");

}

function test(){
postData(handleResponse);

}

Then in page /post.php,you can retrieve variable var1 and var2,in the same
way you do with them submitted via Form.
However,for production usage,I recommend you use some kind of AJAX framework(jQuery for example).

sniperman
07-11-2009, 01:21 PM
Thanks. The basic code was fine but the combined technologies I need to incorporate are fuzzy.

I provided a graphic as an image link (http://www.madchaos.com.au/images/Ajax_Query.gif) for a better grasp. (refresh the page if error 404)
http://www.madchaos.com.au/images/Ajax_Query.gif

The basic question is, how many different technologies does a client-side javascript code (using AJAX's XMLHttpRequest) need to send variable values to a server-side ODBC database like MS Access or MySQL? Is an SQL query essential and does PHP have to be incorporated to handle the javascript variables before PHP refers those variables to the database on the server?

I'm way above my learning curve right now but hopefully someone on these forums will be able to post links to tutorials or share methods so I can begin to have my page talk to a server database.

Jesdisciple
07-11-2009, 05:03 PM
Please write unobtrusive JavaScript (http://javascript.wikia.com/wiki/Unobtrusive_JavaScript).

Your image link gives me Error 403 (forbidden / permission denied).


The basic question is, how many different technologies does a client-side javascript code (using AJAX's XMLHttpRequest) need to send variable values to a server-side ODBC database like MS Access or MySQL? Is an SQL query essential and does PHP have to be incorporated to handle the javascript variables before PHP refers those variables to the database on the server?For your second, two-question sentence, yes and yes... So you sort of answered your own first question. :p

But I don't want to interrupt Viscent - just thought I'd give that link and some constructive responses while I was at it.

sniperman
07-12-2009, 06:03 AM
This thread will probably turn into a mini ajax tutorial because I like to be thorough...

From what has been said of XMLHttpRequest thus far I know variables (global or local) can be stored server-side via the XMLHttpRequest, using a query to PHP and another server-side query to a database on the same server.


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<script type="text/javascript">
var SendMeServerSide="this variable name and its string value";
</script>

<title>AJAX TESTER</title>

</head><body>

<p id="SendMeClientSide">Variable and Value passed to the server MySQL database via SQL query should show up here</p>

</body></html>

So in theory, I can send this variable via those technologies and frameworks to the server and have it return a value or arguments back to the website in the tag provided.

Can we scratch the surface on some of the methods to use and how to get Javascript talking with the server, (without forms).

Jesdisciple
07-14-2009, 04:13 AM
Well, it looks like he's not going to respond.

There are (at least) two ways to communicate with the server. There's XML, which is supposedly part of the name (read somewhere that the originator denies this, but can't cite anything). There's also JSON (http://www.json.org/) (in PHP (http://www.php.net/json)), which I currently prefer.