简体   繁体   English

如何使用$ .ajax将变量传递给alert()?

[英]How can I pass a variable to alert() with $.ajax?

I want to let the user confirm to deletion of a record by letting them click on the button and the button sends the value Yes to a $_POST request so I can delete the record with PHP, instead it's showing me the javascript file that contains the function. 我想让用户通过让他们点击按钮来确认删除记录,然后按钮将值Yes发送给$ _POST请求,这样我就可以用PHP删除记录,而不是它向我显示包含该记录的javascript文件功能。

What am I doing wrong, I've been tinkering and searching for 3 hours straight, while in the back of my mind I know it's a easy fix. 我做错了什么,我一直在修补和寻找3个小时,而在我的脑海里,我知道这是一个简单的解决方案。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

If you think there is a smarter way to be doing this, please let me know. 如果您认为有更聪明的方法可以做到这一点,请告诉我。

$(function () {

    $("#DeleteConfirm").dialog({

        buttons: {
                 "Confirm": function() {
                    var DeleteConfirmation = "Yes";                 
                    $.ajax({
                        type: "POST",
                        url: "index.php",
                        data: DeleteConfirmation,
                        success: function(DeleteConfirmation){
                            alert(DeleteConfirmation);
                        },
                        dataType: "text"
                    });

                 },
                 "Cancel": function() {
                     $(this).dialog("close");
                 }
             }
    });

});

You need to assign the YES value to a property name: 您需要将YES值分配给属性名称:

data: { "confirmed": DeleteConfirmation},

Then in your PHP you can ready this using $_POST["confirmed"] . 然后在你的PHP中你可以使用$_POST["confirmed"]来准备这个。

This does seem a little redundant however, as the user has already clicked the button to confirm the deletion, so there seems little value in sending the value in the POST request. 这似乎有点多余,因为用户已经单击按钮确认删除,因此在POST请求中发送值似乎没什么价值。 Sending the id of the item to delete would make more sense. 发送要删除的项目的id会更有意义。

Your ajax is misformed, try: 你的ajax错了,试试:

                $.ajax({
                    type: "POST",
                    url: "index.php",
                    data: {answer: DeleteConfirmation }, //what you send to the php
                    success: function(data){ //answer from your php
                        alert(data);
                    },
                    dataType: "text"
                });

             },

Javascript: 使用Javascript:

if( confirm('Are you sure?')){
    /*proceed?*/
}

That is standard javascript. 那是标准的javascript。 It wraps your AJAX call within the braces and if the user clicks 'yes' - the ajax call will execute. 它将您的AJAX调用包装在大括号内,如果用户单击“是”,则将执行ajax调用。 If he click 'no', then nothing happens. 如果他点击“否”,则没有任何反应。 The confirm() prompt pops up an alert on screen with buttons. confirm()提示会在屏幕上弹出一个带按钮的警报。 The resulted is captured by the if . 结果由if捕获。 You can also capture the result of that: var res=confirm('are you sure?'); 您还可以捕获结果: var res=confirm('are you sure?'); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM