Page 1 of 1

Updating mySQL records using PHP & AJAX

Posted: Mon Aug 14, 2006 1:41 pm
by matt1019
Hi guys,

I would like to know how to approach/tackle this "feature" that I would like to add to my project.

Lets suppose I have an image on a page.... an image called "ThankYou.gif".

In the mySQL Database, I have a table called "thanks" with colums:

thanks_id ==> smallint(5) UNSIGNED auto_increment
thanks_counter ==> smallint(50) UNSIGNED defualt=0

Now, every time a user clicks on this image, I would like to increase the counter by 1.... ( counter++; ) ;)

If you can point me to the right resource on the web, that would be helpful..... if you can show an example: Even better :P :twisted:


I can do this right now.... but not without refreshing the page ;(

Need to spice up my project with some AJAX (Not gonna OVERKILL it... dont worry)

Thanks Guys,

-Matt

Posted: Mon Aug 14, 2006 3:11 pm
by Ollie Saunders
I'm prepairing an answer for you. Gimme a few.

Posted: Mon Aug 14, 2006 6:23 pm
by Ollie Saunders
OK that was a little more than two minutes but hey who's complaining?

This is a complete AJAX page. No actual XML but all the client server communication is there and all the browser variables have been abstracted away. Tested and working for IE 6, FF 1.5.0.6 and Opera 8.54. Enjoy!

(I used a book to help me do this, I'm not an AJAX genius :()

Code: Select all

<?php
if (!empty($_POST)) {
    echo "Hi JavaScript, I'm PHP.\n\nThanks for sending me these key value pairs:\n\n";
    // Remove this later, outputting everything in $_POST is a real security hazard
    foreach ($_POST as $k => $v) {
        echo "$k: $v\n";
    }
    exit;
}
?>
<html lang="en">
    <head>
        <title>AJAX Test</title>
<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);
    }
} 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']; ?>';
        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)
    {
        document.getElementById('thinger').style.backgroundColor = '#f90';
        alert(sResponseText); // this'll tell you if it worked.
    }
}


</script>
    <style type="text/css">
    div#thinger {
        position:absolute;
        top:100px; left:100px;
        background-color:#09f;
        border:solid #000 1px;
        width:100px; height:100px;
    }
    </style>
    </head>
    <body>
        Click the thinger:
        <div id="thinger" onclick="myEvents.onThingerClick(); return false">
            I'm the thinger
        </div>
    </body>
</html>

Posted: Mon Aug 14, 2006 7:09 pm
by matt1019
Hi ole,

thanks for this great help ;)

So now, how can I go about actuallly adding to the MYSQL db? (increment the counter)


I tested your code, and returns the javascript alert box on clicking it with the requested ID, time, and the message "Hi Javascript....."

So, I will be adding my code (the original code that I had which refreshed the page to work) in this place?

ok, here is what I mean,

should I replace this part:

Code: Select all

<?php 
if (!empty($_POST)) { 
    echo "Hi JavaScript, I'm PHP.\n\nThanks for sending me these key value pairs:\n\n"; 
    // Remove this later, outputting everything in $_POST is a real security hazard 
    foreach ($_POST as $k => $v) { 
        echo "$k: $v\n"; 
    } 
    exit; 
} 
?>
with my code to add to the db?
this is my current code (the code that worked without ajax):

Code: Select all

<?php 
if (!empty($_POST)) { 
dbquery("UPDATE ".$db_prefix."thankyou SET thanks_to='$id', t_counter=t_counter+1 WHERE thanks_to='$id'");
    exit; 
} 
?>
(of course, the $id part would be something I need to get out of the page....)


-Matt

Posted: Mon Aug 14, 2006 7:20 pm
by Ollie Saunders
Yep that's what you should be doing.

Posted: Mon Aug 14, 2006 9:21 pm
by matt1019
Hi ole,

thanks! on it right now ;)

You mentioned about XML.... is it required? when would it be necessary to include it? do I need to have it? if I add XML interaction/communication in what ways can it improve it (or does it have neg. effect)?

Unlike you, I have VERY little AJAX exp..... trying to learn AND implement it at the same time.... ;)


By the way, i created a table in my DB and wrote a quick function and called it using the following way (using your script )

YOUR FILE---MODIFIED THE FIRST PART

AJAXTEST.php

Code: Select all

<?php 
if (!empty($_POST)) { 
	require_once "ajax-func.php";
	inctcount($lookup);
    exit; 
} 
?> 
<html lang="en"> 
    <head> 
        <title>AJAX Test</title> 
<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); 
    } 
} 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']; ?>'; 
        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) 
    { 
        document.getElementById('thinger').style.backgroundColor = '#f90'; 
        alert(sResponseText); // this'll tell you if it worked. 
    } 
} 


</script> 
    <style type="text/css"> 
    div#thinger { 
        position:absolute; 
        top:100px; left:100px; 
        background-color:#09f; 
        border:solid #000 1px; 
        width:100px; height:100px; 
    } 
    </style> 
    </head> 
    <body> 
        Click the thinger: 
        <div id="thinger" onclick="myEvents.onThingerClick(); return false"> 
            I'm the thinger 
        </div> 
    </body> 
</html>
here's what my ajax-func.php file looks like:

ajax-func.php

Code: Select all

<?php
include "maincore.php";
	function inctcount($tid){
	global $db_prefix;
		dbquery("INSERT INTO ".$db_prefix."ajaxtest VALUES('','".$lookup."','1')");
		return $tid;
	}

?>
and works great ;)


You should write/submit a tutorial out of this!! I am sure MANY people would appreciate it....I know I did :):)

-Matt

Posted: Tue Aug 15, 2006 2:55 am
by Ollie Saunders
You mentioned about XML.... is it required? when would it be necessary to include it? do I need to have it? if I add XML interaction/communication in what ways can it improve it (or does it have neg. effect)?
Nope not required. Simply point, when JS makes the request and gets information back from the server it is in a textual form. People use XML or JSON when they need to pass some data back to JS, although there is nothing stopping you using CSV or any other kind of data structure.

At its most simple you could probably return a single number determining if the operation was a sucess or not and then act accordingly in JS.
Note when I say 'return' I mean output in PHP because JS only pick up on the ouptut (echos and prints etc.)

The following examples assume inctcount returns a number on success and false on failure.

Pure Text

Code: Select all

if (!empty($_POST)) {
    require_once "ajax-func.php";
    var_export(inctcount($lookup));
    exit;
}
JSON with a bit more information

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;
}

Code: Select all

{success:true,count:5,operation:inctcount}
This output can simply be evald in JS and assigned to a variable of your choosing. You might want to look into security and JSON if you decide to use this.

XML also with a bit more information

Code: Select all

if (!empty($_POST)) {
    define('XML_BEGIN', "<?xml version=\"1.0\"?>\n");
    require_once "ajax-func.php";
    echo XML_BEGIN;
    echo '<operation>inctcount</operation>';
    $result = inctcount($lookup));
    echo '<result>' . var_export((bool)$lookup , true) . '</result>';
    if ($result) {
        echo '<data name="count">' . $result . '</data>';
    } else {
        echo '<error type="userMessage">Error whilst counting inct</error>';
    }
    exit;
}
XML is the most flexible structure because of the way you can have attributes as well as nodeValues. But you'll have to parse it with something like XPath in JS.

Posted: Tue Aug 15, 2006 3:53 am
by dibyendrah
dear all,
I have heard that Ajax is great but till now I have not used that yet. I want to do similar things using AJAX without refreshing the page. I'm planning to use AJAX with PHP. Is there any pre-requisites for using AJAX based functions ?
If you guys have anything to read on internet, please post the links ...

Regards,
Dibyendra

Posted: Tue Aug 15, 2006 11:18 am
by matt1019
@dibyendrah:

Hello mate, it all depends on what exactly you want to do/accomplish as far as tutorials go.
For example, if you want to have a "cool" accordian panel, you only add like 10 lines of code and reference the library like prototype/rico ;)
If you want to interact with the database, without refreshing the page.... well, you are AT the right place... follow this thread, as I have encountered a problem.

@ole,

Ok, everything works as expected, I have incorporated your script into my project.... one problem though:

The counter does not update until I refresh the page.

Therefore I added a quick line into your scirpt:

Code: Select all

document.getElementById('thinger').innerHTML = (oRequest.responseText)
what should this line do? this line should update the old counter to the new counter...

for example, if the thinger dive contained: "10 thanks yous"
this line should change it to "11 thank yous"
of course, if the msql operation was successfull.

But this line gives nothing but error... saying its not ready to be inputed yet.

any help/suggestion, ole?

-Matt

Posted: Tue Aug 15, 2006 12:54 pm
by Ollie Saunders
OK say you were using JSON in my example earlier

Code: Select all

onAjaxResponse:function(sResponseText)
{
    var php = eval(sResponseText); // I think its like that
    if (php.success) {
        var dThinger = document.getElementById('thinger');
        dThinger.nodeValue = php.count;
    }
}

Posted: Tue Aug 15, 2006 2:07 pm
by matt1019
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