2017-01-16 31 views
0

我試圖用onchange下拉列表觸發的ajax更新表中的1行,我成功更新了數據,但它更新了表中的所有數據。如何獲取由onchange下拉觸發的ajax的行值?

所以我如何獲得獨特的價值(例如:用戶ID)從ajax傳遞給我的PHP,所以我可以更新它只有1行?

這裏是我的代碼:

我的表(transactions.php)

<table class="table table-bordered table-hover table-striped"> 
    <thead> 
     <tr> 
     <th>User ID</th> 
     <th>Pengirim</th> 
     <th>Jumlah Transfer</th> 
     <th>Berita Acara</th> 
     <th>Status</th> 
     <th>Rincian</th> 
     </tr> 
    </thead> 
    <tbody> 
     <?php 
     $query = mysqli_query($koneksi, "select * from konf_transf order by tanggal desc limit 7 "); 
     while($data1 = mysqli_fetch_array($query)){ 
      ?> 
     <tr> 
     <td> 
      <center><?php echo $data1['usr_id']; ?></center> 
     </td> 
     <td> 
      <center><?php echo $data1['nm_pengirim']; ?></center> 
     </td> 
     <td> 
      <center>Rp. <?php echo number_format($data1['jmlh_transf'],0,'','.'); ?>,-</center> 
     </td> 
     <td> 
      <center><?php echo $data1['berita_acara']; ?></center> 
     </td> 
     <td> 
      <center><?php echo $data1['status']; ?></center> 
     </td> 
     <td> 
      <center> 
       <select name="pilihstatus" id="pilihstatus" onchange="updatetransactions();"> 
        <option value="Pilihan">Pilihan</option> 
        <option value="Sudah">Sudah</option> 
        <option value="Belum">Belum</option> 
       </select> 
      </center> 
     </td> 
     </tr> 
     <?php } ?> 
    </tbody> 
</table> 

,在這裏我的AJAX

function updatetransactions(){ 
    var id = $('select option:selected').val(); 
$.ajax({ 
      type:"post", 
      url:"updatestatustransaksi.php", 
      data:"status="+id, 
      success:function(data){ 
       alert('Successfully updated mysql database'); 
      } 
     }); 
    } 

我updatestatustransaksi.php

<?php 
require_once("koneksi.php"); 
session_start(); 
if (!isset($_SESSION['username'])) { 
    echo "<script>alert('You must register an account first, we will redirect you to register page !'); window.location = 'registuser.php'</script>"; 
} 
$dataupd = $_POST["status"]; 
$query = mysqli_query($koneksi, "UPDATE `konf_transf` SET `status` = '$dataupd' WHERE `id` = '$penjualan_id'"); 
if ($query) { 
    echo "<script>alert('Update Success.'); window.location = 'transactions.php' </script>"; 
} else { 
    echo "<script>alert('Update Failure.'); window.location = 'transactions.php' </script>"; 
} 
+0

你必須包括你的'updatestatustransaksi.php'文件 –

+0

@LoganWayne更新 –

+0

數據庫沒有更新但我仍然有alert alert成功更新mysql數據庫,我在控制檯中查看帖子,它成功通過參數ID和狀態 –

回答

1

我假設你更新所有行的在你的數據庫,沒有 WHERE條件。 在您的腳本中,我們還可以獲取數據庫中該行的相應ID。

讓我們先分配的ID爲每個錶行:

while($data1 = mysqli_fetch_array($query)){ 
     ?> 
    <tr id="<?=($data1['user_id'])?>"> 

然後,讓改變你如何觸發你的JavaScript。讓我們先改變<select>領域:

$(".pilihstatus").change(function(){ 

    var elem = $(this), 
     selecteditem = elem.val(), 
     id = elem.closest('tr').attr('id'); 

    $.ajax({ 
     type:"post", 
     url:"updatestatustransaksi.php", 
     data: {'status':selecteditem, 'id':id}, 
     success:function(data){ 
      alert('Successfully updated mysql database'); 
     } 
    }); 
}); 

並在您updatestatustransaksi.php文件(請使用prepared statement):

$stmt = $koneksi->prepare("UPDATE `konf_transf` SET `status` = ? WHERE `id` = ?"); 
$stmt->bind_param("si", $_POST['selecteditem'], $_POST['id']); 
$stmt->execute(); 
$stmt->close(); 

<select name="pilihstatus" id="pilihstatus" class="pilihstatus"> 

然後,使用下面的腳本得到相應的ID

+0

當您在此處編寫並在控制檯中更改時,我將更改「TypeError:elem.nodeName is undefined」,即時使用此jquery < script src =「http://code.jquery.com/jquery-1.9.1.js」> –

0

有你初始化$penjualan_id?我認爲你需要初始化它:

$penjualan_id=$_SESSION['user_id'] 
0

嘗試添加data_id屬性以選擇

<table class="table table-bordered table-hover table-striped"> 
    <thead> 
     <tr> 
     <th>User ID</th> 
     <th>Pengirim</th> 
     <th>Jumlah Transfer</th> 
     <th>Berita Acara</th> 
     <th>Status</th> 
     <th>Rincian</th> 
     </tr> 
    </thead> 
    <tbody> 
     <?php 
     $query = mysqli_query($koneksi, "select * from konf_transf order by tanggal desc limit 7 "); 
     while($data1 = mysqli_fetch_array($query)){ 
      ?> 
     <tr> 
     <td> 
      <center><?php echo $data1['usr_id']; ?></center> 
     </td> 
     <td> 
      <center><?php echo $data1['nm_pengirim']; ?></center> 
     </td> 
     <td> 
      <center>Rp. <?php echo number_format($data1['jmlh_transf'],0,'','.'); ?>,-</center> 
     </td> 
     <td> 
      <center><?php echo $data1['berita_acara']; ?></center> 
     </td> 
     <td> 
      <center><?php echo $data1['status']; ?></center> 
     </td> 
     <td> 
      <center> 
       <select name="pilihstatus" id="pilihstatus" onchange="updatetransactions();" data_id='<?php echo $data1['usr_id']; ?>'> 
        <option value="Pilihan">Pilihan</option> 
        <option value="Sudah">Sudah</option> 
        <option value="Belum">Belum</option> 
       </select> 
      </center> 
     </td> 
     </tr> 
     <?php } ?> 
    </tbody> 
</table> 

和獲取data_id值在函數

function updatetransactions(){ 
    var status = $('select option:selected').text(); 
    var status = $('select').attr('data_id'); 
    var id = $('select option:selected').val(); 
$.ajax({ 
      type:"post", 
      url:"updatestatustransaksi.php", 
      data:{'status'=status,'id'=id} 
      success:function(data){ 
       alert('Successfully updated mysql database'); 
      } 
     }); 
    } 

,並在您的updatestatustransaksi.php

<?php 
require_once("koneksi.php"); 
session_start(); 
if (!isset($_SESSION['username'])) { 
    echo "<script>alert('You must register an account first, we will redirect you to register page !'); window.location = 'registuser.php'</script>"; 
} 
$dataupd = $_POST["status"]; 
$id = $_POST["id"]; 
$query = mysqli_query($koneksi, "UPDATE `konf_transf` SET `status` = '$dataupd' WHERE `id` = '$id'"); 
if ($query) { 
    echo "<script>alert('Update Success.'); window.location = 'transactions.php' </script>"; 
} else { 
    echo "<script>alert('Update Failure.'); window.location = 'transactions.php' </script>"; 
}