繁体   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