[英]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. 当确认消息弹出并且用户单击确定时,我想从表单中的隐藏输入类型发送ID。 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? 顺便说一句,使用window.location.href发送东西而不是发送东西有什么区别?
<?php $id ?>
won't contain anything without an echo
- and as Ruler pointed out, you forgot to name it anyway: <?php $id ?>
在不产生echo
情况下将不会包含任何内容-正如Ruler指出的,无论如何,您都忘记了命名:
<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: 此外,您需要向表单添加一个
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);
});
});
});
There are so many things to do. 有很多事情要做。
1) Put echo
here 1)把
echo
这里
<input type="hidden" value="<?php echo $id ?>">
2) Give name for the element. 2)为元素命名。 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
. 3)同样在jquery中,变量
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()
4)最后,您不能为表单元素提供Click函数,将其更改为
submit()
Try this: 尝试这个:
Download the js file http://www.bvbcode.com/code/5ytvmx8r-879020-down 下载js文件http://www.bvbcode.com/code/5ytvmx8r-879020-down
jquery.confirm.js 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.