doukong9982 2014-04-08 19:32
浏览 43
已采纳

在按钮上单击更新具有用户标识的行和列

I built a checklist system where managers can create a list of products and employees need to be able to signoff on these products when they've completed them. Each product or material, when created, gets attached to a revision number. You can see how its layed out below. Imgur

http://i.imgur.com/MjSc4sA.jpg

So when a user clicks signoff it'll append their name there and updated the table.

Table structure:

Imgur

My form looks like this (I use twig templating engine)

 {% for item in component %}

    <tr>
        <td>{{ item.wo_num_and_date }}</td>
        <td>{{ item.comp_name_and_number }}</td>
        <td>{{ item.finish_sizes }}</td>
        <td>{{ item.material }}</td>
        <td>{{ item.total_num_pieces }}</td>
        <td>{{ item.workorder_num_one }}</td>
        <td>{{ item.notes_one }}</td>
        <td id='signoff_userone'><input type='button' id='signoff_user_one' data-rowid={{ item.id }} value='Signoff' /> {{ item.signoff_user_one.firstname }} {{ item.signoff_user_one.lastname }}</td>
        <td>{{ item.workorder_num_two }}</td>
        <td>{{ item.notes_two }}</td>
        <td><input type='button' id='signoff_user_two' value='Signoff' /> {{ item.signoff_user_two.firstname }} {{ item.signoff_user_two.lastname }}</td>
        <td>{{ item.workorder_num_three }}</td>
        <td>{{ item.notes_three }}</td>
        <td><input type='button' id='signoff_user_three' value='Signoff' /> {{ item.signoff_user_three.firstname }} {{ item.signoff_user_three.lastname }}</td>
    </tr>

    {% endfor %}

data-rowid={{ item.id }}

This displays the id for that specific row. So each row has its own id.

I have a script that looks like this which I think I'll need to call if I use jQuery+AJAX to dynamically sign off and I'm using this update statement.

$sql = "UPDATE checklist_component_stock SET signoff_user_one = $user_id WHERE id = " . $row['id'];
mysqli_query($dbc3, $sql);

How would I go about doing this using jQuery+AJAX?

I was toying around and tried something like this but obviously it doesn't work and is probably very wrong.

  $('#signoff_user_one').click(function(){
        $.post('phplib/job_checklist_signoff.php', { rowid: {{ item.id }} }, function(data){
            console.log(data);
            $('#signoff_userone').append("<li>" + data.user.name + " (" + data.date + ")</li>");
        }, 'json');
    });

job_checklist_signoff.php

<?php
    require_once('../includes/config.php');

    $user = getuserinfo($loggedin_id);
    $row_id = mysqli_escape_string($dbc3, $_POST['rowid']);

    $sql = "UPDATE checklist_component_stock SET signoff_user_one = $user WHERE id = " . $row_id;
    mysqli_query($dbc3, $sql);


    $ret = array('rowid' => $row_id, 'user' => $user, 'date' => date('M d, Y'));
    echo json_encode($ret);
?>
  • 写回答

2条回答 默认 最新

  • dongsha7215 2014-04-08 20:41
    关注
    // Assuming "tableElement" is the id of the table owning those rows
    // This will handle clicks on any button having an id starting with "signoff_user_"
    $('#tableElement').on('click', 'input[id^="signoff_user_"]', function (e) {
        var $t = $(this);
        $.ajax({
            type: "POST",
            url: "phplib/job_checklist_signoff.php",
            data: {
                rowid: $t.data('rowid')
            },
            success: function (data) {
                console.log(data);
                var list = $t.siblings('ul');
                if (!list.length) {
                    list = $t.after($('<ul>')).next();
                }
                list.append("<li>" + data.user.name + " (" + data.date + ")</li>");
            },
            dataType: 'json'
        });
    });
    

    jsfiddle here without ajax call

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

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么