1

I would like to send an id from the hidden input type in the form, when a confirmation message pops up and user clicks ok. Right now I cant even make this code without the confirmation popup work, no errors, just a refresh and nothing happens. You may be thinking why I need to confirm adding an item to a cart but that's because I also need to delete an item from the database later on.

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

the form

<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 what's the difference in using window.location.href to send something instead of this?

4

3 回答 3

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);
        });
    });
});
于 2013-04-05T13:08:47.153 回答
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()

于 2013-04-05T13:11:24.067 回答
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>
于 2013-04-05T13:54:15.747 回答