Home > Net >  PHP ajax Multiple data transfer
PHP ajax Multiple data transfer

Time:04-12

In the code below it works when I pass data. But it doesn't work when I pass multiple data. I have used data from the table row. I can't catch my mistake.

ajax_update_bill_status.php

 if($_POST['unique_id']!=""):
        extract($_POST);
     
        $last_bill=$_POST['last_bill'];
        $last_bill_date = date('Y-m-d');
    
          $sql = $conn->prepare("UPDATE tbl_clients SET last_bill=?, last_bill_date=? WHERE unique_id=?");
          $sql->bind_param("ssi", $last_bill, $last_bill_date, $unique_id);
     $sql->execute()
     
    endif;

index.php

 <a  data-appd="ok" data-appds="2022-04-12" href=""  >Transfer</a>

<script>
   $(document).on('click','.transfer',function(){
   var element = $(this);
   var unique_id = element.attr("data-appd");
   var last_bill = element.attr("data-appds");
   var info = 'unique_id='   unique_id;
   var last = 'last_bill='   last_bill;
   if(confirm("Are you sure you want to transfer this?"))
   {
    $.ajax({
      type: "POST",
      url: "ajax_update_bill_status.php",
      data: info,last,
      success: function(){
    }
   });
     $(this).parents("tr").animate({ backgroundColor: "#003" }, "slow")
     .animate({ opacity: "hide" }, "slow");
    }
   return false;
   });
</script>

CodePudding user response:

data: info, last is not how you combine info and last in the data: option. They should be a single string, with the parameters separated by &.

However, it's better to use an object instead of a string; $.ajax() will automatically convert this to a properly encoded string.

$(document).on('click', '.transfer', function() {
  var element = $(this);
  var unique_id = element.data("appd");
  var last_bill = element.data("appds");
  if (confirm("Are you sure you want to transfer this?")) {
    $.ajax({
      type: "POST",
      url: "ajax_update_bill_status.php",
      data: {unique_id, last_bill},
      success: function() {}
    });
    $(this).parents("tr").animate({
        backgroundColor: "#003"
      }, "slow")
      .animate({
        opacity: "hide"
      }, "slow");
  }
  return false;
});

CodePudding user response:

Wrong use

data: info,last,

<a  data-appd="ok" data-appds="2022-04-12" href=""  >Transfer</a>

<script>
   $(document).on('click','.transfer',function(){
   var element = $(this);
   var unique_id = element.attr("data-appd");
   var last_bill = element.attr("data-appds");
   var info = 'unique_id='   unique_id;
   var last = 'last_bill='   last_bill;
   if(confirm("Are you sure you want to transfer this?"))
   {
    $.ajax({
      type: "POST",
      url: "ajax_update_bill_status.php",
      data: "" info "& " last "",
      success: function(){
    }
   });
     $(this).parents("tr").animate({ backgroundColor: "#003" }, "slow")
     .animate({ opacity: "hide" }, "slow");
    }
   return false;
   });
</script>
  • Related