update fields in DB on td button click using ajax

actually working on a web project and i have a table having data , and … Read more update fields in DB on td button click using ajax

actually working on a web project and i have a table having data , and i want that on a click of a button which belongs to a <tr> , my ajax code update some data in my DB.
This is the structure of my table.enter image description here

Normally on button click i want to update date arrivée and set arrivee_ to true in my DB .

I figured out something but doesnt work.Really need help on this guys.
Table code.

<table class="table">
                    <thead class=" text-primary">
                      <th>Code Article</th>
                      <th>Fournisseur</th>
                      <th>Code Fournisseur</th>
                      <th>Vendor Site</th>
                      <th>Article</th>
                      <th>Frequence</th>
                      <th>Model</th>
                      <th>Date Arrivée</th>
                      <th>Arrivée</th>
                    </thead>
                    <tbody>

                      <?php 
                      $i=0;
                      foreach ($tabRes as $row) { ?>
                        <tr>
                          <td><?= $row['codeP']; ?> </td>
                          <td><?= $row['nomF']; ?> </td>
                          <td><?= $row['codeF']; ?> </td>
                          <td> <?= $row['nomV']; ?> </td>
                          <td><?= $row['libelleP']; ?> </td>
                          <td>
                            <?= $row['frequence']; ?>
                          </td>
                          <td>
                            <?= $row['typeP']; ?>
                          </td>
                          <td class="form-group">
                            <input type="date" id="ArrivalDate" class="form-control" name="ArrivalDate">
                          </td>
                          <td>
                            <button 
                              class="btn btn-primary  " 
                              onClick="suiviIng(1,'<?=$row['dateLivraison']?>','<?=$row['codeP']?>','<?=$row['idV']?>','<?=$row['idF']?>')"
                              >
                              Ok
                            </button>
                          </td>
                        </tr>
                      <?php } ?>
                    </tbody>
                  </table>

Ajax code

<!-- Ajax Scripts -->
    <script>
        function suiviIng(arrivee,dateLivraison,codeP,idV,idF){
            $.post("suiviTraitement.php",{ arrivee : arrivee, 
                      dateLivraison :dateLivraison, 
                      codeP : codeP,
                      idF : idF,
                      idV : idV
                    },function(){
                       console.log("ok ING traité");
                    });
        }

    </script>

SuiviTraitement code to update the fields in the DB,using the passed parameters in Ajax.

<?php
session_start();

$arrivee = $_POST["arrivee"];
$date_livraison = $_POST['dateLivraison'];
$codeProduit = $_POST['codeP'];
$idFournisseur = $_POST['idF'];
$idVendeur =$_POST['idV'] ;

require "../config/bdcon.php";

$sql = "UPDATE produit SET arrivee_ = ?, date_livraison = ? WHERE codeP = ? AND idF = ? AND idV = ?";
$stmt= $pdo->prepare($sql);
$stmt->execute([$arrivee, $date_livraison, $codeProduit, $idFournisseur, $idVendeur]);

header("Location:dashboard.php");

?>

Source: JavaSript – Stack Overflow



Leave a Reply

Your email address will not be published. Required fields are marked *