如何在按钮点击时将每条记录的id传递给模态

问题描述 投票:0回答:3

在下面的代码中,我将显示数据库中的表,每个记录都有一个编辑选项/按钮。当单击编辑按钮时,将打开一个模式,其中包含该记录的详细信息,用户可以在其中编辑数据并保存相同的数据。因此,我尝试将按钮单击的模式的id发送到模态。但我面临的问题是只发送第一条记录的ID。在任何按钮上我点击模态显示每种情况下只有第一条记录的数据。

if ($query->rowCount() > 0) {
        foreach ($results as $result) {

            ?>
                                <tr>

                                    <td width="10"><?php echo htmlentities($result->sr_no);?></td>
                                    <td width="10"><?php echo htmlentities($result->code);?></td>
                                    <td width="10"><?php echo htmlentities($result->unit_name);?></td>
                                    <?php
            $status = $result->status;
            if ($status == 'TRUE') {
                ?>
                                    <td class="v-align-middle">
                                        <div class="checkbox check-primary">
                                            <input type="checkbox" value="" id="status_true" checked> <label
                                                for="status_true"></label>
                                        </div>
                                    </td>
                                    <?php
            } else {

                ?>
                                    <td class="v-align-middle">
                                        <div class="checkbox check-primary">
                                            <input type="checkbox" value="" id="status_false"> <label
                                                for="status_false"></label>
                                        </div>
                                    </td>
                                    <?php
            }
            ?>
        <td>

        <?php  $result->code;?>
                             <button type="button" name="modify"
                                            class="btn btn-primary btn-sm btn-small" data-toggle="modal"
                                            data-target="#myModal"
                                            data-id="<?php $code_send=$result->code;?>">Modify</button> 

                <?php

            $sql = "SELECT * FROM `unit_master` where code='$code_send'";
            $query = $dbh->prepare($sql);
            $query->execute();
            $results = $query->fetchAll(PDO::FETCH_OBJ);
            $cnt = 1;
            if ($query->rowCount() > 0) {
                foreach ($results as $result) {
                    $code = $result->code;
                    $unit_name = $result->unit_name;
                    $abbbr = $result->abbreviation;
                    $app_from = $result->applicable_from;
                    $opd_no = $result->OPD_No;
                    $con_no = $result->concession_no;
                    $free_no = $result->freeship_no;
                    $credt_note_no = $result->credit_note_no;
                    $debit_note_no = $result->debit_note_no;
                    $ipd_no = $result->IPD_no;
                    $ipd_no_prefix = $result->IPD_no_prefix;
                    $receipt_no = $result->receipt_no;
                    $corp_receipt_no = $result->corp_receipt_no;
                    $adv_receipt_no = $result->adv_receipt_no;
                    $refund_receipt_no = $result->refund_receipt_no;
                }
            }
            ?>  

                                            <!-- Modal -->
                                        <form name="update" id="update" method="post"
                                            action="userindex.php" enctype="multipart/form-data">
                                            <input type="hidden" name="q" value="unit_master" /> <input
                                                type="hidden" name="flg" value="2">

                                            <div class="modal fade" id="myModal" tabindex="-1"
                                                role="dialog" aria-labelledby="myModalLabel"
                                                aria-hidden="true">
                                                <div class="modal-dialog">
                                                    <div class="modal-content">
                                                        <div class="modal-header">
                                                            <button type="button" class="close" data-dismiss="modal"
                                                                aria-hidden="true">X</button>

                                                            <h4 id="myModalLabel" class="semi-bold">Modify Unit</h4>

                                                            <br>
                                                        </div>
                                                        <div class="modal-body">
                                                            <div class="row form-row">
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $code?>" placeholder="Code"
                                                                        name="code" id="code">
                                                                </div>
                                                                <div class="col-md-8">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $unit_name?>" placeholder="Name"
                                                                        name="unit_name" id="unit_name">
                                                                </div>

                                                            </div>
                                                            <div class="row form-row">
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $abbbr?>"
                                                                        placeholder="Abbriviation" name="Abbriviation"
                                                                        id="Abbriviation">
                                                                </div>

                                                                <div class="col-md-3">
                                                                    <label> Aplicable From</label>

                                                                </div>
                                                                <div class="col-md-5">
                                                                    <div
                                                                        class="input-append success date col-md-10 col-lg-6 no-padding">
                                                                        <input type="text" class="form-control"
                                                                            value="<?php echo $app_from?>"> <span class="add-on"><span
                                                                            class="arrow" name="applicable_from"
                                                                            id="applicable_from"></span><i class="fa fa-th"></i></span>
                                                                    </div>
                                                                </div>
                                                            </div>

                                                        </div>

                                                        <center>
                                                            <h4 id="myModalLabel" class="semi-bold">OPD</h4>
                                                        </center>
                                                        <div class="modal-body">
                                                            <div class="row form-row">
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $opd_no?>" placeholder="OPD No"
                                                                        name="opd_no" id="opd_no">
                                                                </div>
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $con_no?>"
                                                                        placeholder="Concession No" name="concession_no"
                                                                        id="concession_no">
                                                                </div>
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $free_no?>"
                                                                        placeholder="Freeship No" name="freeship_no"
                                                                        id="freeship_no">
                                                                </div>

                                                            </div>
                                                            <div class="row form-row">
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $credt_note_no?>"
                                                                        placeholder="Credit Note No" name="credit_note_no"
                                                                        id="credit_note_no">
                                                                </div>
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $debit_note_no?>"
                                                                        placeholder="Debit Note No" name="debit_note_no"
                                                                        id="debit_note_no">
                                                                </div>

                                                                <div class="col-md-2">
                                                                    <div class="checkbox check-primary">
                                                                        <input type="checkbox" value="1"
                                                                            id="checkboxIsDialysis"> <label
                                                                            for="checkboxIsDialysis">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IsDialysis</label>

                                                                    </div>
                                                                </div>



                                                            </div>


                                                        </div>


                                                        <center>
                                                            <h4 id="myModalLabel" class="semi-bold">IPD</h4>
                                                        </center>
                                                        <div class="modal-body">
                                                            <div class="row form-row">
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $ipd_no?>" placeholder="IPD No"
                                                                        name="ipd_no" id="ipd_no">
                                                                </div>
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $ipd_no_prefix?>"
                                                                        placeholder="IPD No Prefix" name="ipd_no_prefix"
                                                                        id="ipd_no_prefix">
                                                                </div>
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $receipt_no?>"
                                                                        placeholder="Receipt No" name="receipt_no"
                                                                        id="receipt_no">
                                                                </div>

                                                            </div>
                                                            <div class="row form-row">
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $corp_receipt_no?>"
                                                                        placeholder="Corp Receipt No" name="corp_receipt_no"
                                                                        id="corp_receipt_no">
                                                                </div>
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $adv_receipt_no?>"
                                                                        placeholder="Adv Receipt No" name="adv_receipt_no"
                                                                        id="adv_receipt_no">
                                                                </div>
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $refund_receipt_no?>"
                                                                        placeholder="Refund Receipt No"
                                                                        name="refund_receipt_no" id="refund_receipt_no">
                                                                </div>




                                                            </div>


                                                        </div>

                                                        <div class="modal-footer">
                                                            <button type="button" class="btn btn-default"
                                                                data-dismiss="modal">Close</button>


                                                            <button type="submit" class="btn btn-primary">Save
                                                                changes</button>



                                                        </div>
                                                    </div>
                                                    <!-- /.modal-content -->
                                                </div>
                                                <!-- /.modal-dialog -->
                                            </div>
                                            <!-- /.modal -->



                                            <!-- <button type="button" class="btn btn-danger btn-sm btn-small">Delete</button> -->

                                    </td>

                                    <!--    <td width="70"><a href="functions_upload.php?eid=val=5">Delete</a></td>-->

                                </tr>
                                </form>
php
3个回答
3
投票

你可以这样做:

$(".actionEdit").on("click", function () {
        var uniqueId = $(this).attr("data-id");
        $.ajax({
            url: "path",
            type: 'POST',
            data: { uniqueId: uniqueId },
            success: function (result) {
                 //do something with result
                 $('#update').modal('show');  //open the modal window
            },
            error: function () { alert("Whooaaa! Something went wrong.."); }
        });

    });

提供一个类作为“actionEdit”并将click事件绑定到它。然后单击,您可以获取data-id并进行ajax调用以获取编辑/更新的所有值并打开模态窗口。


1
投票

首先:在数据库表中使用主键

其次:使用隐藏输入并保存主键并发送:

<form ... >
<input name="primary_key"  type="hidden" value="<?php echo ($result->PRIMARY_KEY_COLUMN); ?>">
...
..
.
<submit.....>
</form>

1
投票

或者你可以调用一个id为参数的函数

function load_details(data_id){
        $.ajax({
            url: "path",
            type: 'POST',
            data: { data_id: data_id},
            success: function (result) {
                 //do something with result
                 $('#myModal').modal('show');  //open the modal window
            },
            error: function () { alert("Oops..! Something went wrong.."); }
        });

    }
© www.soinside.com 2019 - 2024. All rights reserved.