I hope this will be the last question I make tonight, this is really getting annoying. I've finally found a way to get this Ajax working to process a HTML form using two separate PHP scripts, it's just the way that will work best for me I guess. I will post all scripts to make it a bit easier to figure out the problem.
Here is the form (messages.php):
<form action="" method="post" id="reply" name="reply">
<div class="form-group">
<textarea class="form-control" rows="3" cols="80" id="message" name="message" placeholder="Send a reply..."></textarea>
<input type="hidden" id="conversation_id" name="conversation_id" value="<? echo $co_conversation_id; ?>">
<input type="hidden" id="sarssystem" name="sarssystem" value="<? echo $sarssystem; ?>">
</div>
<div class="form-group" align="right">
<div class="btn-group" align="left" style="float:left">
<button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="glyphicon glyphicon-cog" aria-hidden="true"></span> <span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a href="messages.php?convoid=<? echo $co_conversation_id; ?>&del=check">Delete Conversation</a></li>
<li><a href="#">Visit Profile</a></li>
<li><a href="#">Report User</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Change Display Photo</a></li>
</ul>
</div>
<button type="reset" class="btn btn-default btn-sm">Cancel</button>
<button type="submit" id="reply" name="reply" class="btn btn-primary btn-sm" onclick="loadDoc()">Send Message</button>
</div>
</form>
Here is the Ajax (messages.php):
<script>
function loadDoc() {
$.ajax({
url: 'system/message_system.php',
type: 'POST',
dataType: 'json',
data: {
message: 'message',
conversation_id: 'conversation_id',
sarssystem: 'sarssystem',
user_id: 'user_id'
},
})
.done(function() {
$.ajax({
url: 'system/sars_system.php',
type: 'POST',
dataType: 'json',
data: {
message: 'message',
conversation_id: 'conversation_id',
sarssystem: 'sarssystem',
user_id: 'user_id'
},
})
.done(function() {
console.log("success");
})
.fail(function() {
console.log("error");
})
})
.fail(function() {
console.log("error");
})
}
</script>
I am using this SAME script as a test on both (system/sars_system.php) and (message_system.php):
require 'db.php';
$message = $_POST['message'];
$conversation_id = $_POST['conversation_id'];
$sarssystem = $_POST['sarssystem'];
$user_id = $_POST['user_id'];
$usr_message = str_replace("'","\\'",$message);
mysqli_query($conn,"INSERT INTO ap_messages (message_id, message, sender_id, time_sent, time_read, conversation_id)
VALUES ('','$usr_message','$user_id', NOW(), NOW(), '$conversation_id')");
mysqli_query($conn, "UPDATE ap_conversations SET time = NOW() WHERE conversation_id = '$conversation_id'");
echo json_encode('success');
This works fine and seems to run okay, although I am getting empty results in MySQL where the variable data should be, and I don't know why.
The reason I have used the Ajax in the way I have is because it seemed to be the only option which actually worked for some reason? So I'm not sure if that has anything to do with the problem - please note that I literally have no idea how to use Ajax, I am still learning.
If anyone has any idea why the data isn't posting, it would be great to have your help! Thanks!