Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AJAX Problem; loading data from server

Status
Not open for further replies.

jubalbarca

Programmer
Oct 25, 2008
12
GB
I'm trying to load variables from the server into a Javascript application with AJAX.

Here are the relevant parts of the code;

Javascript;
var name = 1;
var xmlHttp = createXmlHttpRequestObject();
var newx = 0;

function createXmlHttpRequestObject()
{
var xmlHttp;
if(window.ActiveXObject)
{
//try
//{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
//}
//catch(e)
//{
//xmlHttp = false;
//}
}
else
{
//try
//{
var xmlHttp = new XMLHttpRequest();
//}
//catch(e)
//{
//xmlHttp = false;
//}
}
if (!xmlHttp)
alert("Error makin stuff work, sesifckally creatin' the Haitch Tee Tee Pee requesterization hobject sah...");
else
return xmlHttp;
}

function processx()
{
window.alert("processx started!")
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
{
window.alert("Server ready, sending message!")
nameserv = name;
xmlHttp.open("GET", "adbrat.php?name=" + name, true);
xmlHttp.onreadystatechange = handleServerResponsex;
xmlHttp.send(null);
}
else
setTimeout ('process()', 1000)
}

function handleServerResponsex()
{
if (xmlHttp.readyState == 4)
{
window.alert("SERVER READY! (xval)");
if (xmlHttp.status == 200)
{
window.alert("MESSAGE RECIEVED!");
xmlResponse = xmlHttp.responseXML;
window.alert(xmlResponse);
xmlDocumentElement = xmlResponse.documentElement;
window.alert(xmlDocumentElement);
newx = xmlDocumentElement.firstChild.data;
window.alert(newx);
money = newx;
window.alert(money);
//defaultx = newx;
//window.alert(defaultx);
//xmymove(newx, 20);
//window.alert(newx);
}
else
{
alert("H'im afarid I can not get into the server, m'lud. (xval) Aplolgies." + xmlHttp.statusText);
}
}
}

PHP;
<?php
$conn = mysql_connect("localhost", "", "");
$name = $_GET['name'];
mysql_select_db("test", $conn);
$sql = "SELECT user, xloc, yloc FROM user_location where user = '$name' ";
$result = mysql_query($sql, $conn) or die(mysql_error());
while ($newArray = mysql_fetch_array($result)){
$x = $newArray['xloc'];
}
header ('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
echo '<response>';
echo $x;
echo '</response>';
?>

The popup boxes after I get the message back from the PHP come up with '[object]' then stop at the one where I try and show the value of money.

 
this is a javascript question about how best to parse incoming xml data. i suggest you ask in the js forum.
 
Hi

I would suggest to format the output data with [tt]json_encode()[/tt] in PHP then just [tt]eval()[/tt] it in JavaScript.

So your code should look like this :
JavaScript:
var name = 1;
var xmlHttp = createXmlHttpRequestObject();
var newx = 0;

function createXmlHttpRequestObject()
{
  var xmlHttp;
  if (window.ActiveXObject) {
    try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") } catch(e) { xmlHttp = false; }
  } else {
    try { var xmlHttp = new XMLHttpRequest(); } catch(e) { xmlHttp = false; }
  }
  if (!xmlHttp) alert("Error makin stuff work, sesifckally creatin' the Haitch Tee Tee Pee requesterization hobject sah...");
  else return xmlHttp;
}

function processx()
{
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
    nameserv = name;
    xmlHttp.open("GET", "adbrat.php?name=" + name, true);
    xmlHttp.onreadystatechange = handleServerResponsex;
    xmlHttp.send(null);
  } else setTimeout ('process()', 1000)
}

function handleServerResponsex()
{
  if (xmlHttp.readyState == 4) {
    if (xmlHttp.status == 200) {
      [red]jsonResponse[/red] = [red]eval([/red]xmlHttp.responseText[red])[/red];
      newx = [red]jsonResponse[/red];
      money = newx;
//defaultx = newx;
//xmymove(newx, 20);
    } else {
      alert("H'im afarid I can not get into the server, m'lud. (xval) Aplolgies." + xmlHttp.statusText);
    }
  }
}
PHP:
<?php
$conn = mysql_connect("localhost", "", "");
$name = $_GET['name'];
mysql_select_db("test", $conn);
$sql = "SELECT user, xloc, yloc FROM user_location where user = '$name' ";
$result = mysql_query($sql, $conn) or die(mysql_error());
while ($newArray = mysql_fetch_array($result)) {
  $x = $newArray['xloc'];
}
header('Content-Type: text/[red]javascript[/red]');
echo [red]json_encode([/red]$x[red])[/red];
?>
Note, that I not tested the above. I only modified it based on some old code of mine.

Feherke.
 
Hi

jpadie said:
this is a javascript question about how best to parse incoming xml data.
It may not be a JavaScript question, in case it is not a parsing problem. Maybe the PHP produces invalid XML.
Code:
echo [red]htmlspecialchars([/red]$x[red])[/red];

[gray]# or[/gray]

echo [red]htmlspecialchars([/red]$x[red],ENT_COMPAT,'UTF-8')[/red];

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top