dongshen2903 2015-04-13 15:33
浏览 69

ajax调用后文本框不清除

I have this chat room on my website, where the user types their message into a textbox and when they press enter (or the send button), jQuery takes the data from the textbox, disables the textbox, sends the data to the file send.php where it is then processed and put into the database, and then it should clear and undisable the textbox once it successfully runs through the PHP script. What is happening is the data is being submitted and it runs through the script (sends the data to the database successfully) but the jQuery is not clearing and undisabling the textbox. Can someone explain to me what is wrong?

jQuery:

$('#formSend').on('submit', function (e) {

    e.preventDefault();

    var textArea = document.getElementById('styled');

    if( textArea.value != "" ) {

        var formData = $('form').serialize();
        $('.expand').prop("disabled", true)

        $.ajax({
            type: 'post',
            url: 'send.php',
            data: formData,
            dataType: "json",
            success: function (formData) { //put data in parentheses when coming back
                alert("Success");
                $(".expand").val('');
                $('.expand').prop("disabled", false);

                if( formData["banned"] == 1 ) {
                    var rel = confirm("You have been banned.");
                    if (rel) {
                        location.reload();
                    }
                    else {
                        location.reload();
                    }
                }
            }
        });
    }
    else {
        alert("Your message must be longer than 1 (one) character.");
        $('#styled').focus();
    }
});

send.php:

<?php

include("../config.php");
session_start();


$msg           = strip_tags( $_POST['msg'], '<b></b>' );

if( $msg == "" ) {
 exit("There is no message.");
}
$username      = $_SESSION['USER'];
$date          = new DateTime();
$formattedDate = $date->format('Y-m-d H:i:s');

$stmt = $db->prepare("SELECT id, username FROM users WHERE username = :username");

$stmt->execute(array(":username" => $username));

$row = $stmt->fetch();

$userID = $row['id'];

$checkBanned = $db->prepare('SELECT banned FROM users WHERE username = :username');
$checkBanned->execute(array(
            ':username' => $username
        ));
$banned = $checkBanned->fetch();

if( $banned['banned'] == "yes" ) {
    $return = array('banned' => 1);
    echo json_encode($return);
    exit;
}

try {

    $stmt = $db->prepare('INSERT INTO `chat-messages` (userID,msg,date) VALUES (:userID, :msg, :date)');
    $stmt->execute(array(
        ':userID' => $userID,
        ':msg' => $msg,
        ':date' => $formattedDate
    ));
}
catch(PDOException $e) {
    echo $e->getMessage();
}
?>

Here is the form too, if needed.

<form action="send.php" method="post" name="formSend" id="formSend" />
     <textarea id="styled" class="expand" name="msg" placeholder="Your Message Here" onfocus:"setbg(\'#e5fff3\');"required></textarea>
     <input type="hidden" name="banned" value="no" />
     <input type="submit" name="submit" value="Send" class="send" />

  </form>
  • 写回答

4条回答 默认 最新

  • douluoyou9876 2015-04-13 15:39
    关注

    Have you tried something like:

    $('.expand').removeAttr("disabled");
    
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计