2015-05-04 100 views
0

我正在尋找一種方法將代碼中的自動完成數據自動添加到「發票」頁面。我希望用戶也可以點擊添加或刪除每個項目(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> 
     "; 
    } 
    // 
} 
+0

您將需要JQUERY和AJAX – Aviz

+0

我正在使用它爲每個人的自動完成建議,不是很熟悉它(更多的PHP和SQL)。你能舉一個例子說明我可能會這樣做嗎? – Steven

+0

AJAX和Javascript是唯一的方法..給我一些時間我會仔細檢查你的源代碼 – Aviz

回答

0

該代碼包含JavaScript和Ajax代碼是用於從輸入中進行動態搜索並在'search_output'中顯示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可能會這樣做。

相關問題