Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ajax调用后文本框未清除_Javascript_Php_Jquery_Ajax_Json - Fatal编程技术网

Javascript ajax调用后文本框未清除

Javascript ajax调用后文本框未清除,javascript,php,jquery,ajax,json,Javascript,Php,Jquery,Ajax,Json,我在我的网站上有一个聊天室,在这里,用户将消息键入文本框,当他们按下enter(或send)按钮时,jQuery从文本框中获取数据,禁用文本框,将数据发送到文件send.php,然后在该文件中处理并放入数据库,然后,一旦文本框成功地通过PHP脚本运行,它就应该清除并取消对文本框的禁用。发生的情况是,数据正在提交,并通过脚本运行(成功地将数据发送到数据库),但jQuery没有清除和取消禁用文本框。 有人能给我解释一下怎么了吗 jQuery: $('#formSend').on('submit',

我在我的网站上有一个聊天室,在这里,用户将消息键入文本框,当他们按下enter(或send)按钮时,jQuery从文本框中获取数据,禁用文本框,将数据发送到文件
send.php
,然后在该文件中处理并放入数据库,然后,一旦文本框成功地通过PHP脚本运行,它就应该清除并取消对文本框的禁用。发生的情况是,数据正在提交,并通过脚本运行(成功地将数据发送到数据库),但jQuery没有清除和取消禁用文本框。 有人能给我解释一下怎么了吗

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();
    }
});
<?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();
}
?>
send.php

$('#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();
    }
});
<?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();
}
?>

这是表格,如果需要的话

<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>

您是否尝试过以下方法:

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

我看到您正在刷新页面,这可能会使表单自动完成到上一个值,请尝试以下操作:

  <form autocomplete="off" 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>

我认为问题在于,如果用户未被禁止且消息已成功存储,则您尚未发送任何响应

如果没有提醒“成功”,那么这就是问题所在

试试这个

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

如果您不想重新加载页面(实际上应该是这样,如果您不阻止在ajax回调by event.preventDefault()中触发submit事件),您必须通过执行类似$('#formSend').reset()的操作来重置表单

此外,没有disabled='false',您需要删除属性:$('.expand')。removeAttr('disabled')


顺便说一句,保存选择器是一个好习惯,您需要多次;)

成功回调被触发了吗?正如@A.Wolff所问的,你能看到警告消息“成功”吗?如果你只是想重新加载页面,为什么需要清除该字段?如果用户未被禁止,看起来你没有从服务器返回JSON。我没有收到
警告(“成功”),这是我检查函数是否正在执行的方式。这将如何影响它是否清除输入字段?即使禁用了textarea,也可以设置其值,因此我认为将disabled设置为false没有任何意义。HTML规范规定,只要设置了attribute disabled,输入就确实被禁用。我同意,但这并不能解决清除输入的问题,不是吗?令人遗憾的是,通过指出脚本中的错误,即使它不是完整的解决方案,您也会投反对票:-(我只是在PHP脚本发现该用户被禁止后才刷新。是的,因为他正在将数据类型设置为JSON,而服务器没有返回JSON,这就是我的看法。我使用的PHP版本是5.4,因此最终不被接受。您可以在try and catch saperately中回显JSON。OP使用的是
$('.expand')。prop(“disabled”,false);
将任何布尔属性设置为禁用的首选方法是什么