编辑无线电输入类型并在db table-bootstrap中更新

时间:2017-06-17 14:14:32

标签: jquery mysql twitter-bootstrap

我有一个包含可编辑列的表,然后更新到数据库表。

文字字段编辑完美...
但现在我正在尝试编辑单选按钮 我想显示选定的无线电,其值在db表中。

图像上传也是如此 这是我试过的代码。我坚持这个。怎么解决?有什么建议吗?

代码

<tbody id="_editable_table">
  <?php foreach($queryRecords as $res) :?>
  <tr data-row-id="<?php echo $res['cat_id'];?>">
    <td class="editable-col" contenteditable="true" col-index='0' oldVal ="<?php echo $res['cat_name'];?>"><?php echo $res['cat_name'];?></td>
    <td class="editable-col" contenteditable="true" col-index='1' oldVal ="<?php echo $res['cat_image'];?>"><img src="images/<?php echo $res['cat_image'];?>" width="20px"/><input type="file" class="filestyle" data-icon="false"></td>
    <td class="editable-col"  col-index='3' oldVal ="<?php echo $res['cat_type']?'checked':''?>" ><?php echo $res['cat_switch'];?>
      <input type="radio" name="type" value="0" <?php echo ($res['cat_type']==0)?'checked':'' ?>>Ordinary
      <input type="radio" name="type" value="1" <?php echo ($res['cat_type']==1)?'checked':'' ?> >Special
    </td>
  </tr>
  <?php endforeach;?>
</tbody>

脚本

<script type="text/javascript">

$(document).ready(function(){
  $('td.editable-col').on('focusout', function() {
    data = {};
    data['val'] = $(this).text();
    data['id'] = $(this).parent('tr').attr('data-row-id');
    data['index'] = $(this).attr('col-index');
    if($(this).attr('oldVal') === data['val'])
    return false;

    $.ajax({

      type: "POST",
      url: "server.php",
      cache:false,
      data: data,
      dataType: "json",
      success: function(response)
      {
        //$("#loading").hide();
        if(!response.error) {
          $("#msg").removeClass('alert-danger');
          $("#msg").addClass('alert-success').html(response.msg);
        } else {
          $("#msg").removeClass('alert-success');
          $("#msg").addClass('alert-danger').html(response.msg);
        }
      }
    });
  });
});

</script>

server.php

<?php
    //include connection file 
    include_once("connection.php");

    //define index of column
    $columns = array(
        0 =>'cat_name',
        1 => 'cat_image',
        2 => 'cat_type',
        3 => 'cat_switch'
    );
    $error = false;
    $colVal = '';
    $colIndex = $rowId = 0;

    $msg = array('status' => !$error, 'msg' => 'Failed! updation in mysql');

    if(isset($_POST)){
    if(isset($_POST['val']) && !empty($_POST['val']) && !$error) {
      $colVal = $_POST['val'];
      $error = false;

    } else {
      $error = true;
    }
    if(isset($_POST['index']) && $_POST['index'] >= 0 &&  !$error) {
      $colIndex = $_POST['index'];
      $error = false;
    } else {
      $error = true;
    }
    if(isset($_POST['id']) && $_POST['id'] > 0 && !$error) {
      $rowId = $_POST['id'];
      $error = false;
    } else {
      $error = true;
    }

    if(!$error) {
            $sql = "UPDATE categories SET ".$columns[$colIndex]." = '".$colVal."' WHERE cat_id='".$rowId."'";
            $status = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
            $msg = array('error' => $error, 'msg' => 'Success! updation in mysql');
    } else {
        $msg = array('error' => $error, 'msg' => 'Failed! updation in mysql');
    }
    }
    // send data as json format
    echo json_encode($msg);

?>

0 个答案:

没有答案