简体   繁体   English

jQuery .post参数未发送

[英]jQuery .post params not sending

I'm having an issue that I have been trying to research and fix for hours now and I am at my wits-end in not understanding why it's not working correctly. 我遇到了一个问题,我已经尝试研究和修复了几个小时,而我不知所措,无法理解为什么它无法正常工作。

I have a jQuery dialog (via jQuery UI) pop-up when someone wants to delete an item (by clicking on a delete icon image and calling a JS function with a supplied "id" integer). 当有人要删除项目时(通过单击删除图标图像并使用提供的“ id”整数调用JS函数),我会弹出一个jQuery对话框(通过jQuery UI)。 The pop-up dialog is working properly and comes up with all the available information and button options. 弹出对话框正常工作,并提供所有可用信息和按钮选项。 Here's the code for that: 这是该代码:

function removeItem(id) {
    var rid = id;
    $("#dialog").dialog({
        title: "Removal Confirmation",
        resizable: false,
        height: 200,
        modal: true,
        show: {
            effect: "blind",
            duration: 800
        },
        buttons: {
            "Confirm": function() {
                $.post("includes/inc_details.php", {
                    "type":"remove",
                    "id":rid
                });
                window.location.reload();
                $(this).dialog("close");
            },
            Cancel: function() {
                $(this).dialog("close");
            }
        }
    });
}

When the "Confirm" button within the dialog is clicked, it's suppose to .post to a php file for further processing (code for that portion is below), and when complete will reload the browser window and then close the dialog. 单击对话框中的“确认”按钮时,假定将.post到php文件中以进行进一步处理(该部分的代码在下面),完成后将重新加载浏览器窗口,然后关闭对话框。

However, what is routinely happening is the page just looks like it's reloading and then the dialog closes but the item doesn't get deleted. 但是,通常发生的情况是页面看上去就像它正在重新加载,然后对话框关闭,但该项目不会被删除。 I've watch the processing via chromeDev tools and it appears that when the .post php file is called the two parameters are not sent with it. 我看过通过chromeDev工具进行的处理,看来当.post php文件被调用时,两个参数并未随其一起发送。 (I've used similar .posts other places - albeit without a jQuery UI dialog box - with no issues). (我在其他地方使用过类似的.posts-尽管没有jQuery UI对话框-没有问题)。

HELP! 救命!

PHP code being called via the .post method: 通过.post方法调用的PHP代码:

 $sql = "select * from shopping_cart WHERE id=$_POST[id] LIMIT 1";
            $result = mysql_query($sql);
            $row = mysql_fetch_assoc($result);

            if ($row["Type"] == "CouponUsed") {
                $sql_delete_free_item = "delete from shopping_cart where ProductID=".$_SESSION["freeProductId"]." and SessionID='".session_id()."'";
                $query = mysql_query($sql_delete_free_item);
                unset($_SESSION["freeProductId"]);
            }
$sql_id = "SELECT ProductID FROM shopping_cart WHERE id=$_POST[id] LIMIT 1";
    $result_id = mysql_query($sql_id);
    $row_id = mysql_fetch_assoc($result_id);
    $sql_remove = "DELETE FROM shopping_cart WHERE id=$_POST[id] LIMIT 1";
if (mysql_query($sql_remove)) {
        $sql_remove2 = "DELETE FROM shopping_cart_single WHERE singleid=$_POST[id]";
        mysql_query($sql_remove2);
    }
mysql_close($conn);
    exit();

Thank you, in advance! 先感谢您!

Just including the relevant part. 仅包括相关部分。

"Confirm": function() {
            $.post("includes/inc_details.php", {
                "type":"remove",
                "id":rid
            }).then(function() {
                window.location.reload();
                $(this).dialog("close");
            });
        },

Remember what the A in AJAX stands for - asynchronous! 记住AJAX中的A代表-异步!

EDIT: To explain what was going on, here's how the browser reacts. 编辑:要解释发生了什么,这是浏览器的反应。

  1. POST request queued up for submission. POST请求排队等待提交。 Asynchronous, so not immediate . 异步的,所以不是立即的 Requests take much longer than other user actions, so it won't run it and block the UI thread. 请求比其他用户操作花费更长的时间,因此它不会运行并阻塞UI线程。
  2. Window told to reload. 窗口告诉重新加载。 The browser cancels all pending requests - iframes, images, ajax requests , and begins loading a new page. 浏览器取消所有待处理的请求-iframe,图像, ajax请求 ,然后开始加载新页面。
  3. (If it hadn't reloaded, then once the javascript stack ended, it would actually send the request) (如果尚未重新加载,那么一旦javascript堆栈结束,它将实际发送请求)

select * from shopping_cart WHERE id='".$_POST[id]."' LIMIT 1 从shopping_cart中选择* WHERE id ='“。$ _ POST [id]。”'LIMIT 1

Your problem is that you didn't add double quote and dot around the $_POST[id], 您的问题是您没有在$ _POST [id]周围添加双引号和点,

and also your $sql_remove, please double-check all your $_POST[id] is '".$_POST[id]."' 以及您的$ sql_remove,请仔细检查您所有的$ _POST [id]是否为““。$ _ POST [id]”。

When the "Confirm" button within the dialog is clicked, it's suppose to .post to a php file for further processing (code for that portion is below), and when complete will reload the browser window and then close the dialog. 单击对话框中的“确认”按钮时,假定将.post到php文件中以进行进一步处理(该部分的代码在下面), 完成后将重新加载浏览器窗口,然后关闭对话框。

Your call to reload() is after the $.post() call. 您对reload()调用是 $.post()调用之后。 So you tell the browser to make the POST request, then immediately reload the page. 因此,您告诉浏览器发出POST请求,然后立即重新加载页面。 Since you are reloading the page before the POST request is sent, you cancel the POST request. 由于您是在发送POST请求之前重新加载页面的,因此您可以取消POST请求。

You need to move the call to reload() into a callback function that you pass to $.post() . 您需要将对reload()的调用移到传递给$.post()的回调函数中。

Better still: Don't use Ajax. 更好的是:不要使用Ajax。 Just submit a regular form and have the server side handler redirect to the URL you want to reload. 只需提交常规表格,并使服务器端处理程序重定向到您要重新加载的URL。

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

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