JavaScript $ .GET没有通过

时间:2019-04-12 12:49:08

标签: javascript php mysql

我正在构建一个非常简单的聊天应用程序,没有什么花哨的东西只是让多个用户同时聊天的一种方式。

我遇到的问题是,虽然我可以读取和显示数据库中的消息,但不会插入任何新消息。

我正在使用JavaScript传递带有消息和用户名的$.Get。我已经检查了控制台,没有错误显示,但是当我使用POSTMAN进行测试时,我有一个undefined index: textundefined index: username

有人可以帮助我朝正确的方向解决吗?

chat.php

<div class="wrapper">
            <p id='chat-user'> </p>
            <div id='chat-area'></div>
            <form id='send-message-area'>
                <textarea name="the-textarea" id="the-textarea" maxlength="150" placeholder="Start Typing..."autofocus></textarea>              
            </form>
            <button id='chatSend' class="btn btn-info" type="submit">Post New Message</button>
            <div id="the-count">
                <span id="current">0</span>
                <span id="maximum">/ 150</span>
            </div>
        </div>
var name = prompt("Enter your name:", "Guest");
            if (!name || name === ' ') {
                name = "Guest";
            }
            window.onload = function () {
                document.getElementById("chat-user").innerHTML = "Your are: " + name;
            };

            $(document).ready(function () {
                var chatInterval = 250;                       
                var $chatOutput = $("#chat-area");
                var $chatInput = $("#the-textarea");
                var $chatSend = $("#chatSend");

                function sendMessage() {               
                    var chatInputString = $chatInput.val();

                    $.GET("testProcess.php", {
                        username: name,
                        text: chatInputString
                    });


                    retrieveMessages();
                }

process.php

<?php

error_reporting(E_ALL);
include ("connection.php");


$username = substr($_GET["username"], 0, 32);
$text = substr($_GET["text"], 0, 150);

$nameEscaped = htmlentities(mysqli_real_escape_string($conn, $username)); 
$textEscaped = htmlentities(mysqli_real_escape_string($conn, $text)); 
$timestamp = date("Y-m-d H:i:s");


$insertMessage = "INSERT INTO message (messageID, messageBody, timeSent, nickname, sent) VALUES ('', '$textEscaped', '$timestamp', '$nameEscaped')";
$result = mysqli_query($conn, $insertMessage);

Messages being displayed

Error Messages

1 个答案:

答案 0 :(得分:0)

您的邮递员请求错误,如图所示 您正在传递带有值send的参数GET,但是在您的php脚本中,您已经有$ _GET [“ username”]和$ _GET [“ text”]方法GET已在图片的左侧设置,因此您不需要发送它,您想要发送的是参数用户名和文本,因此url看起来像这样

celery worker -A project.celery -l info  # Default queue worker
celery worker -A project.celery -l info -Q pipedrivequeue  # pipedrivequeue worker
celery worker -A project.celery -l info -Q testqueue  # testqueue worker

对于您的JavaScript代码,get的jquery方法是$ .get而不是$ .GET,并且您缺少处理表单提交的方法,因为您的Submit按钮不在表单之外,因此这是一种正确的方法会是

 protected void Button1_Click(object sender, EventArgs e)
    {


        int caseNo1 = Convert.ToInt32(DateTime.Now.ToString("yyyy"));
        int caseNo2 = Convert.ToInt32(DateTime.Now.ToString("MM"));
        int caseNo3 = 0;

        int currentDD=Convert.ToInt32(DateTime.Now.ToString("dd"));

        if (currentDD != 1)
        {
            caseNo3++;

            lbl_display.Text = caseNo1.ToString() + caseNo2.ToString("00") + caseNo3.ToString("000");
        }