dongsou0083 2016-06-15 21:33
浏览 17

AJAX数据,了解这实际上是做什么的

I found some old code which I am trying to understand and unfortunately I did not leave comments which leads to my difficulties. Even with the jQuery documentation, I can't wrap my head around it; I really have looked, but I still don't understand. This program is basically a chat where you type your name and your message and click submit. Then it displays your message in a chat box similar to Facebook. Here is the working code:

My JS

$(document).ready(function() {
    $('#submit').on('click', function() {
        var name = $('#name').val();
        var shout = $('#shout').val();
        var outText = 'name=' + name + '&shout=' + shout;

        if (name == '' || shout == '') {
            alert("Please fill in your name and shout");
        } else {
            $.ajax({
                type: "POST",
                url: "database.php",
                data: outText,
                cache: false,
                success: function(html) {
                    $('#shouts ul').prepend(html);
                }
            });
        }

        return false;
    });
});

and my PHP

<?php

$connection = mysqli_connect('localhost','root','','muntasirshoutbox');

if(isset($_POST['name']) && isset($_POST['shout']))
{

    $name = mysqli_real_escape_string($connection,$_POST['name']);
    $shout = mysqli_real_escape_string($connection,$_POST['shout']);
    $query = "INSERT INTO shouts(name,shout) ";
    $query.= "VALUES ('$name','$shout')";
    $result = mysqli_query($connection,$query);

    if(!$result)
    {
        echo "doesent work".mysqli_error($connection);
    }
    else
    {
        echo '<li>'.$name.': '.$shout.'</li>';
    } 


}
?>

So I am confused as to what outText is actuallly doing, the reason I am confused is because, I was under the impression that my PHP was printing the text inside the chat box when I do this echo '<li>'.$name.': '.$shout.'</li>';, but after experimenting with outText, I noticed that if I change some stuff with outText the message I type in wont appear at all in the chat box, or it will appear with whatever string I added to it. What exactly is this doing?!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 我要一个分身加定位两个功能的安卓app
    • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
    • ¥15 IAR程序莫名变量多重定义
    • ¥15 (标签-UDP|关键词-client)
    • ¥15 关于库卡officelite无法与虚拟机通讯的问题
    • ¥15 目标检测项目无法读取视频
    • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
    • ¥100 求采集电商背景音乐的方法
    • ¥15 数学建模竞赛求指导帮助
    • ¥15 STM32控制MAX7219问题求解答