Ajax处理表单提交但不发布变量数据

时间:2022-11-24 11:24:36

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.

我希望这将是我今晚的最后一个问题,这真的很烦人。我终于找到了一种方法来让这个Ajax使用两个单独的PHP脚本处理HTML表单,这就是我认为最适合我的方式。我将发布所有脚本,以便更容易找出问题。

Here is the form (messages.php):

这是表单(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):

这是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):

我使用此SAME脚本作为对(system / sars_system.php)和(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.

这工作正常,似乎运行正常,虽然我在MySQL中得到空的结果,变量数据应该是,我不知道为什么。

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.

我之所以使用Ajax的原因是因为它似乎是出于某种原因实际工作的唯一选择?所以我不确定这是否与问题有关 - 请注意我完全不知道如何使用Ajax,我还在学习。

If anyone has any idea why the data isn't posting, it would be great to have your help! Thanks!

如果有人知道为什么数据没有发布,那么获得你的帮助会很棒!谢谢!

1 个解决方案

#1


0  

Your INSERT query is referring to "$usr_message"

您的INSERT查询是指“$ usr_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')");

You are reading from the $POST as

你是从$ POST读取的

$message = $_POST['message'];

Change the above to

将上面的内容改为

$usr_message = $_POST['message'];

#1


0  

Your INSERT query is referring to "$usr_message"

您的INSERT查询是指“$ usr_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')");

You are reading from the $POST as

你是从$ POST读取的

$message = $_POST['message'];

Change the above to

将上面的内容改为

$usr_message = $_POST['message'];