2013-07-26 8 views
0

Делаю живой болтовни для моего проекта, и мне нужно, чтобы этот скрипт признать это:Input посылает пустое сообщение

  • Если 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 болтовня.

+0

если вы сообщаете/consol clientmsg, что он показывает? – Muath

+0

всплывает небольшое окно, в котором отображается сообщение - ошибка – exZerry

ответ

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; 
}); 
+0

спасибо всем =) – exZerry

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; 
}); 
Смежные вопросы