簡體   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