douliangbian7323 2013-09-20 11:09
浏览 36
已采纳

单击链接/按钮上的Jquery / AJAX更新表

I have a table populated from MySQL, I'm trying to update values by a click of a button, but I'm not really sure where to start.

I am just getting into Jquery / AJAX, and haven't really figured it out quite yet so don't be too harsh on me.

In my example I have a HTML table that looks like this:

ID   Status      Set Status
1    Pending     Yes / No
2    Pending     Yes / No
3    Pending     Yes / No
4    Pending     Yes / No
5    Pending     Yes / No

Now when I click on ID 3 Yes, MySQL entry with ID 3 should be updated to Yes

My question is, how do I find out which Yes / No of which row was clicked ? How do I pass that to the processing script via AJAX ?

My Yes / No Links have the ID of the entry attached like so:

<a href="status.php?set=yes&id=3">

I'm fine with the PHP part I think, my problem is getting the correct data to it.

//Edit, this is the table:

<table class="table table-striped table-bordered table-hover datatable">
       <thead>
         <tr>
           <th><div class="checkbox"><input type="checkbox" /></div></th>
           <th>ID</th>
           <th>Status</th>
           <th>Set Status</th>
         </tr>
       </thead>
<tbody>
<?php 
      $query=mysql_query( "select * from test");
      if(mysql_num_rows($query)>0){
      while($data=mysql_fetch_array($query,1)){
?>
<tr>
  <td><div class="checkbox"><input type="checkbox" /></div></td> 
  <td><?php echo $data['id']; ?></td>
  <td>
  <?php 
  if($data['status'] == 'pending') 
  { 
       echo "<span class=\"label label-warning\">Pending</span>";
  } 
  elseif($data['status'] == 'yes') 
  { 
      echo "<span class=\"label label-success\">Yes</span>";
  } 
  elseif($data['status'] == 'no') 
  { 
    echo "<span class=\"label label-danger\">No</span>";
  } 
  ?></td>
  <td>
  <?php 
  if($data['status'] == 'pending') 
  { 
      echo "<a href=\"" . "status?set=yes&id=" . $data['id'] ."\" class=\"btn btn-success btn-xs\"><i class=\"icon-ok-sign\"></i> Yes</a>" . " <a href=\"" . "status?set=no&id=" . $data['id'] ."\" class=\"btn btn-danger btn-xs\"><i class=\"icon-remove\"></i>No</a>"; 
   } ?>
   </td>
   </tr>
   <?php 
          }
     } ?>
   </tbody>
   </table>

/// EDIT 2

Utilizing user574632's answer, I got the simple AJAX GET to work as to updating the table, now I'm trying to get some feedback posted back to the user and hiding the 2 buttons.

status.php

$id = $_GET['id'];
$status = $_GET['set'];

if($_GET['set'] == 'yes') {
$result = mysql_query("UPDATE test SET status='yes' WHERE id='$id'") 
or die(mysql_error()); 
echo "Status Changed to Yes";
exit();  }

if($_GET['set'] == 'no') {
$result = mysql_query("UPDATE test SET status='no' WHERE id='$id'") 
or die(mysql_error()); 
echo "Status Changed to No";
exit();  }
  • 写回答

2条回答 默认 最新

  • duanou3868 2013-09-20 11:23
    关注

    Whilst you probably want to display some kind of feedback to the user, this simplest way to acheive this would be like so:

    $('#tableid a').click(function(){
        event.preventDefault();
        var href = $(this).attr('href');
        $.get( href );
    })
    

    provided the html table has an id='tableid'.

    What this does is stop the link being followed, then grap the href attribute, and perform a get request via ajax.

    You would probably want to indicate success/fail etc, but without seeing your status.php i cant elaborate

    EDIT per comment: To get data back from the php file:

     $('#tableid a').click(function(){
        event.preventDefault();
        var href = $(this).attr('href');
    
        $.get( href, function( data ) {
            //append data to a div, hide buttons etc.
            //alert used as example
            alert( data );
        });
     });
    

    Y

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试