Here's my situation I have a perfect membership system I realized it's been easier to just use a form with some hidden input with predetermined values created from previous button clicks from previous pages to send the user id and the other user id aka the other member to other pages which the post data becomes variables eventually in each page deeper in each code document which are eventually use in SQL statements. My method has been working but now I have a new problem now I have a private messaging system page where users can send private messages to each other so I found this really cool script which I later semi modify it to work on my membership system but it consist of two main pages that are used to make it work completely the first page consist of pretty much the private messaging system primary components the second page aka chat.php is a AJAX response page designed to show new messages. So how can I share the main page post data variables with the AJAX response page. I tried to put the ajax response page code on the same page of the main page but it looked strange and it gave me strange results. Note in this situation the partner_id is aka the other user id and since the first include of each page consist of the login member id already aka messenger_id it's just the other user id i'm concern of aka the partner_id and just know that the SQL column call message belongs to the messenger_id the partner_id in the SQL database is designed to show who the login member aka messenger_id talked to (partner_id) Here's the code
MAIN PAGE
<?php
include("0/instructions/php/session.php");
$messenger_id = $user_id;
$partner_id= $_POST['partner_id'];
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width">
<meta charset="UTF-8">
<title>Chat System in PHP</title>
<link rel="stylesheet" href="style.css" media="all"/>
<script>
function ajax() {
var req = new XMLHttpRequest();
req.onreadystatechange = function(){
if(req.readyState == 4 && req.status == 200) {
document.getElementById('chat').innerHTML = req.responseText;
}
}
req.open('GET','chat.php',true);
req.send();
}
setInterval(function(){ajax()},1000);
</script>
</head>
<body onload="ajax();">
<div class="main_container">
<div id="container">
<div id="chat_box">
<div id="chat"></div>
</div>
</div>
<form method="post" action="">
<input type="text" name="messenger_id" placeholder="messenger_id" value="<?php echo $messenger_id; ?>"/>
<textarea name="message" placeholder="enter message"></textarea>
<input type="text" name="partner_id" placeholder="partner_id" value="<?php echo $partner_id; ?>"/>
<input type="submit" name="submit" value="Send it"/>
</form>
<?php
if(isset($_POST['submit'])){
$messenger_id = $_POST['messenger_id'];
$message= $_POST['message'];
$partner_id= $_POST['partner_id'];
$query = "INSERT INTO messages_x (messenger_id,message,partner_id) values ('$messenger_id','$message','$partner_id')";
$run = $connect->query($query);
if($run) {
echo "<div id='hide_audio'><embed loop='false' src='chat.mp3' hidden='true' autoplay='true'/></div>";
}
}
?>
</div>
</body>
</html>
THE AJAX RESPONSE PAGE AKA CHAT.PHP
<?php
include("0/instructions/php/session.php");
$messenger_id = $user_id;
$partner_id = $_POST['partner_id'];
$query= "SELECT * FROM messages_x WHERE messenger_id='$messenger_id' AND partner_id='$partner_id' ORDER BY messenger_id DESC";
$run = $connect->query($query);
while($row = $run->fetch_array()) :
$messenger_id = $row['messenger_id'];
?>
<div id="chat_data">
<span style="color:green;"><?php echo $row['messenger_id']; ?></span> :
<span style="color:brown;"><?php echo $row['message']; ?></span>
<span style="float:right;"><?php echo formatDate($row['date']); ?></span>
</div>
<?php endwhile;?>
<?php
$query_other= "SELECT * FROM messages_x WHERE messenger_id='$partner_id' AND partner_id='$messenger_id' ORDER BY messenger_id DESC";
$run_other = $connect->query($query_other);
while($row_other = $run->fetch_array()) :
?>
<div id="chat_data">
<span style="color:gold;"><?php echo $row_other['messenger_id']; ?></span> :
<span style="color:purple;"><?php echo $row_other['message']; ?></span>
<span style="float:right;"><?php echo formatDate($row_other['date']); ?></span>
</div>
<?php endwhile;?>
In this example the hidden form inputs are shown for simple explanation here and as a result I want it to look like this.
[ATTACH]5465[/ATTACH][ATTACH]5465[/ATTACH]