使用PHP将数据从表传递到模式 [英] Passing data from table to modal using PHP

查看:33
本文介绍了使用PHP将数据从表传递到模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我单击查看按钮时,在特定的人中,模态都会给我错误的数据.

Whenever I clicked the view button, in a specific person, the modal is giving me a wrong data.

我从表中单击了Paul的数据,模态显示了我的Kharen数据,我想在选择时显示正确的数据.

I clicked Paul's data from the table, and the modal is giving me Kharen's data, I want to display the correct data when selected.

仅用于显示表的PHP代码

PHP code so far for displaying of table only

<?php 
$rs = $PDOCon->prepare("SELECT * FROM applicant)
");
if ($rs->execute()) {
}else{
    echo "<script type='text/javascript'>alert('Invalid ID!');</script>";
}
$str="<div class='demo_jui'><table style='color: black;' cellpadding='0' cellspacing='0' border='1' class='display' id='tbl' class='jtable'>";
$str.="
    <thead>
        <tr>
            <th>Applicant ID</th>
            <th>Full Name</th>
            <th>Applied Job</th>
            <th>Date Applied</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>";
    while($row=$rs->fetch(PDO::FETCH_ASSOC)){
        $str.= "<tr>";
            $str.= "<td>" .( $row['people_id']) . "</td>"; 
            $people_id= $row['people_id'];
            $str.= "<td>" .($row['FName']) . " " .( $row['MName']) . " " .( $row['LName']) ."</td>";
            $LName= $row['LName'];
            $FName= $row['FName'];
            $MName= $row['MName'];
            $str.= "<td >" .( $row['job_description']) . "</td>";
            $str.= "<td >" .( $row['applicant_applied_date']) . "</td>";
            $str.= "<td ><a href='' data-toggle='modal'  data-target='#myModal'><i class='fa fa-eye'> VIEW RESUME</i></a></td> "; 
        $str.= "</tr>";
    }
        echo $str;      
    echo "</tbody></table></div>";

上面的代码将显示数据库中的数据.

Code above will display the data from the database.

Modal与第一个页面位于同一页面中.在这里:

Modal is in the same page as the first one. Here it is:

echo '<div class="modal fade" id="myModal" role="dialog" data-backdrop="static" data-keyboard="false">';
    echo '<div class="modal-dialog modal-md">';
        echo '<div class="modal-content">';
            echo '<div class="modal-header">';
                echo '<button type="button" class="close" data-dismiss="modal">&times;</button>';
                echo '<h5 class="modal-title">View Applicant Information</h5>';
            echo '</div>';
            echo '<div class="modal-body">';
                echo '<label style="font-weight: bold;"><strong>Name: </strong>'.$LName.', '.$FName.' '.$MName.'</label></br>';
                echo '<label><strong>Email Address: </strong></label> </br>';
                echo '<label><strong>Contact Number: </strong></label> </br>';

                echo '<a href=encode_view.php?id='.$people_id.' type="submit">VIEW DETAILED RESUME</a>';
                echo '<a href=print_pdf_2.php?id='.$people_id.' type="submit" class="btn btn-primary">VIEW PDF RESUME</a>';
                echo '<a href=print_pdf.php?id='.$people_id.' type="submit" class="btn btn-primary">VIEW HH PDF RESUME</a>';
            echo '</div>';
        echo '</div></div></div></div>';     ?>

到目前为止,这是我的代码,我是PHP的新手,是否应该使用Javascript来获取价值,但我对Javascript不熟悉?

This is my code so far, I'm new to PHP, should I use Javascript, in getting the value, but I'm not familiar in Javascript?

有什么主意吗?

推荐答案

您可以使用以下模板.

在视图链接中添加了人员id,以便模式可以知道要检索的特定记录.

Added people id in the view links so that the modal will know which particular record to retrieve.

替换:

$str.= "<td ><a href='' data-toggle='modal'  data-target='#myModal'><i class='fa fa-eye'> VIEW RESUME</i></a></td> "; 

使用方式:

$str.= "<td ><a href='' data-toggle='modal' data-people-id='".$people_id."' data-target='#myModal'><i class='fa fa-eye'> VIEW RESUME</i></a></td> "; 

在模式打开时,以下代码使用视图链接中的人员ID从数据库中检索记录.这将使用ajax和单独的php文件来检索相关人员的记录.

On modal open, the below code retrieves the record from database using the people id that is in the view link. This uses ajax and a separate php file to retrieve the record of the relevant person.

使用此模式处理程序:

<script>   
$('#myModal').on('show.bs.modal', function(event) {
    var button = $(event.relatedTarget);
    var people_id = button.data('people-id');
    var modal = $(this);

    $.ajax({
      type: "POST",
      url: "get_person.php",
      data: {
        'people_id': people_id,
        'submit': 'submit',
      },
      success: function(res) {
        var response = JSON.parse(res);
        var row = response.data;
        if (response.status == "success") {
          var full_name = row.FName + " " + row.MName + " " + row.LName;
          $(modal).find('.modal-body').html('<label style="font-weight: bold;"><strong>Name: </strong>'+full_name+'</label></br>');
        } else {
          alert(response.msg);
        }
      }
    });
  });
</script>

以下代码将以json格式响应请求的记录,以模式显示.

The below code will respond with the requested record in json format to display in the modal.

创建一个名为 get_person.php

if(isset($_POST['submit'])){
    $people_id = isset($_POST['people_id'])?$_POST['people_id']:0;

    if($people_id > 0){
        $dbh = new PDO('mysql:host=localhost;dbname=peeps', 'dbuser','dbpasss');
        $stmt = $dbh->prepare("SELECT * FROM tblname WHERE people_id = :people_id");
        $stmt->bindValue(":people_id", $people_id);

        if($stmt->execute()){
            $row = $stmt->fetch(PDO::FETCH_ASSOC);
            exit(json_encode(array('status'=>'success', 'data'=>$row)));
        }
    }
}

exit(json_encode(array('status'=>'fail', 'msg'=>'Failed to fetch data!')));

这篇关于使用PHP将数据从表传递到模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆