Using AJAX to update a record onclick of an image

I have an icon image and when i is clucked, I want to execute this function below.  Can anyone help with a very simple code example?

function readmessage($mid)
		mysql_query("UPDATE messages SET status='Read' WHERE messageid='".$mid."'");

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hello JuniorBee,

A simple one

Your HTML page :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<script language="javascript">
	function setMessageRead() {
		if(window.XMLHttpRequest) xmlhttp=new XMLHttpRequest(); else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                var messageid = document.getElementById("messageid").value;"GET","setMessageRead.php?messageid=" + messageid,false);
		if(xmlhttp.readyState==4) {
			alert( xmlhttp.responseText );
Message ID: <input id="messageid" name="messageid" />&nbsp;<input type="button" value="submit message id" onclick="setMessageRead();" />

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
setMessageRead.php :

(update with your own values : localhost, username, password and database)

    if( isset($_GET['messageid']) ) {
        $connect = mysql_connect('localhost','username','password') or die("Connexion error to server !");
        mysql_select_db("database", $connect) or die("Connexion error to database");
        $mid = $_GET['messageid'];

        mysql_query("UPDATE messages SET status='Read' WHERE messageid='".$mid."'");

        $result = mysql_query($query);
        echo "Message set as read";
    else {
        echo "invalid message id";

Open in new window

JuniorBeeAuthor Commented:
Hi I'm sorry, I need to do it without submitting the page (AJAX)
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

It's the case. We're using ajax here...
hello JuniorBee, , as far as I have ever seen, you can NOT do any MySQL database access in javascript, you will have to use the ajax submit, because MySQL is on the server, and not accessible from your browser in jscript. I think leakim971 is giving you the correct method.
Hello @angelIII,

I posted valid answers : ID:28876208 and ID:28876844
The user said : Hi I'm sorry, I need to do it without submitting the page (AJAX)

The valid answers (ajax source page doing the call and the one that answer it) : ID:28876208 and ID:28876844
Thank you very much @ModEErf
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.