-1

我有带有以下代码的 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 的同一页面中而不是另一个新页面中显示输出。

4

1 回答 1

0

在页面中包含 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。

于 2017-11-13T12:49:31.593 回答