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

时间:2013-12-19 02:56:51

标签: 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-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> 

<?php include("InvoiceForm.php"); ?>行带来包含表,tr,td和所有输入字段的HTML。这样我可以重复使用相同的“InvoiceForm”来将数据输入数据库并检索它。

这是我到目前为止用来点击“获取发票”按钮时从数据库中获取数据的测试代码。

<?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();
?>

我在编程方面仍然很陌生。上面的代码可以很好地测试我的SQL工作正常。但是,我不希望数据简单地回显到新的空白屏幕。我希望它填充我单击“获取发票”按钮的表单。我不想在PHP中重建它,除非你的专家能告诉我这是做事的常用方法。我应该把我的SELECT放在其他地方,比如客户端javascript吗?有41个字段可以填充吗?

1 个答案:

答案 0 :(得分:1)

根据OP回答:

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

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

<?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);

?>
  • 使用JQuery AJAX创建客户端脚本以从API脚本中获取数据

    1. 在同一目录中创建一个名为client.php的html脚本,其中包含以下内容:

<!---------------------------------------------------------------------------
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>

答案仅供参考,它来自here