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?
 
leakim971Connect With a Mentor PluritechnicianCommented:
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
 
leakim971Connect With a Mentor PluritechnicianCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
leakim971PluritechnicianCommented:
It's the case. We're using ajax here...
0
 
Slick812Connect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.