javascript - 无法使用 AJAX 识别 HTML 表单元素的值

标签 javascript php jquery html ajax

我正在尝试使用 AJAX 在我的项目中实现消息系统,但是我遇到了一些问题,因为我是 AJAX 新手。 下面的代码能够完美地将输入数据从 MsgMain.php 文件发送到 MsgInsert.php。但是,当我尝试从 MsgInsert.php 上的 MsgMain.php 获取 $_POST['msgSend'] 时,它失败了。

AJAX 代码位于 MsgMain.php

$(document).ready(function(){
    $("#chatBtn").click(function(){
        $("#msgBtn").val("chat");
    });
    $("#pmBtn").click(function(){
        $("#msgBtn").val("pm");
    });
});
$(function() {
    $("#msgBtn").click(function() {
        var textcontent = $("#msgInput").val();
        var dataString = 'content='+ textcontent;
        if(textcontent=='')
        {
            alert("Enter some text..");
            $("#msgInput").focus();
        }
        else
        {
            $.ajax({
                type: "POST",
                url: "msg/MsgInsert.php",
                data: dataString,
                cache: true,
                success: function(response){
                    document.getElementById('content').value='';
                    $("#msgBtn").focus();
                }
            });
        }
        return false;
    });
});

MsgMain.php 仅用于 HTML 表单的文件代码

<form action="MsgInsert.php" id="frmBox" name="msgSend" method="POST" onsubmit="return formSubmit();">
    <div class="input-group ">
        <input type="text" class="form-control" name="msgBox" id="msgInput" title="Enter your message" required>
        <div class="input-group-btn">
            <button class="btn btn-success w3-hover-white w3-hover-text-green" type="submit" id="msgBtn" title="Send your message" value="chat"><i class="glyphicon glyphicon-send"></i></button>
        </div>

    </div>
</form>

MsgInsert.php 文件代码,当我删除 $_POST['msgSend']

的 if 语句时效果很好
    <?php
if(!isset($_SESSION['login_user'])){
    header("location: ../index.php"); // Redirecting To Home Page
}
if (isset($_POST['content'])) {
    if (isset($_POST['msgSend'])) {
        $conn = mysqli_connect("localhost", "root", "", "msg");
        if (!$conn) {
            die('Could not connect: ' . mysqli_error($conn));
        }
        $content=$_POST['content'];
        mysqli_query($conn, "INSERT INTO `msgpm`(`id`, `msgFrom`, `msgTo`, `msg`) VALUES ('','bob','ram','$content')");
    }
}
?>

很抱歉,如果已经提出此类问题。

最佳答案

根据 JS 中的这一行,您没有发送 msgSent 属性:

var dataString = 'content='+ textcontent;

我只能看到您可以通过 $_POST['content'] 使用的内容

试试这个:

var dataString = 'content='+ textcontent + '&msgSent' + '<?php echo $_POST['msgSent']; ?>';

关于javascript - 无法使用 AJAX 识别 HTML 表单元素的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46862520/

相关文章:

javascript - node.js index.html javascript函数未定义

php - 修改资源模型中的函数以在 Magento 中执行 'equals' 搜索

php - 如何在 CodeIgniter PHP 中捕获数据库错误

Google 搜索中的 PHP file_get_contents?

javascript - JavaScript 包装器对象创建是轻量级的吗?

jquery - 尝试使用 GET 请求调用方法\u0027GetNextImage\u0027,这是不允许的

javascript - javascript 回调只是作为函数调用中的参数发送的匿名函数吗?

javascript - 如何为 Bokeh 回调过滤器的一部分编写 Javascript 代码,以便我可以按数字和类别进行过滤?

javascript - AngularJS $注入(inject)器:unpr Unknown Provider Error

jquery - 使用 Jquery 进行时间验证