如何动态地将自动填充数据添加到发票中

时间:2015-05-04 13:26:36

标签: sql pdo autocomplete

我正在寻找一种方法将代码中的自动填充数据自动添加到“发票”页面。我希望用户也能够单击添加或删除每个项目(quickbooks样式,可用多个表单域,并动态添加),并且从长远来看,能够将元素拖动到发票中的某个位置。我现在正在寻找的是:

好:关于如何做到这一点的伪代码 最好:基本工作代码起飞。

这是我到目前为止所做的:

调用数据的页面:

<?php 

    require 'database.php';
    require 'results.php';

if(!empty($_GET['wid']))      { $wid = $_GET['wid'];  }
elseif(!empty($_POST['wid'])) { $wid = $_POST['wid']; }
else                         { $wid = null;         }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link   href="css/bootstrap.min.css" rel="stylesheet">
    <script src="js/bootstrap.min.js"></script>
    <script src="js/jquery-1.9.1.min.js"></script>
    <script src="js/engine.js"></script>
</head>

<body>
<div class="container-fluid">

                <div class="span10 offset1">
                    <div class="row">
                        <h3>Add Item to Workorder</h3>
                    </div>

                    <form class="form-horizontal" action="additems.php" method="post">
                    <?php 
                    // Add Custom Call for values from prior page ?>
                    <input type="hidden" name="wid" value="<?php echo htmlentities($wid); ?>">

                    <?php
                    //not required, but for get link purposes
                    if(isset($_POST['search']) && $_POST['search']!=''){
                        $search = $_POST['search'];
                        } elseif (isset($_GET['search']) && $_GET['search']!=''){
                        $search = $_GET['search'];
                        } else {
                        $search='';
                                }
                    //

                    echo"

                    <input type='text' class='search_input' name='search' id='search' placeholder='search' value='$search' autofocus>
                    <div id='search_output' class='search_output'>";
                    ?>  

检索结果的网页:

<?php
require_once"database.php";//connection to database
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

if(isset($_POST['search']) && $_POST['search']){
    $search=$_POST['search'];
} elseif(isset($_GET['search']) && $_GET['search']){
    $search=$_GET['search'];
}

if(isset($search)){
    $search = '%' . strtr($search, ['%' => '\%', '_' => '\_']);
    $search = str_replace(" ", "%", $search);
    $search= "%$search%";
    $sql="select * from products where `model` LIKE ? OR `category` LIKE ? OR `description` LIKE ? OR `subcategory` LIKE ? LIMIT 50;";
    $statement = $pdo->prepare($sql);
    $statement->execute([$search, $search, $search, $search]);

//configure for your custom data dump to autocomplete
echo "
<table class='table table-striped table-bordered' width='100%'>
                        <thead>
                        <tr>
                          <th>Model</th>
                          <th>Category</th>
                          <th>Description</th>
                        </tr>
                      </thead>
                      <tbody>
                      ";
    while($row = $statement->fetch()){
        $item=$row['model'];
        $title=$row['category'];
        $description=$row['description'];
        echo "  
                    <tr>
                    <td>
            <a href='?item=$item'>
            $item
            </td><td>
            $title
            </td><td>
            $description
            </a>
            </td>
            </tr>
        ";
    }
    //
}

1 个答案:

答案 0 :(得分:0)

此代码包含Javascript和AJAX代码,用于从输入进行动态搜索,并在&#39; search_output&#39;中显示sql结果。

<input type='text' class='search_input' onkeyup="performSearch(this.value);" id='search' placeholder='search' value='$search' autofocus>
<div id='search_output' class='search_output'>";                    

<script>
    function performSearch(data) { 
       if (data.length > 0) { 
          var xmlhttp = new XMLHttpRequest() { 
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("search_output").innerHTML = xmlhttp.responseText;
            }                 
          }
          xmlhttp.open("GET", "YOURPHPFILENAME.php?search=" + data, true);
          xmlhttp.send();
       } 
    } 
</script>

不幸的是,对于其他动态功能,您需要学习JQUERY / AJAX并理解HTML DOM。可能有第三方API可能会这样做。