This is a PHP code connecting itself to a MySQL database. The deal is, it displays a list of names stored in the database. What I need to do, is that when clicked upon, the names should display a Chat box of their own (same as in Facebook).
The problem here lies in the line marked with arrow. When clicked upon the particular name, it should call the function display_chat_box, but doesn't. It worked well until I hadn't added the studentid argument in it. I suspect the function chat_box_number() being sent as an argument in it, but it really isn't helping me out. It worked fine earlier, so why isn't it now? I opened up Developer Tools in Google Chrome to check the error, and it displays
**Uncaught ReferenceError :
display_chat_box()is not defined**.
I cannot understand why? Can anybody else? And whatever the problem exists, could anybody please post a working solution to it so I can bring back the chat boxes to life.
<?php
$con = mysql_connect("localhost", "****", "******");
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM online_students");
while ($row = mysql_fetch_array($result)) {
$result2 = mysql_query("SELECT * FROM students WHERE email='$row[email]'");
$row2 = mysql_fetch_array($result2);
$name = $row2['firstname'] . " " . $row2['lastname'];
$studentid = $row2['studentid'];
echo "<li onclick='display_chat_box(chat_box_number(),\"" . $name . "\"," . $studentid . ")'>" . $name . "</li>";
}
mysql_close($con);
?>
<script src="jquery-1.9.1.min.js" type="text/javascript">
var n = 0, chat_wid;
function chat_box_number() {
if (n == 2)
n = 0;
n++;
return n;
}
function display_chat_box(j, name, studentid) {
alert("hi");
switch (j) {
case 1: {
document.getElementsByClassName("chat_box1")[0].style.display = "block";
document.getElementById("chat_with1").innerHTML = name;
break;
}
case 2: {
document.getElementsByClassName("chat_box2")[0].style.display = "block";
document.getElementById("chat_with2").innerHTML = name;
break;
}
case 3: {
document.getElementsByClassName("chat_box3")[0].style.display = "block";
document.getElementById("chat_with3").innerHTML = name;
break;
}
}
chat_wid = studentid;
update_chat();
}
</script>
<script>opening tag after your jquery include line, no?