Hi ole,
using your last code gave me error saying:
Code: Select all
A Runtime error has occured
do you wish to debug?
Syntax Error
When the same page was viewed under FireFox, the Javascript Console gave me the following line as the source of error:
Code: Select all
var php = eval(sResponseText); // I think its like that
So, I put the sResponseText in quotes:
Code: Select all
var php = eval("sResponseText"); // I think its like that
This does not give any errors, But now, it stalls... (i.e., the backgournd color remains white (#FFF))
And the counter gets displayed as "undefined"
Here's my updated Ajax.js file
Code: Select all
<script type="text/javascript">
<!--
////////////////////////////////////////////////////////////////////////////////
//
// TOOLKIT (you might want to include this as a separate file)
//
if (typeof XMLHttpRequest == 'undefined' && window.ActiveXObject) {
function XMLHttpRequest()
{
var sPre = 'MSXML2.XMLHTTP';
var aSigs = [sPre + '.5.0', sPre + '.4.0', sPre + '.3.0', sPre, 'Microsoft.XMLHTTP'];
for (var i=0; i<aSigs.length; i++) {
try {
var oRequest = new ActiveXObject(aSigs[i]);
return oRequest;
} catch (oError) {
// ignore
}
}
throw new Error(Pre + ' is not installed on your system.');
}
}
var Http = {
sNoSupport:'Browser does not support HTTP requests',
sHeaderContentType:'application/x-www-form-urlencoded',
delay:10,
getParamFormat:function(sUrl, sName, sValue)
{
sUrl += (sUrl.indexOf('?') == -1 ? '?' : '&');
sUrl += encodeURIComponent(sParamName) + '=' + encodeURIComponent(sParamValue);
return sUrl;
},
postParamFormat:function(sParams, sParamName, sParamValue)
{
if (sParams.length > 0) {
sParams += '&';
}
return sParams + encodeURIComponent(sParamName)
+ '=' + encodeURIComponent(sParamValue);
}
};
if (typeof XMLHttpRequest == 'object' || window.ActiveXObject) {
Http.get = function(sUrl, fnCallback)
{
var oRequest = new XMLHttpRequest();
oRequest.open('get', sUrl, true);
oRequest.onreadystatechange = function()
{
if (oRequest.readyState == 4) {
fnCallback(oRequest.responseText);
}
}
// space is supposed to be better than null but if
// you have problems replace space with null
oRequest.send(' ');
}
Http.post = function(sUrl, fnCallback, sParams)
{
var oRequest = new XMLHttpRequest();
oRequest.open('post', sUrl, true);
oRequest.setRequestHeader('Content-Type', Http.sHeaderContentType);
oRequest.onreadystatechange = function()
{
if (oRequest.readyState == 4) {
fnCallback(oRequest.responseText);
}
}
oRequest.send(sParams);
document.getElementById('thinger').innerHTML = (oRequest.sResponseText)
}
} else if (navigator.javaEnabled() && typeof java != 'undefined' && typeof java.net != 'undefined') {
Http.get = function(sUrl, fnCallback)
{
setTimeout(function ()
{
var oUrl = new java.net.URL(sUrl);
var oStream = oUrl.openStream();
var oReader = new java.io.BufferedReader(new java.io.InputStreamReader(oStream));
var sResponseText = '';
var sLine = oReader.readLine();
while (sLine != null) {
sResponseText += sLine + '\n';
sLine = oReader.readLine();
}
oReader.close();
fnCallback(sResponseText);
}, Http.delay);
}
Http.post = function(sUrl, fnCallback, sParams)
{
setTimeout(function ()
{
var oUrl = new java.net.URL(sUrl);
var oConnection = oUrl.openConnection();
oConnection.setDoInput(true);
oConnection.setDoOutput(true);
oConnection.setUseCaches(false);
oConnection.setRequestProperty('Context-Type', Http.sHeaderContentType)
var oOutput = new java.io.DataOutputStream(oConnection.getOutputStream());
oOutput.writeBytes(sParams);
oOutput.flush();
oOutput.close();
var sResponseText = '';
var oInput = new java.io.DataInputStream(oConnection.getInputStream());
var sLine = oInput.readLine();
while (sLine != null) {
sResponseText += sLine + '\n';
sLine = oInput.readLine();
}
oInput.close();
fnCallback(sResponseText);
}, Http.delay);
}
} else {
throw new Error(http.sNoSupport);
}
//
// ^ THAT WAS THE TOOLKIT, YOU TOOL
//
////////////////////////////////////////////////////////////////////////////////
// EXAMPLE CODE //
var myEvents = {
onThingerClick:function()
{
// Remember these can be tampered with like anything else on the client
// check this is outputting the correct page
var requestPage = 'http://<?php echo $_SERVER['HTTP_HOST'], $_SERVER['SCRIPT_NAME']; echo "?lookup=$lookup&modep=$lookup";?>';
var sParams = Http.postParamFormat('', 'requestId', 'dbIncrement');
sParams = Http.postParamFormat(sParams, 'time', '<?php echo time(); ?>');
document.getElementById('thinger').style.backgroundColor = '#fff';
Http.post(requestPage, myEvents.onAjaxResponse, sParams);
},
onAjaxResponse:function(sResponseText)
{
var php = eval(sResponseText); // I think its like that
if (php.success) {
var dThinger = document.getElementById('thinger');
dThinger.nodeValue = php.count;
document.getElementById('thinger').style.backgroundColor = '#f90';
alert(sResponseText); // this'll tell you if it worked.
}
}
}
//-->
</script>
<script>
//onAjaxResponse:function(sResponseText)
// {
// document.getElementById('thinger').style.backgroundColor = '#f90';
// alert(sResponseText); // this'll tell you if it worked.
// }
</script>
All I want to do is show the updated counter.... instead of the same number... hence the reason for innerHTML question.
Everything else works just fine
and I am using JSON at the top of my file:
Code: Select all
if (!empty($_POST)) {
require_once "ajax-func.php";
$result = inctcount($lookup)
echo '{success:' . var_export((bool)$result, true);
if ($result !== false) {
echo ',count:'. $result;
} else {
// perhaps a user message of what went wrong
echo ',error:"Error whilst counting inct"';
}
echo ',operation:inctcount}';
exit;
}
What to do?
-Matt