For context I am new to jQuery, PHP, and MySQL. So apologies if this is an easy fix.
I am building a simple private messaging system based off my initial code for a public forum (which is much simpler). Going off what I have learned so far in the short span of time, I am almost done with the hard part (retrieving specific chat private data). But I am getting a undefined variable error that I can not seem to resolve.
Notice: Undefined variable: sessionmember in C:\\xampp\\htdocs\\StEndas\\handlers\\ajax_privatemessager.php on line 28
Notice: Undefined variable: othermember in C:\\xampp\\htdocs\\StEndas\\handlers\\ajax_privatemessager.php on line 28
Notice: Undefined variable: sessionmember in C:\\xampp\\htdocs\\StEndas\\handlers\\ajax_privatemessager.php on line 29
Notice: Undefined variable: othermember in C:\\xampp\\htdocs\\StEndas\\handlers\\ajax_privatemessager.php on line 29
No convo between you two yet.
This particular section of the mockup is contained within a while loop that prints the records of every member other than the user currently in session. All printed conversation windows are hidden with the a bootstrap pilltab menu (toggle-data).
<div id="member_ID_<?php echo $row['member_ID'];?>" class="tab-pane fade">
<header class="clearfix">
<h4>Your conversation with <b><?php echo $row['member_name'];?></b>.</h4>
</header>
<div class="chat">
<div class="privatemessager-chathistory scrollfeed">
</div> <!-- end forum-chathistory -->
<form action="#" method="post">
<fieldset>
<textarea placeholder="Type message.." id="privatemessager-message" name="privatemessager-message" class="message-box" required></textarea>
<input type="text" id="privatemessager-sessionmember" name="privatemessager-sessionmember" value="<?php echo $_SESSION['member_ID'];?>"/>
<input type="text" id="privatemessager-othermember" name="privatemessager-othermember" value="<?php echo $row['member_ID'];?>"/>
<input type="hidden">
</fieldset>
</form>
</div> <!-- end chat -->
</div>
The sending message section of the code has not been modified yet for this new purpose, please ignore that bit.
<script>
$(document).ready(function(){
loadChat();
});
$('#privatemessager-message').keyup(function(e){
var privatemessage = $(this).val();
// alert(e); // Displays the value of the keyboard button.
if(e.which == 13){ // 13 is the keyboard value of the 'Enter' key. "IF (USER HITS ENTER)"
$.post('handlers/ajax_privatemessager.php?action=sendPrivateMessage&privatemessager-message='+privatemessage, function(response){
loadChat();
$('#privatemessager-message').val('');
});
}
});
/* THIS IS THE SECTION NEEDING ATTENTION */
function loadChat(){
$.post('handlers/ajax_privatemessager.php?action=getPrivatemessage', function(response){
$('.privatemessager-chathistory').html(response);
});
}
setInterval(function(){
loadChat();
}, 1000); // Every 1 second the method 'loadchat' is activated.
</script>
The sending message section of the code has not been modified yet for this new purpose, please ignore that bit.
**<?php
session_start();
include "db_Connect.php";
if(isset($_REQUEST['action'])){
switch($_REQUEST['action']){
case "sendPrivateMessage":
$privatemessage = $_GET["privatemessager-message"];
$sessionmember = $_GET["privatemessager-sessionmember"];
$othermember = $_GET["privatemessager-othermember"];
$privatemessage = addslashes($privatemessage);
$sql = "INSERT INTO tbl_forum (message, member_ID) VALUES ('$privatemessage', '$new_member_ID' )";
$result = $mysqli->query($sql) or die(mysqli_error($mysqli));
echo 1;
break;
/* THIS IS THE SECTION NEEDING ATTENTION */
case "getPrivatemessage":
$sql = "SELECT privatemessage_message, privatemessage_time_created, privatemessage_sent_by, privatemessage_sent_to
FROM tbl_privatemessages
WHERE (privatemessage_sent_by = '$sessionmember' AND privatemessage_sent_to = '$othermember')
OR (privatemessage_sent_to = '$sessionmember' AND privatemessage_sent_by = '$othermember')
ORDER BY privatemessage_time_created ASC";
$result = $mysqli->query($sql) or die(mysqli_error($mysqli));
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<div class="chat-message clearfix">
<div class="chat-message-content clearfix">
<span class="chat-time"><?php echo $row['privatemessage_time_created'];?></span>
<h5><?php echo $row['privatemessage_sent_by'];?></h5>
<p class="chat-message"><?php echo $row['privatemessage_message'];?></p>
</div> <!-- end chat-message-content -->
</div> <!-- end chat-message -->
<hr>
<?php
}
} else {
echo "</br></br>";
echo "No convo between you two yet.";
}
break;
}
}
?>**
Any and all advice will be taken with much gratitude. Thank you in advance for taking your time to read this.
Your problem appears to be that in the case "getPrivatemessage"
code you are attempting to use your $sessionmember
etc. variables, however you have not actually set them, since the code to set their values ie
$privatemessage = $_GET["privatemessager-message"];
$sessionmember = $_GET["privatemessager-sessionmember"];
$othermember = $_GET["privatemessager-othermember"];
only exists in the "sendPrivateMessage"
branch of the switch
statement. You need to either copy that code into the "getPrivatemessage"
branch or perhaps move it to between
if(isset($_REQUEST['action'])){
and
switch($_REQUEST['action']){
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.