2017-04-10 33 views
0

我在服務器上有一個MySQL數據庫。這是我的HTML商店在DB用jQuery插入數據庫並獲得反饋

var shV = localStorage.getItem("PersName"); 
var idV = localStorage.getItem("codeQR"); 
var posV = localStorage.getItem("position"); 

window.open('http://*.es/insert.php?sh='+shV+'&id='+idV+'&pos='+posV, 
'blank','location=no,hidden=yes,closebuttoncaption=Done,toolbar=no'); 

事情怎麼Insert.PHP文件

<?php 
try { 
$pdo = new PDO('mysql:host=mysql.**'); 
    $shortV = $_GET['sh']; 
    $idnumberV = $_GET['id']; 
    $positionV = $_GET['pos']; 
$statement = $pdo->prepare("INSERT INTO idtabelle (short, idnumber, position) 
VALUES (:sh, :id, :pos)"); 
$statement->execute(array('sh' => $shortV, 'id' => $idnumberV, 'pos' => $positionV)); 
echo "$idnumberV eingetragen"; 
    $pdo = null; 
    } catch (PDOException $e) { 
    die($e -> getMessage()); 
    } 
?> 

我想將它們存儲,而無需打開新頁面或顯示他們插入網址。在數據庫中之後,我只希望得到像「您的數據已輸入」這樣的反饋,只有當他們真的在數據庫中。

在其他HTML中,我通過在文本框中輸入ID來搜索位置。它像我想要的那樣工作。在頁面中給出反饋。

Search.html

<form id="search" name="search" method="post"> 
    <input type="text" id="txt1" name="search_input"> 
    <button type="submit" class="btn btn-info" id="bt1">Get Position</button> 
    </form> 
    <div id="div1">Testing</div> 
     <div id="div2"> Here </div> 
    <script> 
      $(document).ready(function(){ 
       $("#search").on("submit", function(e){ 
        e.preventDefault(); 
        $.post("/search3.php", $("#search").serialize(), function(d){ 
         if (!$.trim(d)){ 
          alert("Nothing found !"); } 
         else{ 
          $("#div1").empty().append(d); 
          localStorage.setItem("PosGet",d); 
          document.getElementById("div2").innerHTML=(d); 
           } 
         }); 
        }); 
      }); 

的search.php中文件

<?php 
try { 
$pdo = new PDO('mysql:host=mysql.**'); 
    $pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    $idV = (isset($_POST['search_input'])) ? $idV = $_POST['search_input'] : exit('The search was empty'); 
    $statement = $pdo->prepare("SELECT position, short FROM idtabelle WHERE idnumber = ?"); 
    $statement->bindParam(1, $idV); 
    $statement->execute(); 
    foreach($statement -> fetchAll() as $row){ 
    echo $row['position']; 
    } 
    $pdo = null; 
    } catch (PDOException $e) { 
    die($e -> getMessage()); 
    } 
?> 

我怎麼能做到這一點的是,插入就像上面搜索在頁面和一個反饋? 謝謝。

+0

你也可以像選擇步驟一樣使用ajax – hassan

回答

0

在您的第一個示例中,該代碼運行一個新窗口(但隱藏),因此您無法從初始窗口獲得結果。你應該像在第二個例子中那樣做。

如果你可以使用jQuery(你的例子看起來像)你可以使用jQuery.ajax()函數

$.ajax({ 
    method: "POST", 
    url: "some.php", 
    data: { name: "John", location: "Boston" } 
}) 

你也可以有成功和失敗的回調。例如:

$.ajax({ 
    method: "POST", 
    url: "some.php", 
    data: { name: "John", location: "Boston" }, 
    function(){ 
     console.log("success"); 
    }, 
    function(){ 
     console.log("error") 
    } 
}) 
0
<!DOCTYPE html> 
<html> 
<head> 
<style> 
table { 
    width: 100%; 
    border-collapse: collapse; 
} 

table, td, th { 
    border: 1px solid black; 
    padding: 5px; 
} 

th {text-align: left;} 
</style> 
</head> 
<body> 

<?php 
$q = intval($_GET['q']); 

$dbconn = pg_connect('localhost','peter','abc123','my_db'); 
if (!$con) { 
    die('Could not connect'); 
} 



$query = "SELECT * FROM user WHERE id = $1"; 
$result = pg_prepare($dbconn, "my_query", $query); 

$data = array($q); 
$result = pg_execute($dbconn, "my_query", $data); 


echo "<table> 
<tr> 
<th>Firstname</th> 
<th>Lastname</th> 
<th>Age</th> 
<th>Hometown</th> 
<th>Job</th> 
</tr>"; 
while($row = pg_fetch_row($result)) { 
    echo "<tr>"; 
    echo "<td>" . $row[0] . "</td>"; 
    echo "<td>" . $row[1] . "</td>"; 
    echo "<td>" . $row[2] . "</td>"; 
    echo "<td>" . $row[3] . "</td>"; 
    echo "<td>" . $row[4] . "</td>"; 
    echo "</tr>"; 
} 
echo "</table>"; 
pg_close($dbconn); 
?> 
</body> 
</html> 

在HTML

<html> 
<head> 
<script> 
function showUser(str) { 
    if (str == "") { 
     document.getElementById("txtHint").innerHTML = ""; 
     return; 
    } else { 
     if (window.XMLHttpRequest) { 
      // code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp = new XMLHttpRequest(); 
     } else { 
      // code for IE6, IE5 
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     xmlhttp.onreadystatechange = function() { 
      if (this.readyState == 4 && this.status == 200) { 
       document.getElementById("txtHint").innerHTML = this.responseText; 
      } 
     }; 
     xmlhttp.open("GET","getuser.php?q="+str,true); 
     xmlhttp.send(); 
    } 
} 
</script> 
</head> 
<body> 

<form> 
<select name="users" onchange="showUser(this.value)"> 
    <option value="">Select a person:</option> 
    <option value="1">Peter Griffin</option> 
    <option value="2">Lois Griffin</option> 
    <option value="3">Joseph Swanson</option> 
    <option value="4">Glenn Quagmire</option> 
    </select> 
</form> 
<br> 
<div id="txtHint"><b>Person info will be listed here...</b></div> 

</body> 
</html> 

這是使用PostgreSQL,你要了解的基礎知識一個簡單的例子。如果你想更多的幫助開始編程,並且如果你有任何困難問社區。

在上例中,當用戶在上面的下拉列表中選擇一個人時,會執行一個名爲「showUser()」的函數。

該函數由onchange事件觸發。代碼說明:

代碼說明:

首先,檢查是否有人被選中。如果沒有人被選中(str ==「」),清除txtHint的內容並退出該功能。如果選擇了一個人,執行下列操作:

1)創建一個XMLHttpRequest對象

2)創建的函數被執行時,服務器響應就緒

3)發送請求關閉到在服務器上的文件

注意到一個參數(q)被添加到URL(與下拉列表中的內容)

通過上述JavaScript調用服務器上的網頁被稱爲「的getUser PHP文件.PHP」。

「getuser.php」中的源代碼在PostgreSQL數據庫上運行更便宜的執行,並將結果返回到HTML表中。

說明:當查詢與JavaScript發送到PHP文件時,會發生以下情況:

1)PHP打開正確的人發現

一個PostgreSQL服務器

2)的連接

3)創建一個HTML表格,填充數據併發送回「txtHint」佔位符。