简体   繁体   English

获取显示的SQL查询,而不是使用AJAX查询结果

[英]get sql query displayed rather than query result using AJAX

Hello I have been trying to figure out why my code aimed at listing a query result in a table does not work. 您好,我一直试图弄清楚为什么我的代码旨在在表中列出查询结果不起作用。 I took code found on the web and tired to adapt it. 我接受了在网上找到的代码,并厌倦了适应它。 My data is stored in pgsql. 我的数据存储在pgsql中。 The html page has a drop down menu that allows selecting an institution name. html页面具有一个下拉菜单,允许您选择机构名称。 When I click the submit button to know who belongs to this institution in my database, the php page is loaded and displays the SQL query I want to send to pgsql. 当我单击提交按钮以了解谁属于我的数据库中的该机构时,将加载php页面并显示我想发送给pgsql的SQL查询。 I should get the result of the query displayed in a table displayed on the html page instead. 我应该将查询结果显示在html页面上显示的表中。 The drop down menu works correctly so I do not provide the php code for this one (listinstitutions.php) 下拉菜单正常工作,因此我不为此提供php代码(listinstitutions.php)

A person told me I should use ajaxsubmit() but I do not know where to put this function. 一个人告诉我,我应该使用ajaxsubmit(),但是我不知道将这个函数放在哪里。 There may be an error in the php file also since the query is displayed rather than being sent to pgsql. 由于查询是显示而不是发送到pgsql的,因此php文件中也可能存在错误。 Is the json correctly sent? json是否正确发送?

Your guidance would be very much appreciated. 您的指导将不胜感激。

Thank you. 谢谢。

The html side: 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>

Here is the php code for SelectPersonsBasedOnInstitution.php 这是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);

?>

The following line is likely to be the problem (it shouldn't be there): 以下行可能是问题所在(不应存在):

echo $sqlquery;

Rewrite your code without that line and it should work. 不用该行重写代码,它应该可以工作。

$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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM