Jump to content

[SOLVED] Priniting PHP variables into Javascript without page refresh


mikemessiah

Recommended Posts

Hi all,

 

I query a mysql database and get values these variables now need to be put into a javascript function and then run that javascript function.

 

I have tried running:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="JavaScript" type="text/javascript">
// JavaScript Document

function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (err2) {
	try {
	req = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (err3) {
	  req = false;
	}
}
}
return req;
}

var http = getXMLHTTPRequest();

function getServerText() {
var myurl = 'java-jump.txt';
myRand = parseInt(Math.random()*999999999999999);
var modurl = myurl+"?rand="+myRand;
http.open("GET", modurl, true);
http.onreadystatechange = useHttpResponse;
http.send(null);
}

function useHttpResponse() {
if (http.readyState == 4) {
if(http.status == 200) {
var mytext = http.responseText;;
document.getElementById('myPageElement').innerHTML = mytext;
}
} else {
document.getElementById('myPageElement').innerHTML = '<div id="loading-gif"><img src="anim.gif"><br><p>Loading... Please Wait</p></div>';
}
}
</script>
</head>

<body>

<a onclick="getServerText()">TEST</a>

<div id="MapPanel" style="width:400px;height:300px;"></div>
<div id="myPageElement" class="TEST"></div><!--End #Advanced Search--> 



</body>
</html>

 

so that it brings in "servertext.php"

 

<?php 

echo "Wish this would work";

require ("config.php");	

$conn = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbdatabase, $conn);

$UnitSerialNumber = '358278002008100';

$query = "SELECT * FROM $dbdatabase.$dbtableDecodedData WHERE UnitID = '$UnitSerialNumber' order by MessageDateTime desc limit 1";

$result = mysql_query($query) or die('Unable to run Serial Search query : ' . mysql_error());
$DecodedData = mysql_fetch_array($result);

$UnitID = $DecodedData["UnitID"];
$MessageDateTime = $DecodedData["MessageDateTime"];
$Latitude = $DecodedData["Latitude"];
$Longitude = $DecodedData["Longitude"];
$Speed = $DecodedData["Speed"];
$Altitude = $DecodedData["Altitude"];
$Network = $DecodedData["Network"];
$Address = $DecodedData["Address"];

$query = "SELECT * FROM $dbdatabase.$dbtableUnits WHERE UnitSerialNumber=$UnitSerialNumber ";

$result = mysql_query($query) or die('Unable to run GPSUnits Search query : ' . mysql_error());
$Units = mysql_fetch_array($result);

$Owner = $Units["Owner"];
$DateAdded = $Units["DateAdded"];
$CommonName = $Units["CommonName"];

$query = "SELECT * FROM $dbdatabase.$dbtableABIUnits WHERE UnitSerialNumber=$UnitSerialNumber ";

$result = mysql_query($query) or die('Unable to run ABIUnits Search query : ' . mysql_error());
$ABIUnits = mysql_fetch_array($result);

$Description_Unit = $ABIUnits["Description_Unit"];
$Region = $ABIUnits["Region"];
$Location_Info = $ABIUnits["Location_Info"];
$Location_Latitude = $ABIUnits["Location_Latitude"];
$Location_Longitude = $ABIUnits["Location_Longitude"];

$query = "SELECT * FROM $dbdatabase.$dbtableABIUnits_Alerts WHERE Unit_Serial=$UnitSerialNumber ";

$result = mysql_query($query) or die('Unable to run Alert query : ' . mysql_error());
$ABIUnits_Alerts = mysql_fetch_array($result);

$Unit_Serial = $ABIUnits_Alerts["Unit_Serial"];
$Alert_Type = $ABIUnits_Alerts["Alert_Type"];
$Time = $ABIUnits_Alerts["Time"];
$State = $ABIUnits_Alerts["State"];
$Comment = $ABIUnits_Alerts["Comment"];

echo "$Speed is the speed - lat: $Latitude - long: $Longitude ";


echo "

<script language=\"JavaScript\" type=\"text/javascript\">
		var g_map = null;		
            function initAGMap1()
		{
        		g_map = new AGMap(document.getElementById(\"MapPanel\"), '22615975125160232162022552211342541312121430183244137157');
		}			

		function btnAddSerialClick()
		{
			g_map.AddPoint(\"/images/green.gif\",\"img1\", $Latitude,$Longitude,-8,-8,\"<b>Init $UnitID</b>\",\"State: $State <br>Speed: $Speed Kmph <br> Altitude: $Altitude MSL <br> Network: $Network <br> Area: $Address <br> Region: $Region <br> Location Info: $Location_Info <br> Description: $Description_Unit \");
			g_map.CentreAndScale($Latitude,$Longitude,9);
		}
		function btnResetMapClick()
		{
			g_map.ResetMap();
		}

initAGMap1();			
btnAddSerialClick();			
</script>
"; ?>

 

The php prints and it is visable on the page. but the javascript functions do not run.

 

Please help  ???  ???

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.