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?


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

Open in new window

JuniorBeeAsked:
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.

leakim971PluritechnicianCommented:
Hello JuniorBee,

A simple one

Your HTML page :




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

Open in new window

0

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
leakim971PluritechnicianCommented:
setMessageRead.php :

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

<?PHP
    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);
        mysql_close();
        echo "Message set as read";
    }
    else {
        echo "invalid message id";
    }
?>

Open in new window

0
JuniorBeeAuthor Commented:
Hi I'm sorry, I need to do it without submitting the page (AJAX)
0
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!

leakim971PluritechnicianCommented:
It's the case. We're using ajax here...
0
Slick812Commented:
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.
0
leakim971PluritechnicianCommented:
Hello @angelIII,

I posted valid answers : ID:28876208 and ID:28876844
0
leakim971PluritechnicianCommented:
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
0
leakim971PluritechnicianCommented:
Thank you very much @ModEErf
0
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
AJAX

From novice to tech pro — start learning today.