R
R
RoffDaniel2019-07-21 10:49:02
PHP
RoffDaniel, 2019-07-21 10:49:02

Why is the php script buggy when checking the CKEDITOR editor for emptiness?

Good day to all. Literally yesterday, when I was doing the output of form errors, I came across such a thing ...
I check for emptiness all fields that require filling, errors are displayed in the bodal window via ajax. If you check each field gradually, then the check on the field that works as CKEditor is checked twice. Why is that?
Here is the code for everything you need:

<form class="md-form" enctype="multipart/form-data" action="/engine/queryforms/postnews.php" method="post" id="NewsCreatorForm">
        <div class="row">
            <div class="col-lg-8">
                <div class="card mb-4 post-title-panel">
                    <div class="card-body">
                        <div class="file-field mb-4">
                            <div class="btn ptadmin-color white-text btn-sm float-left disabled">
                                <span><i class="fas fa-image mr-2" aria-hidden="true"></i>Изображение новости</span>
                                <input type="file" name="imgnews" id="imgnews">
                            </div>
                                <div class="file-path-wrapper">
                                <input class="file-path" type="text" name="imgnews" id="imgnews" placeholder="Загрузите изображение" disabled>
                            </div>
                        </div>
                        <div class="md-form mt-1 mb-0">
                            <input type="text" class="form-control" name="newstitle" id="newstitle">
                            <label class="form-check-label" for="newstitle">Тема новости</label>
                        </div>
                    </div>
                </div>
                <div class="card mb-4">
                    <textarea name="newscreator" id="newscreator" rows="10" cols="80"></textarea>
                </div>
                <div class="card mb-4">
                    <div class="card-body">
                        <div class="md-form mb-0 mt-2">
                            <textarea type="text" class="md-textarea form-control" rows="3" name="script_text" id="script_text"></textarea>
                            <label class="form-check-label" for="script_text">Текст для скрипта</label>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-lg-4">
                <div class="card card-cascade narrower mb-5">
                    <div class="view view-cascade gradient-card-header ptadmin-color">
                        <h4 class="font-weight-500 mb-0">Информация</h4>
                    </div>
                    <div class="card-body card-body-cascade pt-3 pb-3 text-center">
                        <p><i class="fas fa-user mr-1" aria-hidden="true"></i> Автор: <strong>Pavel</strong></p>
                        <div class="text-center">
                            <button class="btn ptadmin-color white-text" type="submit" name="createnews">Опубликовать</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>


$date = strtotime(date('Y-m-d H.i.s'));
$author = 'Pavel_Garson';
$data = $_POST;
$errorContainer = array();
$arrayFields = array('newstitle' => $data['newstitle'], 'newscreator' => $data['newscreator'], 'script_text' => $data['script_text']);
if (empty($data['imgnews'])) {
    if (!empty($data['newstitle'])) {
        $url = str2url($data['newstitle']);
        if (!empty($data['newscreator'])) {
            if (!empty($data['script_text'])) {
                mysqli_query($query, "INSERT INTO `news` (date, author, title, url, site_text, script_text) VALUES ('{$date}', '{$author}', '{$data['newstitle']}', '{$url}', '{$data['newscreator']}', '{$data['script_text']}') ");
            } else {
                $errorContainer['script_text'] = 'Введите текст для версии скрипта!';
            }
        } else {
            $errorContainer['newscreator'] = 'Введите текст для версии сайта!';
        }
    } else {
        $errorContainer['newstitle'] = 'Введите название новости!';
    }
}

if(empty($errorContainer))
{
    echo json_encode(array('result' => 'success'));
}
else
{
    echo json_encode(array('result' => 'error', 'texterror' => $errorContainer));
}


$('#NewsCreatorForm').submit(function(){
        var newstitle = $('#newstitle').val();
        var newscreator = $('#newscreator').val();
        var script_text = $('#script_text').val();

        $.ajax({
            type: "POST",
            url: "/engine/queryforms/postnews.php",
            data: {
                'newstitle': newstitle,
                'newscreator': newscreator,
                'script_text': script_text
            },
            dataType: "json",
            success: function(data)
            {
                if(data.result == 'success') {
                    $('#NewsCreatorSuccessModal').modal('show');
                    setTimeout(function(){document.location="/admin/news/"}, 1000);
                } else {
                    for(var errorField in data.texterror){
                        document.getElementById('errortext').innerHTML = data.texterror[errorField];
                        $('#NewsCreatorErrorModal').modal('show');
                    }
                }
            }
        });
        return false;
    });


Well, the modal windows themselves:
<div class="modal fade" id="NewsCreatorSuccessModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
             aria-hidden="true">
            <div class="modal-dialog modal-frame modal-top modal-notify modal-success" role="document">
                <div class="modal-content">
                    <div class="modal-body">
                        <div class="text-center">
                            <i class="fas fa-check fa-4x mb-3 animated rotateIn"></i>
                            <p>Новость успешно опубликована!</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal fade" id="NewsCreatorErrorModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
             aria-hidden="true">
            <div class="modal-dialog modal-frame modal-top modal-notify modal-danger" role="document">
                <div class="modal-content">
                    <div class="modal-body">
                        <div class="text-center">
                            <i class="fas fa-times fa-4x mb-3 animated rotateIn"></i>
                            <p><strong><span class="h5-responsive" id="errortext"></span></strong></p>
                        </div>
                    </div>
                </div>
            </div>
        </div>


Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xmoonlight, 2019-07-21
@RoffDaniel

$('#NewsCreatorForm').submit(function(e){
e.preventDefault();
.......
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question