Jquery Ajax将空的textarea文本提交到数据库

时间:2017-07-12 13:21:08

标签: php jquery mysql ajax forms

我正在尝试将textarea的文本插入我的MySQL数据库,但我的问题是当我的程序尝试向数据库提交空文本时。

当我删除e.preventDefault();时,它工作得很好。我做错了什么?

var frm = $('#newsform');

frm.submit(function (e) {

    e.preventDefault();

    $.ajax({
        type: frm.attr('method'),
        url: frm.attr('action'),
        data: frm.serialize(),
        success: function (data) {
            alert('Submission was successful.');
            console.log(data);
            //clear all fields
            $('#newsform').trigger("reset");
        },
        error: function (data) {
            console.log('An error occurred.');
            console.log(data);
            //clear all fields
            $('#newsform').trigger("reset");
        },
    });        
});

<form action="updatenews.php" method="POST" id="newsform">
    <textarea name="news"></textarea>
    <input type="submit">
</form>

1 个答案:

答案 0 :(得分:-3)

var frm = $('#newsform'); 

frm.submit(function (e) {

    e.preventDefault();

    $.ajax({
        type: 'post',
        url: frm.attr('action'),
        data: {'frm':frm},
        success: function (data) {
            alert('Submission was successful.');
            console.log(data);
            //clear all fields
                $('#newsform').trigger("reset");
        },
        error: function (data) {
            console.log('An error occurred.');
            console.log(data);
            //clear all fields
                $('#newsform').trigger("reset");
        },
    });


});