doujiang1939 2015-12-11 19:47
浏览 35
已采纳

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

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!

  • 写回答

1条回答 默认 最新

  • douwu5009 2015-12-11 20:37
    关注

    Your INSERT query is referring to "$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

    $message = $_POST['message'];
    

    Change the above to

    $usr_message = $_POST['message'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥15 在现有系统基础上增加功能