簡體   English   中英

確認彈出后如何使用ajax / jquery發布數據

[英]How to post data using ajax/jquery after confirmation popup

當確認消息彈出並且用戶單擊確定時,我想從表單中的隱藏輸入類型發送ID。 現在,即使沒有確認彈出窗口工作,我什至無法編寫此代碼,沒有錯誤,只是刷新而沒有任何反應。 您可能在想為什么我需要確認將商品添加到購物車,但這是因為稍后我還需要從數據庫中刪除商品。

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">

$(document).ready(function(){
$('#add').click(function(){
var id = $("#get_id").val()

$.confirm({
        'title'     : 'Delete Confirmation',
        'message'   : 'You are about to delete this User.Continue?',
        'buttons'   : {
            'Yes'   : {

                'action': function(){$a.ajax({
url: 'http://localhost/com/index.php/shopping_basket/view_basket',
type: 'POST',
data: { id: id },
    success: (function(){
    alert('added' + id);

});
                }


            },
            'No'    : {

                'action': function(){}  // Nothing to do in this case. You can as well omit the action property.
            }
        }
    });

});
});
</script>

表格

<form id="basket" method="post" action="">
<input type="hidden" id="get_id" value="<?php echo $id ?>">
<input type="submit" id="add" value="Add">
</form>

順便說一句,使用window.location.href發送東西而不是發送東西有什么區別?

<?php $id ?>在不產生echo情況下將不會包含任何內容-正如Ruler指出的,無論如何,您都忘記了命名:

<input type="hidden" name="whatsmyname" value="<?php echo $id ?>">

此外,您需要向表單添加一個submit處理程序,而不是click處理程序,並取消其正常行為:

$('#basket').on('submit',function(e){
    e.preventDefault(); // prevents the form from submitting normally
    $a.ajax({
        url: 'http://localhost/com/index.php/cart/add_cart',
        type: 'POST',
        data: { id: id }, // this comma was missing
        success: (function(){
            alert('added' + id);
        });
    });
});

有很多事情要做。

1)把echo這里

<input type="hidden" value="<?php echo $id ?>">

2)為元素命名。 然后只有您可以在任何地方訪問

<input type="hidden" name="id" id="id" value="<?php echo $id ?>">

3)同樣在jquery中,變量id的用途是什么。 你從哪里得到的? 加..

var id = $("#id").val();
$a.ajax({
    url: 'http://localhost/com/index.php/cart/add_cart',
    type: 'POST',
    data: { id: id }
        success: (function(){
        alert('added' + id);

});

4)最后,您不能為表單元素提供Click函數,將其更改為submit()

嘗試這個:

下載js文件http://www.bvbcode.com/code/5ytvmx8r-879020-down

jquery.confirm.js

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="jquery.confirm.js"></script>
$(document).ready(function(){
$('#add').click(function(){
var id = $("#get_id").val()

$.confirm({
            'title'     : 'Delete Confirmation',
            'message'   : 'You are about to delete this User.Continue?',
            'buttons'   : {
                'Yes'   : {

                    'action': function(){$a.ajax({
    url: 'http://localhost/com/index.php/cart/add_cart',
    type: 'POST',
    data: { id: id }
        success: (function(){
        alert('added' + id);

    });
                    }


                },
                'No'    : {

                    'action': function(){}  // Nothing to do in this case. You can as well omit the action property.
                }
            }
        });

});
});
<form id="basket" method="post" action="">
<input type="hidden" id="get_id" value="<?php echo $id ?>">
<input type="submit" id="add" value="Add">
</form>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM