How would I control PostgreSQL via a web page?

时间:2017-04-10 01:49:36

标签: ajax postgresql

I know I would need to use AJAX to update the web page with the results. My idea is to run queries and send back results to the form. Usually this will simply be the success or failure of the query but sometimes text would need to be returned. The platform is Linux.

2 个答案:

答案 0 :(得分:1)

你可以使用spring boot + hibernate来构建一个不错的应用程序。适用于postgresql。一个好的方法是实现@restcontroller

答案 1 :(得分:0)

此示例基于网站https://www.w3schools.com

上的示例

getuser.php

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

这是一个简单的例子,供您了解基础知识。如果你想要更多帮助开始编程,如果你有任何困难,请向社区询问。

在上面的示例中,当用户在上面的下拉列表中选择一个人时,会执行一个名为“showUser()”的函数。

该函数由onchange事件触发。

代码说明:

首先,检查人是否被选中。如果未选择任何人(str ==“”),请清除txtHint的内容并退出该功能。如果选择了某个人,请执行以下操作:

1)创建XMLHttpRequest对象

2)创建服务器响应准备就绪时要执行的功能

3)将请求发送到服务器上的文件

请注意,参数(q)已添加到URL(包含下拉列表的内容)

上面的JavaScript调用的服务器上的页面是一个名为“getuser.php”的PHP文件。

“getuser.php”中的源代码在PostgreSQL数据库上运行preaper执行,并将结果返回到HTML表中。

说明:当查询从JavaScript发送到PHP文件时,会发生以下情况:

1)PHP打开与PostgreSQL服务器的连接

2)找到了正确的人

3)创建一个HTML表格,填充数据,然后发送回“txtHint”占位符。

相关问题