繁体   English   中英

如何在不刷新PHP页面的情况下按按钮获取HTML文本框中的数据库值?

[英]How to fetch database values in html text boxes on button press without refreshing page in php?

我想在不刷新页面的情况下将数据库值提取到文本框中。 使用页面刷新可以正常运行,但我不想刷新它。

我的代码如下所示。

PHP

 <?php include 'Connection.php'; $sql=mysqli_query($connect,"select * from `registration`"); while ($row=mysqli_fetch_array($sql)) { echo "<tr> <td>".$row['Emp_Code']."</td> <td>".$row['Full_Name']."</td> <td>".$row['Permanent_Address']."</td> <td>".$row['Mobile_No']."</td> <tr>"; if (isset($_POST['Fetchdetails'])) { $userid = $_POST['userid']; $fetchquery = mysqli_query($connect,"select * from `registration` where `Emp_Code` = '$userid'"); if (mysqli_num_rows($sql)>0) { while ($row=mysqli_fetch_array($fetchquery)) { $employeecode=$row['Emp_Code']; $fullname=$row['Full_Name']; $permanentaddress=$row['Permanent_Address']; $mobileno=$row['Mobile_No']; } } } } ?> 

我的HTML代码是

 <html> <body> <div align="center" id="div"> <table width="400" border="1"> <tr align="right" style="background-color:#FFF;"> <td width="100">Permanent Address :&nbsp;</td> <td width="100">&nbsp; <input type="text" name="permanentaddress" id="permanentaddress" placeholder="Permanent Address" size="15" value="<?php echo $permanentaddress;?>"/>&nbsp;</td> </tr> <tr align="right" style="background-color:#FFF;"> <td width="100">Employee Code :&nbsp;</td> <td width="100">&nbsp;<input type="text" name="employeecode" id="employeecode" placeholder="Employee Code" size="15" value="<?php echo $employeecode;?>"/>&nbsp;</td> </tr> <tr align="right" style="background-color:#FFF;"> <td width="100">Full Name :&nbsp;</td> <td width="100">&nbsp;<input type="text" name="fullname" id="fullname" placeholder="Full Name" size="15" value="<?php echo $fullname;?>"/>&nbsp;</td> </tr> <tr align="right" style="background-color:#FFF;"> <td width="100">Mobile No. :&nbsp;</td> <td width="100">&nbsp;<input type="text" name="mobileno" id="mobileno" placeholder="Mobile No." size="15" value="<?php echo $mobileno;?>"/>&nbsp;</td> </tr> </table> </div> <br> <div align="center"> <input type="submit" name="Fetchdetails" value="Fetch Details" id="Fetchdetails" /> </div> </body> </html> 

如何在不刷新网页的情况下从数据库取值到文本框中?

您可以使用AJAX实现数据的动态加载,例如:

$.ajax({
  url: "yourscript.php",
  cache: false,
  success: function(html){
    $("#results").append(html);
  }
});

其中yourscript.php是您的后端脚本,将在其中执行数据库调用,并在成功请求后返回您的成功。

请注意,数据应以序列化格式交换(对于AJAX请求,最常用的方法是JSON)。

您可以通过json实现。 请尝试以下代码。

PHP代码

<?php
    include 'Connection.php';
    $userid = $_GET['userid'];
    $fetchquery = mysqli_query($connect,"select * from `registration` where `Emp_Code` = '$userid'");

    if (mysqli_num_rows($sql)>0)
    {
        while ($row=mysqli_fetch_array($fetchquery))
        {
            $return['employeecode']=$row['Emp_Code'];
            $return['fullname']=$row['Full_Name'];
            $return['permanentaddress']=$row['Permanent_Address'];
            $return['mobileno']=$row['Mobile_No'];
        }
    }

    echo json_encode($return);

JavaScript代码

$("#Fetchdetails").click(function(e) {
    e.preventDefault();
    var userId = $(this).attr('data-user-id');
    $.ajax({
        type: "GET",
        url : 'http://localhost/test/get_data.php?userid='+userId,
        dataType : 'json',
        success : function(response) {
            $('#permanentaddress').val(response.permanentaddress);
            $('#employeecode').val(response.employeecode);
            $('#fullname').val(response.fullname);
            $('#mobileno').val(response.mobileno);
        },
        error : function() {
            alert('There was an error');
        }
    });
});

注意 :您必须将以下行替换为您的php文件url。

网址:' http://localhost/test/get_data.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM