簡體   English   中英

無法使用AJAX識別HTML表單元素的值

[英]Unable to identify value of HTML form element using AJAX

我正在嘗試使用AJAX在我的項目中實現消息系統,但是由於我是AJAX的新手,所以遇到了一些問題。 下面的代碼能夠將MsgMain.php文件中的輸入數據完美地發送到MsgInsert.php 但是,當我嘗試從MsgMain.php上的MsgInsert.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;
    });
});

僅用於HTML表單的MsgMain.php文件代碼

<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;

我只能看到content ,你將能夠通過使用$_POST['content']

嘗試這個:

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

您必須根據msgSend的實現傳遞msgSend參數, MsgInsert.php所示:

$("#msgBtn").click(function () {
    var textcontent = $("#msgInput").val();

    if (textcontent == '') {
        alert("Enter some text..");
        $("#msgInput").focus();
    }
    else {
        var dataString = 'content=' + textcontent + '&msgSend=1';
        $.ajax({...});
    }
});

在將用戶生成的內容保存到數據庫中以避免sql注入時,請始終考慮通過准備語句轉義您的內容!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM