【问题标题】:How to include the output from database query in div using GET如何使用 GET 在 div 中包含数据库查询的输出
【发布时间】:2017-11-13 12:21:45
【问题描述】:

我有 index.html 和下面的代码

<html>
<head><title>Testing</title></head>
<body>
<script>
  var javascriptVariable = "abc";
  window.location.href = "test_selected.php?name=" + javascriptVariable;
</script>
</body>
</html>

和 test_selected 包含代码的文件

<?php

$host = 'localhost';
$port = '5432';
$database = 'postgis';
$user = 'postgres';
$password = 'postgres';
$reg_name=$_GET['name'];

$connectString = 'host=' . $host . ' port=' . $port . ' dbname=' . $database . 
    ' user=' . $user . ' password=' . $password;


$link = pg_connect ($connectString);
if (!$link)
{
    die('Error: Could not connect: ' . pg_last_error());
}
$query="select * from table where name='{$reg_name}'";
$result = pg_query($query);

$i = 0;
echo '<html><body><table border="1"><tr>';
while ($i < pg_num_fields($result))
{
    $fieldName = pg_field_name($result, $i);
    echo '<td>' . $fieldName . '</td>';
    $i = $i + 1;
}
echo '</tr>';
$i = 0;

while ($row = pg_fetch_row($result)) 
{
    echo '<tr>';
    $count = count($row);
    $y = 0;
    while ($y < $count)
    {
        $c_row = current($row);
        echo '<td>' . $c_row . '</td>';
        next($row);
        $y = $y + 1;
    }
    echo '</tr>';
    $i = $i + 1;
}
pg_free_result($result);

echo '</table></body></html>';
?>

输出显示在新页面中,因为我使用了 windows.location。是否可以在 div 的同一页面中显示输出而不是另一个新页面。

【问题讨论】:

标签: javascript php html postgis pg-query


【解决方案1】:

在页面中包含 jquery。并替换

window.location.href = "test_selected.php?name=" + javascriptVariable;

 $.ajax({
    type:'GET'
  url: "test_selected.php?name=" + javascriptVariable,
  success: function(result){
        $("#div_id").html(result);
},error:(error){
    console.log(error);
});

将 div_id 替换为要加载到的 div 的 id。

【讨论】:

    猜你喜欢
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多