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 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置