Franky2207
Franky2207

Reputation: 163

Sending variable values via ajax in a table for specific rows

I am stuck on an issue concerning my ajax request. The goal is to have a table with a number of rows each including a button or a link that can be clicked to send a value in order to update the database. After this is done, the button or link should not be clickable anymore and be replaced by pure text on that specific row.

EDIT: Thanks to @Sagar Arora we have the working code now to send a specific variable value for a specific row, but we need a way to replace the button with pure text so one can see that it has been clicked.

For testing, I have the following potatoe.php:

// Several table-rows before and after
// Problem is, the class "paid_button" also belongs to the other buttons in every other
// table row, but I want to adress them specifically to get the id of that row for the ajax

<td><button name="paid" value="<?php echo $id; ?>" class="paid_button">Bezahlt</button></td>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>                         
<script src="../js/global.js"></script> 

In my global.js

$(document).on("click",".paid_button",function(){

var current_element = $(this);
$.ajax({
type: "POST",
url: "https://www.xxx",
data: { 'paid': current_element.attr("value")},
success: function(data){
 alert("ok"); 
 this.attr("disabled","disabled");//this will disable the clicked button
} 
});
});

updatePotatoe.php:

// the update database query and execution (is already doing fine)

$request_id = $_POST['request_id'];

echo "test $request_id test";

I am thankful for any help and tips!

Upvotes: 0

Views: 616

Answers (1)

Sagar Arora
Sagar Arora

Reputation: 1773

SO this in the following way:

<button name="paid" class="paid_button" value="$id" >Support</button>

$(document).on("click",".paid_button",function(){

var current_element = $(this);
  $.ajax({
    type: "POST",
    url: "https://www.xxx.php",
    data: { 'paid': current_element.attr("value")},
    success: function(data){
     alert("ok"); --> just for testing, I actually want to return values
     this.attr("disabled","disabled");//this will disable the clicked button
    } 
   });


});

Upvotes: 1

Related Questions