输入发送空消息

时间:2013-07-26 00:38:29

标签: php javascript jquery chat

我正在为我的项目进行实时聊天,我需要这个脚本来识别这个:

  • 如果 clientmsg 为空,则会调出错误并且不会发布错误 消息。
  • 如果 clientmsg 有超过3个或4个字符(字母) - 它会发布消息。

代码:

$("#submitmsg").click(function(){   
    var clientmsg = $("#usermsg").val();
    $.post("post3.php", {text: clientmsg});             
    $("#usermsg").attr("value", "");
    return false;
});

我无法弄清楚这是如何运作的。

在这里你可以看到它的整个代码。

的index.php

<?php
/*****************************
    File: index.php
    Written by: exZerry development crew
******************************/
session_start();

if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
    echo "";
} else {
    header ("Location: login.php");
}
require('includes/config.php');
echo $sOutput;

if(isset($_POST['enter'])){
    if($_POST['username'] != ""){
        $_SESSION['username'] = stripslashes(htmlspecialchars($_POST['username']));
    }
    else{
        echo '<span class="error">Please type in a name</span>';
    }
}
?>
<style type="text/css">

input { 
    font: 12px;
}
.loginbox {
    background-image: url(bar.png);
    width: 100%;
    height: 100%;
    margin-top: -38;
}
#loginform { 
    font:16px dinpro;
    color: #ffffff;
    margin-top: 100;
}
#hedthe {
    background-image: url(spacer.png);
    font-family: bank-gt, sans-serif;
    font-size: 34;
    width: 830;
    height: 34;
    font-weight: normal;
}
#thereis {
    magrin-top: -20;
}
#chatbox {
    text-align: justify;
    height: 70%;
    width: 100%;
    overflow: auto;
    opacity: 0.91;
}
#usermsg {
    width: 100%;
    font-family: Play, sans-serif;
    font-size: 14;
    font-weight: normal;
    color: #ffffff;
    border: 0px solid black;
    text-indent: 5px;
    height: 32;
    margin-top: 5px;
    border: 0px solid black;
    color: #ffffff;
    background-image: url(chatinp.png);
} 
input {
    opacity: 0.7; 
}
#submitmsg { 
    background-color: #48513e;
    width: 0;
    height: 0;
    font-size: 20;
    font-weight: normal;
    color: #ffffff;
    text-align: left;
    border: 0px solid black;
} 
.error { 
    color: #000000; 
} 
#menu {
    margin-right: 0; 
} 
.welcome {
    float:left; 
} 
.logout { 
    float:right; 
} 
.msgln { 
    margin-right: 0;
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="modules/chatbox.js"></script>

<!-- Actual chat -->

<div id="chatbox">
<?php
    if(file_exists("tmp/log.html") && filesize("tmp/log.html") > 0){
    $handle = fopen("tmp/log.html", "r");
    $contents = fread($handle, filesize("tmp/log.html"));
    fclose($handle);

    echo $contents;
    }
?>
</div>

<form name="message" action="" id="sender" class="sender">
    <input name="usermsg" id="usermsg" class="required" type="text" placeholder="Your message here" maxlength="60"/>
    <input name="submitmsg" type="submit" id="submitmsg" width="0" height="0"/>
</form>

chatbox.js文件

// jQuery Document
$(document).ready(function(){
    //If user submits the form
    $("#submitmsg").click(function(){   
        var clientmsg = $("#usermsg").val();
        $.post("post3.php", {text: clientmsg});             
        $("#usermsg").attr("value", "");
        return false;
    });


    //Load the file containing the chat log
    function loadLog(){     
        var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
        $.ajax({
            url: "tmp/log.html",
            cache: false,
            success: function(html){        
                $("#chatbox").html(html); //Insert chat log into the #chatbox div               
                var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
                if(newscrollHeight > oldscrollHeight){
                    $("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
                }               
            },
        });
    }
    setInterval (loadLog, 2000);    //Reload file every 3 seconds

});

post3.php文件

<?
session_start();

if(isset($_SESSION['username'])){
    $text = $_POST['text'];

    $fp = fopen("tmp/log.html", 'a');
    fwrite($fp, "<div class='msgln'> <b>".$_SESSION['username']."</b>: ".stripslashes(htmlspecialchars($text))."<br></div>");
    fclose($fp);
}
?>

这是完整的js + php聊天。

2 个答案:

答案 0 :(得分:1)

喜欢这个吗?

$("#submitmsg").click(function(){   
    var clientmsg = $.trim($("#usermsg").val());
    if(clientmsg.length >= 3){
        $.post("post3.php", {text: clientmsg});             
        $("#usermsg").val("");
    }else{
        alert('error');
    }
    return false;
});

答案 1 :(得分:0)

$("#submitmsg").click(function(){   
    var clientmsg = $("#usermsg").val();

    // check if the length of the string is greater than 3 letters
    if(clientmsg.length > 3)
        $.post("post3.php", {text: clientmsg});  
    else
        alert("Error");

    $("#usermsg").attr("value", "");
    return false;
});