从PHP/MySQL填充HTML加载的表单

从PHP/MySQL填充HTML加载的表单,php,html,mysql,select,Php,Html,Mysql,Select,我在这里和其他网站上读过很多帖子,它们解释了如何读取MySQL数据库并在HTML表单上显示数据。所有这些信息的问题在于,示例使用PHP构建表单。我的已存在并已加载 我的HTML/PHP: <html> <head> <title>Alpaga Wasi - Facture</title> <meta http-equiv="Content-Type" content="text/html;charset=windows-125

我在这里和其他网站上读过很多帖子,它们解释了如何读取MySQL数据库并在HTML表单上显示数据。所有这些信息的问题在于,示例使用PHP构建表单。我的已存在并已加载

我的HTML/PHP:

<html>
<head>
    <title>Alpaga Wasi - Facture</title>
    <meta http-equiv="Content-Type" content="text/html;charset=windows-1252" >
    <meta name="description" content="">
    <meta name="keywords" content="">
    <link rel="stylesheet" type="text/css" href="StyleSheet_Invoice.css"/>  
    <style>
        @media print
        {
        input.Button {display:none;}
        button.Button {display:none;}
        }
    </style>
</head>
<body>
<form action="InvoiceViewFunction.php" method="post">
<?php include("InvoiceForm.php"); ?>
<input class="Button" type="submit" value="Get Invoice" name="nGetInvoice"/>    
</form>
</body>
</html> 

阿尔帕加瓦西断裂
@媒体印刷品
{
input.Button{display:none;}
按钮{显示:无;}
}
行带来了包含表格、tr、td和所有输入字段的HTML。通过这种方式,我可以重用相同的“InvoiceForm”将数据输入数据库和检索数据库

这是我的测试代码,到目前为止,当用户单击“获取发票”按钮时,我已经从数据库中获取了数据

根据操作回答:

  • 创建一个php脚本以接收http请求并从数据库获取数据

  • 在服务器上创建一个名为api.php的php脚本
  • 复制并粘贴下面的示例并保存:


您应该研究ajax。基本上,javascript会向另一个php文件发出请求并异步返回数据。我已经对各种语言HTML、CSS、Javascript、SQL了如指掌。这个Ajax似乎包含了所有混合在一起的内容。我希望找到更简单的东西。如果你使用像jQuery这样的javascript库,这真的很简单。你需要用它的字段值来显示表单吗?我说的对吗?如果你不想使用ajax,那么你需要让表单提交操作与你提供的第一个html/php文件相同,但是有很多条件来确定数据是否存在。最好按照@AustinAllover的建议使用jQueryAjax
<?php
//Connect to database
include("../ConfigFiles/ConnectDB_local_i.php");

    //Populating the variables
    $InvoiceNo = $_POST["nInvoiceNo"];

    //Reading a specific invoice from DB
        echo "<br>Trying to read from DB with invoice = <br>" . $InvoiceNo . "<br>"; //This tells the correct number just fine.

        $query = "SELECT * FROM `invoicedata_table` WHERE InvoiceNo = '$InvoiceNo'";
        $result = $mysqli->query($query) or die($mysqli->error.__LINE__);
        if($result->num_rows > 0) 
        {
            while($row = $result->fetch_assoc()) 
            {
                echo stripslashes($row['ClientName']) . "<br>"; 
            }
        }
        else 
        {
            echo 'NO RESULTS';  
        }

//Close the DB connection
$mysqli->close();
?>
<?php 
  //--------------------------------------------------------------------------
  // Example php script for fetching data from mysql database
  //--------------------------------------------------------------------------
  $host = "localhost";
  $user = "root";
  $pass = "root";

  $databaseName = "ajax01";
  $tableName = "variables";

  //--------------------------------------------------------------------------
  // 1) Connect to mysql database
  //--------------------------------------------------------------------------
  include 'DB.php';
  $con = mysql_connect($host,$user,$pass);
  $dbs = mysql_select_db($databaseName, $con);

  //--------------------------------------------------------------------------
  // 2) Query database for data
  //--------------------------------------------------------------------------
  $result = mysql_query("SELECT * FROM $tableName");          //query
  $array = mysql_fetch_row($result);                          //fetch result    

  //--------------------------------------------------------------------------
  // 3) echo result as json 
  //--------------------------------------------------------------------------
  echo json_encode($array);

?>
<!---------------------------------------------------------------------------
Example client script for JQUERY:AJAX -> PHP:MYSQL example
---------------------------------------------------------------------------->

<html>
  <head>
    <script language="javascript" type="text/javascript" src="jquery.js"></script>
  </head>
  <body>

  <!-------------------------------------------------------------------------
  1) Create some html content that can be accessed by jquery
  -------------------------------------------------------------------------->
  <h2> Client example </h2>
  <h3>Output: </h3>
  <div id="output">this element will be accessed by jquery and this text replaced</div>

  <script id="source" language="javascript" type="text/javascript">

  $(function () 
  {
    //-----------------------------------------------------------------------
    // 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
    //-----------------------------------------------------------------------
    $.ajax({                                      
      url: 'api.php',                  //the script to call to get data          
      data: "",                        //you can insert url argumnets here to pass to api.php
                                       //for example "id=5&parent=6"
      dataType: 'json',                //data format      
      success: function(data)          //on recieve of reply
      {
        var id = data[0];              //get id
        var vname = data[1];           //get name
        //--------------------------------------------------------------------
        // 3) Update html content
        //--------------------------------------------------------------------
        $('#output').html("<b>id: </b>"+id+"<b> name: </b>"+vname); //Set output element html
        //recommend reading up on jquery selectors they are awesome 
        // http://api.jquery.com/category/selectors/
      } 
    });
  }); 

  </script>
  </body>
</html>