php - 单击按钮时返回每个查询的记录

标签 php mysql

我在同一用户的数据库表中有 3 条查询记录。当我执行查询时返回并显示数据库表的最新查询。 我想返回 3 个查询,始终显示最近的查询,但有一个按钮,单击该按钮可关闭正在显示的查询并打开上一个查询,依此类推,直到没有进一步的查询。 通过这种方式,我始终可以访问有关用户的每次咨询中注册的信息。

此时我返回查询如下:

<a name="view2" id="<?php echo $row["Id"]; ?>" data-toggle="modal" href="#dataModal1" class="btn btn-primary view_data2" />EGA</a>

<div id="dataModal1" class="modal fade" style="width:1000px;">  
      <div class="modal-dialog" style="width:1000px;">  
           <div class="modal-content" style="width:1000px;">  
                <div class="modal-header" style="width:1000px;">  
                     <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> 
                     <h4 class="modal-title"><strong>Estado Geral e Autonomia</strong></h4>  
                </div> 
                <div class="container"></div>       
                <div class="modal-body" id="employee_detail1">  
                </div> 
                <div class="modal-footer">  
                     <a href="#" class="btn btn-danger" data-dismiss="modal">Sair</a>  
                </div>  
           </div>  
      </div>  
 </div> 

$(document).on('click', '.view_data2', function() {
  var employee_id1 = $(this).attr("Id");
  if (employee_id1 != '') {
    $.ajax({
      url: "./select2",
      method: "POST",
      data: {
        employee_id1: employee_id1
      },
      success: function(data) {
        console.log(data);
        $('#employee_detail1').html(data);
        $('#dataModal1').modal('show');
      }
    });
  }
});

在 select2 页面上我有以下代码:

if(isset($_POST["employee_id1"]))  
{ 
    $output = '';  

    $query = "SELECT * FROM centrodb.PsicUtentes WHERE centrodb.PsicUtentes.Id = '".$_POST["employee_id1"]."'"; 
    $result = mysqli_query($conn, $query);  
    $output;  
    while($row = mysqli_fetch_array($result))  
    {
        $output .= '
            <h4 class="modal-title">Identificação do Utente</h4>  
            <form method="post" id="insert_form2">
                <fieldset class="grupo">
                <table class="campo" cellspacing="10">
                 <tr>
                  <td>                      
                  <label>Data</label>  
                  <input type="text" id="Data1" name="Data" class="form-control" value="'.$row["Data"].'" style="width:150px;" />  
                  </td> 
                  <td>                      
                  <label>Código Utente</label>  
                  <input type="number" id="CodigoUtente1" name="CodigoUtente" value="'.$row["CodigoUtente"].'" class="form-control" style="width:100px;"/>  
                  </td>
                  <td>                        
                  <label>Nome Utente</label>  
                  <input type="text" id="Nome1" name="Nome" value="'.$row["Nome"].'" class="form-control" class="form-control" style="width:400px;"/>   
                  </td>
                  <td>  
                  <label>Data Nascimento</label>  
                  <input type="date" id="DataNasc1" name="DataNasc" value="'.$row["DataNasc"].'" class="form-control" style="width:150px;"/>   
                  </td>
              </tr>
            </table>
            </fieldset>   
            </form>                  
           ';  
    }  
    $output;  
    echo $output;  
}   

当我有三个查询时,我将在页面上显示图像:

enter image description here

我打算有一个类似红色包围的按钮,每当我点击时,我都会关闭正在显示的查询信息并打开上一个查询。

我正在尝试的解决方案。

<script type="text/javascript"> 
$(document).ready(function(){ 

$('.conteudo').hide(); 

$('.exibir').each(function(i){ 
$(this).click(function(){ 
$('.conteudo').each(function(j){ 
if(i == j) $(this).show('slow'); 
}); 
}); 
}); 
$('.ocultar').each(function(i){ 
$(this).click(function(){ 
$('.conteudo').each(function(j){ 
if(i == j) $(this).hide('slow'); 
}); 
}); 
}); 
}); 
</script> 

if(isset($_POST["employee_id1"]))  
 { 
$output = '';  

$query = "SELECT * FROM centrodb.PsicUtentes WHERE centrodb.PsicUtentes.Id = '".$_POST["employee_id1"]."'"; 
      $result = mysqli_query($conn, $query);  
      $output;  
      while($row = mysqli_fetch_array($result))  
      {
      $output .= '

                <h4 class="modal-title">Identificação do Utente</h4>
<div> 
<a class="exibir" href="#">Ver</a>-- 
<a href="#" class="ocultar">Ocultar</a>
</div> 

<div class="conteudo"> 
<form method="post" id="insert_form2">
                        <fieldset class="grupo">
                        <table class="campo" cellspacing="10">
                         <tr>
                          <td>                      
                          <label>Data</label>  
                          <input type="text" id="Data1" name="Data" class="form-control" value="'.$row["Data"].'" style="width:150px;" />  
                          </td> 
                          <td>                      
                          <label>Código Utente</label>  
                          <input type="number" id="CodigoUtente1" name="CodigoUtente" value="'.$row["CodigoUtente"].'" class="form-control" style="width:100px;"/>  
                          </td>
                          <td>                        
                          <label>Nome Utente</label>  
                          <input type="text" id="Nome1" name="Nome" value="'.$row["Nome"].'" class="form-control" class="form-control" style="width:400px;"/>   
                          </td>
                          <td>  
                          <label>Data Nascimento</label>  
                          <input type="date" id="DataNasc1" name="DataNasc" value="'.$row["DataNasc"].'" class="form-control" style="width:150px;"/>   
                          </td>
                          </tr>
                        </table>
                        </fieldset>   
                        </form>    

<div>
';  
      }  
      $output;  
      echo $output;  
 }

事情就是这样,但我还是想把一件事做得更好。我看到了第一条记录,但是当我看到第二条记录时,第一条记录总是打开的,当我打开第二条记录时应该隐藏起来。第二条记录在我单击查看时打开,在我单击隐藏时隐藏。 我还想要编辑和新建按钮,总是根据我打开的 div,在我的项目中,如果有两个注册表,则在项目开头打开这两个按钮,并且应该在我打开 div 时打开,我在图像中显示:

当我第一次打开时:

enter image description here

当我看到第一条记录时:

enter image description here

当我看到第二条记录时:

enter image description here

最佳答案

你好,你可以做的是在你的 html 内容中隐藏你的模式,然后用你的 ajax 填充它,然后一旦你有了数据就可以显示它,你可以这样做:

你的 php 脚本:

if (isset($_POST["action"])) {
        $action = $_POST["action"];
        switch ($action) {
            case 'SLC':
                if (isset($_POST["id"])) {
                    $id = $_POST["id"];
                    if (is_int($id)) {
                        $query = "select * from client where clientId = '$id' ";
                        $update = mysqli_query($mysqli, $query);
                        $response = array();
                        while($row = mysqli_fetch_array($update)){
                        .......
                        // you need to fill the response array here with the info you 
                        //want to display and send it with json encode later

                        }

                       echo json_encode($response);
                    }
                }
                break;

        }
    }

其中 action 可以是你想要执行 SLC、UPD、DEL 等的命令,id 是一个参数

然后在你的ajax中:

function getInfo(id) {

   return $.ajax({
        type: "POST",
        url: "getInfo.php",
        data: { action: "SLC", id:id }
    })

}

然后这样调用它:

getInfo(id).done(function(response){
var data=JSON.parse(response);
if (data != null) {
$('#form_id').trigger("reset");
//fill your modal form using your data and display the modal after reset the form
$("#modal").show();
}
})

希望对你有帮助

关于php - 单击按钮时返回每个查询的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54655800/

相关文章:

Mysql 选择具有不同选项的查询

MySQL UPDATE 多个 IF 条件

PHP 允许的内存大小 内存大小耗尽

php - 使用 Swift_Message 发送邮件时如何更改发件人的姓名文本?

php - 如何为具有 HTTPS 的子域设置 php.ini 和 session.php 的变量以使用共享 session ?

php - Yii:根据下拉列表选择更改输入文本字段值

mysql 如果不等于0则从列中选择,否则从另一列中选择

mysql - 循环多次求和mysql

php - 如何在 iOS 应用程序中显示重音字符实体?

php - PHP7 中类型声明前的问号(?string 或?int)的用途是什么?