简体   繁体   中英

How to post data using ajax/jquery after confirmation popup

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?

<?php $id ?> won't contain anything without an echo - and as Ruler pointed out, you forgot to name it anyway:

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

Further, you need to add a submit handler to the form, not a click handler, and cancel its normal behavior:

$('#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);
        });
    });
});

There are so many things to do.

1) Put echo here

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

2) Give name for the element. Then only you can access anywhere

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

3) Also in jquery, what is the use of the variable id . where are you getting it from? Add that..

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) And finally, you cannot give Click function for form element, change it to submit()

Try this:

Download the js file 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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