Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用AJAX显示sql查询而不是查询结果_Php_Ajax_Postgresql - Fatal编程技术网

Php 使用AJAX显示sql查询而不是查询结果

Php 使用AJAX显示sql查询而不是查询结果,php,ajax,postgresql,Php,Ajax,Postgresql,您好,我一直在试图弄明白为什么我的代码在表中列出查询结果时不起作用。我接受了在网络上找到的代码,并对其进行了调整。我的数据存储在pgsql中。html页面有一个下拉菜单,允许选择机构名称。当我单击submit按钮了解数据库中谁属于这个机构时,php页面被加载并显示我想要发送给pgsql的SQL查询。我应该将查询结果显示在html页面上显示的表中。下拉菜单工作正常,因此我不提供此listinstitutions.php的php代码 有人告诉我应该使用ajaxsubmit,但我不知道该将此功能放在何

您好,我一直在试图弄明白为什么我的代码在表中列出查询结果时不起作用。我接受了在网络上找到的代码,并对其进行了调整。我的数据存储在pgsql中。html页面有一个下拉菜单,允许选择机构名称。当我单击submit按钮了解数据库中谁属于这个机构时,php页面被加载并显示我想要发送给pgsql的SQL查询。我应该将查询结果显示在html页面上显示的表中。下拉菜单工作正常,因此我不提供此listinstitutions.php的php代码

有人告诉我应该使用ajaxsubmit,但我不知道该将此功能放在何处。 php文件中也可能有错误,因为查询是显示的,而不是发送到pgsql。json是否正确发送

非常感谢您的指导

多谢各位

html端:

<html>
<head>

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>
<script>
$(document).ready(function(){


  //////////////////////////////////////
  // Displays insitution names in Drop Down Menu

        //Getting the selector and running the code when clicked
        $('#selinstit').click(function(e){
                 //Getting the JSON object, after it arrives the code inside
               //function(dataI) is run, dataI is the recieved object
               $.getJSON('http://localhost/listinstitutions.php',function(dataI){
                            //loop row by row in the json, key is an index and val the row
                            var items = [];  //array
                          $.each(dataI, function(key, val) {
                            //add institution name to <option>
                            items.push('<option>' + val['Iname'] + '</option>');
                        });//end each
                        //concatenate all html
                        htmlStr=items.join('');
                        console.log(htmlStr);
                        //append code
                        $('option#in').after(htmlStr);
                });//end getJSON
        });//end cluck



    ///////////////////////////////
   // Displays persons form an institution in a table

     $( "$subinst" ).button().click(function( event ) {
     console.log($(this)); // for Firebug
     $.getJSON('http://localhost/SelectPersonsBasedOnInstitution.php',function(data){   // I make an AJAX call here
     console.log($(this)[0].url); // for Firebug   check what url I get here
                            //loop row by row in the json, key is an index and val the row
                            var items = [];  //array
                          $.each(data, function(key, val) {

                        //add table rows
                            items.push('<tr border=1><td>' + val['Pfirstname'] + '</td><td>' + val['Plastname'] + '</td><td><a mailto:=" ' + val['Pemail'] + ' " >' + val['Pemail'] + '</a></td></tr>');
                        });//end each
                        //concatenate all html
                    htmlStr=items.join('');

                        //append code
                        $('#instito').after(htmlStr);
                });//end getJSON
      event.preventDefault();
      });

}); //end ready

</script>

</head>   

<body>
<form id="myForm" action="SelectPersonsBasedOnInstitution.php" method="post">
Select persons from an institution:
<br>                                            
<tr>
 <td>
   <select id="selinstit" name="instit">
   <option id="in">Select</option>                       
   </select>
 </td>
 <td>
   <input type="submit" id="subinst" value="Submit" /> 
 </td>
</tr>

</form>

   <table frame="border" id="instito">
   </table>
</body>
</html>
以下是SelectPersonsBasedOnInstitution.php的php代码

<?php


//////////
// part 1: get information from the html form
ini_set('display_errors', 1);                                      
ini_set('display_startup_errors', 1);

foreach ($_REQUEST as $key => $value){
 $$key=$value;  
}

// part2: prepare SQL query from input
$sqlquery= sprintf('SELECT "Pfirstname", "Plastname", "Pemail" FROM "PERSON"
LEFT JOIN "INSTITUTION" ON
"PERSON"."Pinstitution"="INSTITUTION"."Iinstitution"
WHERE "Iname" = \'%s\'',$instit);
echo $sqlquery;


/////////
// part3: send query
$dbh = pg_connect("host=localhost dbname=mydb user=**** password=*****");
$sql=  $sqlquery;
$result = pg_query($dbh,$sql);
$myarray = pg_fetch_all($result);

$jsontext = json_encode($myarray);
echo($jsontext);

?>

下面这一行可能是问题所在,但不应该出现:

echo $sqlquery;
重写没有那一行的代码,它应该可以工作

$sqlquery= sprintf('SELECT "Pfirstname", "Plastname", "Pemail" FROM "PERSON" LEFT JOIN "INSTITUTION" ON "PERSON"."Pinstitution"="INSTITUTION"."Iinstitution" WHERE "Iname" = \'%s\'', $instit);

$dbh = pg_connect("host=localhost dbname=mydb user=**** password=*****");
$result = pg_query($dbh, $sqlquery);
$myarray = pg_fetch_all($result);
$jsontext = json_encode($myarray);
echo($jsontext);

这个具体问题已经解决了,谢谢。现在php页面被加载,没有显示任何内容。我应该得到表id=instito,结果显示在html页面中。有没有使用ajaxsubmit解决此问题的提示?我想现在应该在html页面中。我在以下部分添加了此代码:$subinst.clickfunction{var url=;//处理表单输入的脚本。$.ajax{type:POST,url:url,data:$myForm.serialize,//序列化表单元素。成功:functiondata{alertdata;//显示来自php脚本的响应。}};返回false;//避免执行表单的实际提交。};您现在可以从SelectPersonsBasedOnInstitution.phpSorry获取所需的JSon编码文本了吗?关于上面的错误格式。在下面添加了代码并在php页面上显示了查询结果。如何在表中显示结果?[code]$subinst.clickfunction{var url=;$.ajax{type:POST,url:url,data:$myForm.serialize,success:functiondata{alertdata;}};返回false;//避免执行表单的实际提交。};