如何在确认弹出后使用ajax / jquery发布数据

时间:2013-04-05 13:06:53

标签: php javascript jquery ajax

我想在弹出确认消息并且用户点击确定时,从表单中的隐藏输入类型发送一个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>

btw使用window.location.href来发送一些东西而不是这个有什么不同?

3 个答案:

答案 0 :(得分:4)

如果没有<?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 :(得分:2)

有很多事情要做。

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()

答案 2 :(得分:1)

试试这个:

下载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>