【问题标题】:data submit even when cancel button is pressed in sweetalert2即使在 sweetalert2 中按下取消按钮,也提交数据
【发布时间】:2018-10-19 14:55:42
【问题描述】:

按下确认按钮后,我能够发送数据。
但是,当 按下取消按钮时,sweetalert2 会显示为 成功插入数据。
后端显示为空字符串。(在数据库表中)
当我按下取消按钮时如何验证,而不是向后端发送数据。


Javascript 函数

    function inputPass(complaintID) { // complaint id pass is ok.
    swal({
        text: 'Input comment message',
        input: 'textarea',
        showCancelButton: true,
    }).then(function(sample_text) { 
        console.log(sample_text);
        if(sample_text === '') { // problem is here.
            swal({
                type: 'warning',
                html: 'cannot proceed without input'
            });
        } else {
            console.log(sample_text);
            $.ajax({
                type: "POST",
                url: "../ajax/ajax_active_deact.php?type=complaint_answered",
                data: {complaintID: complaintID, sampleText: sample_text}
            }).done(function (res) {
                if(!res) {
                    swal({
                        type: 'error',
                        html: 'insert the valid text'
                    });
                } else {
                    swal({
                        title: 'done',
                        text: 'all right',
                        type: 'success',
                        allowOutsideClick: false,
                        confirmButtonText: 'Ok'
                    });
                }
            });
        }
    });
}

php ajax 代码

function complaint_answered() {
    include_once('../backend/ConsumerComplaint.php');
    $con_complaint = new ConsumerComplaint();
    $res = $con_complaint>mark_as_answered($_POST['complaintID'],$_POST['sampleText']);
    echo $res;
}

这是我的类函数

    function mark_as_answered($id, $comment) {
    //var_dump($comment);
    $val = $comment['value']; // $comment is a associative array, with the key of 'value'
    $sql = "UPDATE c_consumer_complaint SET `status` = 'answered', `update` = '$val' 
            WHERE  complaint_id = '$id' ";
    $res = $this->conn->query($sql);
    return $res;
}

  • 当我在 chrome 的网络面板中按下取消按钮时的图像

  • 控制台图像

  • 在 chrome 中发布数据的图像

我是开发新手,不知道如何解决这个问题。 请任何人都可以告诉我我在这里做错了什么。 谢谢!

【问题讨论】:

    标签: javascript php ajax sweetalert2


    【解决方案1】:

    只有在用户点击 Ok 时,您才会得到 result.value,因此您可以检查是否有值,如果它为空,则显示错误消息。如果没有值,则不会发生任何事情。

    片段:

        swal({
            text: 'Input comment message',
            input: 'textarea',
            showCancelButton: true,
        }).then(function(result) {
            if(result.value) {
                $.ajax({
                    type: "POST",
                    url: "../ajax/ajax_active_deact.php?type=complaint_answered",
                    data: {complaintID: complaintID, sampleText: result.value}
                }).done(function (res) {
                    if(!res) {
                        swal({
                            type: 'error',
                            html: 'insert the valid text'
                        });
                    } else {
                        swal({
                            title: 'done',
                            text: 'all right',
                            type: 'success',
                            allowOutsideClick: false,
                            confirmButtonText: 'Ok'
                        });
                    }
                });
            } else if (result.value === "") {
                swal({
                    type: 'warning',
                    html: 'cannot proceed without input'
                });
            }
        });
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@7.28.7/dist/sweetalert2.all.min.js"></script>

    你的班级:

    在您的 php ajax 代码中,您传递的不是数组而是字符串 $_POST['sampleText'],因此 $comment['value'] 将不包含文本。

    function mark_as_answered($id, $comment) {
        //var_dump($comment);
        $val = $comment;
        $sql = "UPDATE c_consumer_complaint SET `status` = 'answered', `update` = '$val' 
                WHERE  complaint_id = '$id' ";
        $res = $this->conn->query($sql);
        return $res;
    } 
    

    PS:请自学SQL-Injection,以免人们将有害代码注入您的 SQL 查询。

    【讨论】:

    • 代码有效,但仍有一些问题。当我在文本区域输入一些文本时,在数据库表中只显示“一个字符”....即使我的表“更新”字段数据类型是“文本”类型。
    • 我更新了我的答案,我认为你的mark_as_answered() 方法有误。
    • -> 这是我从后端得到的错误警告:Illegal string offset 'value' in C:\wamp64\www\PDMS\backend\ConsumerComplaint.php 在线151
    • 你拯救了我的一天哦!男人。非常感谢你。它就像一个魅力。
    • 我在答案中添加了关于 sql-injection 的部分。
    【解决方案2】:

    看起来示例文本总是设置为一个数组。我会尝试更改 if 语句

    if(sample_text === '') { // problem is here.
                swal({
                    type: 'warning',
                    html: 'cannot proceed without input'
                });
            } else {
                console.log(sample_text);
    

    类似

    if(sample_text['dismiss'] == 'cancel') { // problem is here.
            swal({
                type: 'warning',
                html: 'cannot proceed without input'
            });
        } else {
            console.log(sample_text);
    

    【讨论】:

    • 仍然有同样的问题。即使我更改为您的代码建议,数据也会传递。
    猜你喜欢
    • 1970-01-01
    • 2021-05-25
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    相关资源
    最近更新 更多