I'm building a checkout on a marketplace application. I need to be able to increment, decrement and remove items at the checkout but when there is multpile items the AJAX event jQuery only picks up the first item_id when the AJAx event fires no mater what item a user clicks on, how can I fix this?
Here's the PHP/HTML code...
<?php
$total = 0;
$subtotal = 0;
foreach($items as $item):
$subtotal += $item['product_price']*$item['product_qty'];
?>
<tbody id="item_<?=$item['cart_item_id']?>">
<tr>
<td>
<img src="<?=$item['product_image']?>">
<?php if($item['brand_name']!='None'): ?>
<p style="color: #666;"><?=$item['brand_name']?></p>
<?php endif; ?>
<p style="font-size: 16px;"><?=$item['product_name']?></p>
<a href="#" id="remove-item-button" >Remove this item</a> <!--HERE-->
</td>
<td class="align-td-center">
<?=formatted_price($item['product_price'])?>
</td>
<td class="align-td-center">
Less<!--AND HERE-->
<input type="hidden" id="item_id" value="<?=$item['cart_item_id']?>">
</td>
<td class="align-td-center">
<span id="value-shipping-<?=$item['cart_item_id']?>"><?=formatted_price($item['product_price']*$item['product_qty'])?></span>
</td>
</tr>
</tbody>
And here's my jQ/AJAX...
$('a#qty-inc-button').click(function(){
$("#processing").fadeIn("fast");
$.ajax({
url: "<?=BASE_URL?>landing/inc_cart_item",
type: "post",
dataType: "json",
data: {inc:1, item_id: $('#item_id').val(),},
success: function(data){
$("#processing").fadeOut("fast");
if(data.error) {
alert(data.error);
} else {
// parent.$('#line1').text(data.line1);
//parent.$('#line2').text(data.line2);
//parent.$('#address-dialog').dialog('close');
alert(data.line1);
}
}
});
return false;
});
$('a#qty-dec-button').click(function(){
$("#processing").fadeIn("fast");
$.ajax({
url: "<?=BASE_URL?>landing/dec_cart_item",
type: "post",
dataType: "json",
data: {dec:0, item_id: $('#item_id').val(),},
success: function(data){
$("#processing").fadeOut("fast");
if(data.error) {
alert(data.error);
} else {
//parent.$('#line1').text(data.line1);
//parent.$('#line2').text(data.line2);
//parent.$('#address-dialog').dialog('close');
alert(data.line1);
}
}
});
return false;
});
$('a#remove-item-button').click(function(){
$("#processing").fadeIn("fast");
$.ajax({
url: "<?=BASE_URL?>landing/rem_cart_item",
type: "post",
dataType: "json",
data: {item_id: $('#item_id').val(),},
success: function(data){
$("#processing").fadeOut("fast");
if(data.error) {
alert(data.error);
} else {
//parent.$('#line1').text(data.line1);
//parent.$('#line2').text(data.line2);
//parent.$('#address-dialog').dialog('close');
alert(data.line1);
}
}
});
return false;
});
All help appreciated and thanks for looking.
An id is meant to be an unique identifier on a page. http://css-tricks.com/the-difference-between-id-and-class/
So, JQuery stops searching as soon as it finds the first occurrence of #qty-inc-button.
There are many ways to do what you are trying to acheive. You should make qty-inc-button a class name along with any id that appears multiple times on a page. Then you can try
$('a.qty-inc-button').click(function(){
$(this).DoSomething(); // where $(this) is the 'a.qty-inc-button' that the user clicked on.
// you can do:
$(this).children(".aChildClass").hide();
// you can do:
$(this).find( 'someElement' ).css( 'background-color', 'red' ); //etc, etc
});
However, in my very humble opinion you should learn more about HTML attributes and JQuery traversal before trying things of this caliber. Otherwise, you'll keep stumbling.
Try to change IDs to classes, modify selectors to find child object of parent table_item
Read about difference bteweeen id and classes.
After speaking to both client and PM and experimenting with traversal this is the answer...
onClick="remItem(<?=$item['cart_item_id']?>)"
In the relevant anchor tags, untidy it is but time is against us. Thanks to all.
Related
I have a cancel button on each row of a table that is autopopulated in a while loop.
The $id resolves correctly but when I click on the cancel button all the active rows are modified. I only want the row that is concerned to be modified.
So in a while loop in php:
<tr id="dataTable">
<td id="nom"><?php echo $nom;?></td>
<td id="statut"><?php echo $statut;?> </td>
<td id="<?php echo $id;?>" >
<a id="<?php echo $id;?>" href="#" title="Annuler"
onclick="document.write('<?php
$queryannuler = "UPDATE wp_RDV SET statut = 'Testy' WHERE id_RDV = '".$id."'";
$resultatannuler = $connexion->query($queryannuler); ?>'); location.reload(); 'return false';">Annuler</a>
</td>
</tr>
My problem is the 'statut' column is being updated for all the rows and not just the row where I click on the cancel button. Thanks if you can help.
UPDATE
I have tried to implement your different advices by using ajax. It's tough. I didn't specify in the original post that I am using a Wordpress framework. So I have modified #ssurli code to take into account wordpress
<td id="<?php echo $id;?>" >
<a id="<?php echo $id;?>" class="button delete" href="javascript:void(0)" title="Annuler"
onclick="cancelRecord('<?php echo $id; ?>')">Annuler</a>
</td>
<script>
function cancelRecord(id) {
jQuery.ajax({
type: 'post',
url: ajaxurl,
data: {id:id},
action: 'delete_rdv',
success: function(response) {
return;
},
error: function(jqXHR, status, error) {
alert('Error: Please refresh the page');
return;
},
complete: function() {
console.log('The function is hooked up');
location.reload();
console.log('The function is hooked up2');
}
});
}
AND in my functions.php file
add_action( 'wp_ajax_delete_rdv', 'delete_rdv' );
add_action( 'wp_ajax_nopriv_delete_rdv', 'delete_rdv' );
function delete_rdv() {
$queryannuler = "UPDATE wp_RDV SET statut = 'Testy' WHERE id_RDV = '".$_POST['id']."'";
$resultatannuler = $connexion->query($queryannuler); //include connection file above
return $resultatannuler;
}
I'm stuck on the very first hurdle.
The console is sending an error message :
admin.php?page=plugin-voc:1 Uncaught ReferenceError: cancelRecord is not defined
But my cancelRecord function is right after my button call so I don't understand this error.
Here the working code.
Update your table row as below:
<tr id="dataTable">
<td id="nom"><?php echo $nom;?></td>
<td id="statut"><?php echo $statut;?> </td>
<td id="<?php echo $id;?>" >
<a id="<?php echo $id;?>" href="javascript:void(0)" title="Annuler"
onclick="cancelRecord('<?php echo $id; ?>')">Annuler</a>
</td>
</tr>
Add below js scripts to bottom of your html page. Include jquery cdn file only if not add to page already.
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script>
function cancelRecord(id) {
$.ajax({
type: 'post',
url: 'cancel.php', //use actual directory path, if not kept in root directory
data: {id:id},
success: function(response) {
return;
},
error: function(jqXHR, status, error) {
alert('Error: Please refresh the page');
return;
},
complete: function() {
location.reload();
}
});
}
</script>
Add PHP file having PHP code having DB query responsible for cancelling the record.
//cancel.php
<?php
$queryannuler = "UPDATE wp_RDV SET statut = 'Testy' WHERE id_RDV = '".$_POST['id']."'";
$resultatannuler = $connexion->query($queryannuler); //include connection file above
return $resultatannuler;
Hope it will work for you. Feel free if you have any further query.
This was the original html request
<td id="<?php echo $id;?>" >
<a id="<?php echo $id;?>" class="button delete" href="javascript:void(0)" title="Annuler"
onclick="cancelRecord('<?php echo $id; ?>')">Annuler</a>
The javascript function was not defined when I put it in the same file as the php. I moved into the plugin index.js file and most importantly I moved this line:
action: 'delete_rdv',
into data {}
data: {
action: 'delete_rdv',
id:id},
Here is the complete javascript code:
jQuery( document ).ready(function($){ $('.button.delete').on('click', function(){ let id = $(this).attr('id'); cancelRecord(id); }); });
function cancelRecord(id) {
jQuery.ajax({
type: 'post',
url: ajaxurl,
data: {
action: 'delete_rdv',
id:id},
success: function(response) {
return;
},
error: function(jqXHR, status, error) {
alert('Error: Please refresh the page');
return;
},
complete: function() {
location.reload();
}
});
}
The ajax calls the function delete_rdv which I put in the functions.php file
add_action( 'wp_ajax_delete_rdv', 'delete_rdv' );
add_action( 'wp_ajax_nopriv_delete_rdv', 'delete_rdv' );
function delete_rdv() {
$queryannuler = "UPDATE wp_RDV SET statut = 'Annulé' WHERE id_RDV = '".$_POST['id']."'";
$resultatannuler = $connexion->query($queryannuler); //include connection file above
return $resultatannuler;
}
Special thanks to #jeprubio for kinda forcing to look the ajax way and to #sssurii for posting his code and answering my question.
i am using ajax to sort list the hotels when i click on heat icon which is coming from while query from database on that hear icon i called one function to set a session after set a session i want to fill heart icon without refreshing a page and again clicking on same heart icon i want to unset the particular id from session and show the heart icon as blank .
please provide me solution.
live url- https://www.hotelinkonkan.com
<a class="clickforact"><i class="fa fa-heart-o" id="boricon1" onclick="cart('<?php echo $id;?>')" ></i> </a>
The Function which is write on this icon click event for add and remove the is from session
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type:'post',
url:'./../include/store_items.php',
data:{
total_cart_items:"totalitems"
},
success:function(response) {
document.getElementById("total_items").value=response;
}
});
});
function cart(id)
{
$('.clickforact').click(function(){
if($(this).find($("i")).hasClass('fa-heart-o'))
{
$(this).find($("i")).removeClass('fa-heart-o').addClass('fa-heart');
}
else if($(this).find($("i")).hasClass('fa-heart'))
{
$(this).find($("i")).removeClass('fa-heart').addClass('fa-heart-o');
}
});
$.ajax({
type:'post',
url:'./../include/store_items.php',
data:{
iteam_id:id
},
success:function(response) {
document.getElementById("total_items").value=response;
}
});
}
function cart_remove(id)
{
$.ajax({
type:'post',
url:'./../include/store_items.php',
data:{
iteam_id_remove:id
},
success:function(response) {
document.getElementById("total_items").value=response;
}
});
show_cart();
}
function show_cart()
{
$.ajax({
type:'post',
url:'./../include/store_items.php',
data:{
showcart:"cart"
},
success:function(response) {
document.getElementById("mycart").innerHTML=response;
$("#mycart").slideToggle();
}
});
}
</script>
You are calling event multiple times so you are facing an issue
1)with javascript 'onclick' : onclick="cart('')"
2)with jquery 'click' : $('.clickforact').click(function(){}
You can achive your goal using jquery only.
with this:
//pass data-id with anchor tag
<a class="clickforact" data-id="<?php echo $id;?>"><i class="fa fa-heart-o" id="boricon1" ></i> </a>
Use only jquery event
$('.clickforact').click(function(){
if($(this).find($("i")).hasClass('fa-heart-o'))
{
$(this).find($("i")).removeClass('fa-heart-o').addClass('fa-heart');
}
else if($(this).find($("i")).hasClass('fa-heart'))
{
$(this).find($("i")).removeClass('fa-heart').addClass('fa-heart-o');
}
var id = $(this).data("id"); //<---retrive your id
$.ajax({
type:'post',
url:'./../include/store_items.php',
data:{
iteam_id:id
},
success:function(response) {
document.getElementById("total_items").value=response;
}
});
});
I used a solution in this post
I used this solution on my webpage and it works partially.
Basically I have a table with a link on each row and when I click a link, I retrieve data via AJAX and display this data in another table. All works well when I click the first link but throws a "403 Forbidden" error when I click another link in the table.
<div class="col-lg-4" id="media-sources-view">
<div id="result">
<table class="table table-hover hidden" id="xarticletab">
<tr>
<th>Title</th>
<th>Name</th>
</tr>
</table>
</div>
</div>
<script type="text/javascript">
$('#mashed_row a').click(function () {
var link_id = $(this).attr('link_id');
$.ajax({
type: 'POST',
url: '<?php echo base_url(); ?>main/explode_link',
data: {'<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>', link_id},
dataType: 'json',
success : function(data) {
if(data){
var len = data.length;
var txt = "";
if(len > 0){
for(var i=0;i<len;i++){
if(data[i].title){
txt += "<tr><td>"+data[i].title+"</td><td>"+data[i].name+"</td></tr>";
}
}
if(txt != ""){
$("#xarticletab").append(txt).removeClass("hidden");
}
}
}
}
});
return false;
});
</script>
It's because of CSRF token regeneration after every POST request.
First time when you output the page, CSRF token in javascript is valid but after first request it gets regenerated but your javascript code still uses old one.
Either disable CSRF regeneration which will use same CSRF token until it expires - default 7200 seconds (this lowers security a bit so I wouldn't recommend it) - in application/config/config.php
$config['csrf_regenerate'] = false;
or refactor your javascript code and the php file which processes ajax to use GET request which doesn't require CSRF token.
To clear old table rows and populate new, first make a proper html table layout:
<table class="table table-hover hidden" id="xarticletab">
<thead>
<tr>
<th>Title</th>
<th>Name</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
And ajax code:
$.ajax({
type: 'POST',
url: '<?php echo base_url(); ?>main/explode_link',
data: {'<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>', link_id},
dataType: 'json',
success : function(data) {
if (data) {
// Clear table body rows
$('#xarticletab tbody').empty();
var len = data.length;
var txt = "";
if (len > 0) {
for (var i=0;i<len;i++) {
if (data[i].title) {
txt += "<tr><td>"+data[i].title+"</td><td>"+data[i].name+"</td></tr>";
}
}
if (txt != "") {
$('#xarticletab').removeClass('hidden');
// Append rows to table body
$("#xarticletab tbody").append(txt);
}
}
}
}
});
I have a problem and I do not know if the method I am using is correct.
I want to decrease the variable $ pag -1 if a record is deleted.
I do this because I have a pagination and affects which records are deleted, I must decrease that variable I use in the pagination.
In this code I decreases though it has not eliminated any registration.
How can I do to make me decrease the variable only if a record is deleted?
her code:
<?php $pag=20; ?>
<script type="text/javascript">
$('.delete').live("click",function()
{
var ID = $(this).attr("id");
var dataString = 'id='+ ID;
jConfirm('DELETE?',
function(r) {
if(r==true)
$.ajax({
type: "POST",
url: "delete.php",
data: dataString,
cache: false,
beforeSend: function(){
$("#stbody"+ID).animate({'backgroundColor':'#F2F2F2'},300);},
success: function(html){
$("#body"+ID).fadeOut(300,function(){$("#body"+ID).remove();});
//div update
setTimeout(function() {
$("#apDiv101").load('disminuye.php');
}, 100);
}
});
});
return false;
});
</script>
<!--html delete-->
<a class="delete" href="#" id="<?php echo $id;?>" title="DELETE"></a>
<!--DIV update-->
<div id="apDiv101"><?php
include('disminuye.php');
?></div>
file disminuye.php
<?php
$pag = $pag-1;
echo $pag;
?>
hy , I use AJAx in my project
<?php foreach ($dt_pesanan_detail->result_array() as $key) { ?>
<tr class="content">
<td class="td-keranjang" id="id_kelas"><?php echo $key['nama_menu']; ?></td>
<script type="text/javascript">
$("#id_kelas").change(function(){
var id_kelas = {id_kelas:$("#id_kelas").val()};
$.ajax({
type: "POST",
url : "<?php echo base_url(); ?>transaksi/ambil_data_pelanggan_ajax",
data: id_kelas,
success: function(msg){
$('#siswa').html(msg);
}
});
});
</script>
but this code only for array index 1 , if result $dt_pesanan_detail more then 2 row,why j query only run in 1 row, 2 row can't run
The first problem is that you're generating that script in every iteration of the array $dt_pesanan_detail. The 2nd (and real issue) is that ID's are unique, and therefore you can't do what you're trying to achieve. Therefore, perform these adjustments:
<?php foreach ($dt_pesanan_detail->result_array() as #key) { ?>
<tr class="content">
<td class="td-keranjang" id="id_kelas" class="id_kelas">
Then don't generate the script in your foreach loop.
$(".id_kelas").change(function(){
$.ajax({
type: "POST",
url : "<?php echo base_url(); ?>transaksi/ambil_data_pelanggan_ajax",
data: id_kelas,
success: function(msg){
$('#siswa').html(msg);
}
});
});