通过对数据库使用data-attr访问特定行

时间:2016-09-19 12:10:43

标签: php jquery mysql ajax bootstrap-modal

我试图根据用户点击的行显示模式,数据属性字段是根据数据库中的现有条目设置的。然后我需要将该行的数据显示在模态本身中。

点击之前,这是我的表格。

<div class="table-responsive">
                <table class="table table-bordered">

                <?php
                $list = mysqli_query($con,"SELECT * FROM notes WHERE memberid = '$membersid'");

                $rows = mysqli_num_rows($list); 

                if($rows == 0)
                {
                    ?><th id="no-notes">No Existing Notes</th><?php
                }
                else
                {?>
                    <tr>
                    <th id="note-h-date">Date</th>
                    <th id="note-h-excerpt">Excerpt</th>
                    <th id="note-h-addedby">Added By</th>
                    </tr>
                    <?php
                    if($list)
                    {
                        while($listnotes = mysqli_fetch_array($list))
                        {
                            $dateadded = new DateTime($listnotes['dateadded']);

                            ?><tr class="clickable table-sel" data-toggle="modal" data-id="<?php echo $listnotes['id'];?>" data-target="#noteModal"><?php
                            echo "<td class='note-dateadded'>".$dateadded->format('d-m-Y')."</td>";
                            echo "<td class='note-excerpt'>".substr($listnotes['note'],0,95)."...</td>";
                            echo "<td class='note-addedby'>".$listnotes['addedby']."</td>";
                            echo "</tr>";
                        }
                    }
                }


                ?>
                </table>
            </div>

这是模态代码。

<!-- Modal -->
            <div class="modal fade" id="noteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
              <div class="modal-dialog" role="document">
                <div class="modal-content">
                  <div class="modal-header">
                    <?php
                    $noteid = $_SESSION['noteid'];

                    $check = mysqli_query($con,"SELECT * FROM notes WHERE id = '$noteid'");
                    if($check){

                        $chkres = mysqli_fetch_array($check);
                        $dateadded = new DateTime($chkres['dateadded']);
                    }
                    else
                    {
                        echo "Failed to load note";
                    }
                    ?>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">Note dated: <?php echo $dateadded->format('d-m-Y');?></h4>
                  </div>
                  <div class="modal-body">
                    <textarea class="form-control" rows="5" id="fullnote"><?php echo $chkres['note'];?></textarea>
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save changes</button>
                  </div>
                </div>
              </div>
            </div>

JQuery正在尝试将其设置为会话变量,然后使用该变量从数据库中访问正确的数据。

    jQuery(document).ready(function($) {
     $(".clickable").click(function() {
        var value = $(this).attr('data-id');

        $.post("modal-post.php",{"nid": value});
        alert(value);
    });
});

任何帮助都会很棒。解释可能不太好,我道歉。

2 个答案:

答案 0 :(得分:0)

你有错误。在模态页面中,而不是:

 $noteid = $_SESSION['noteid'];
你应该

 $noteid = $_POST['nid'];

答案 1 :(得分:0)

好的,所以我设法用你们的反馈来解决我的问题并深入研究。

所以这是排序代码。

jQuery(document).ready(function($) {
$(".clickable").click(function() {
    var value = $(this).attr('data-id');
    $.ajax({
                type: "POST",
                url: "modal-post.php", //URL here
                data: { param: value },
                dataType: 'json',
                }).done(function( data ) {
                     $('#myModalLabel').html("Date Added: " + data.notedate);
                     $('#fullnote').html(data.note);
            });
    });
});

JQuery - &gt; Ajax代码

$nid = $_POST['param'];

include("connection.php");
$check = mysqli_query($con,"SELECT * FROM notes WHERE id = '$nid'");
if($check){
    $chkres = mysqli_fetch_array($check);
    $dateadded = new DateTime($chkres['dateadded']);
    echo json_encode(
        array(
            "notedate"=>$dateadded->format('d-m-y'),
            "note"=>$chkres['note']
        )
    );
}
else
{
    echo "Failed to load note";
}

然后是PHP脚本。

int snd_max(int length, int* args) {
    int max, max2;
    max = -1;
    max2 = -1;
    for (int i = 0; i< length; i++) {
        if (args[i] > max) {
            max2 = max;
            max = args[i];
        } else if (args[i] > max2 {
            max2 = args[i]
        }
    }
    return max2;

感谢。