php / mysql / ajax:在提交表单的页面中显示查询结果

时间:2017-05-04 15:33:01

标签: php jquery mysql ajax mysqli

我有一个表单,我想用它来填充select语句中的where列表。那部分工作正常。但是,我想用AJAX提交表单,然后将结果显示在“peers”div中。我抓了一下,但它似乎没有做任何事情。谢谢! HTML:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>990 Report Builder Demo</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#tabs" ).tabs();
    $( "#accordion" ).accordion();

  } );   
  </script>

    <script>
      $(function () {

        $('form').on('submit', function (e) {

          e.preventDefault();

          $.ajax({
            type: 'post',
            url: 'search.php',
            data: $('form').serialize(),
            success: function () {
              $('#peers').html(response);
            }
          });

        });

      });
    </script>
</head>
<body>

<div id="accordion">
  <h3>Entity Filter</h3>
  <div>
    <p>
<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Total Entities (EIN Count):";
$sql_ein_count = "SELECT COUNT(EIN) as total FROM filing";
$result_ein_count = $conn->query($sql_ein_count);

        if ($result_ein_count->num_rows > 0) {
    // output data of each row
    while($row = $result_ein_count->fetch_assoc()) {
        $total = $row["total"];
        echo number_format($total);
    }
} else {
    echo "0 results";
}
echo "<form><table><tr><td>Filing Year:</td><td><select name='year'>";
$sql_years = "SELECT YEAR(TaxPeriod) as year, COUNT(EIN) as total FROM filing GROUP BY YEAR(TaxPeriod)";
$result_years = $conn->query($sql_years);

if ($result_years->num_rows > 0) {
    // output data of each row
    while($row = $result_years->fetch_assoc()) {
        $year = $row["year"];    
        echo '<option value="'.$year.'">'.$year.'</option>';
    }
} else {
    echo "0 results";
}
echo "</select></td></tr>";
echo '<tr><td>Operating Revenue:</td><td> <select name="revcut"><option value="xs">0 - $499,999</option><option value="sm">$500,000-$2,499,999</option><option value="med">$2,500,000-$4,999,999</option><option value="lg">$5,000,000-$24,999,999</option><option value="xl">$25,000,000+</option></select></tr>';
echo '<tr><td>Keyword:</td><td> <input type="text" size="30" name="keyword"></td></tr></table><button type="submit">Find Peers</button></form>';

$conn->close();

echo "Filtered Entity Count: ";
        echo '<div id="entCount">count</div>';
?>
           </p>
  </div>
<h3>Peer Selection</h3>
  <div id="peers">

  </div>
  <h3>Results</h3>
  <div>
    <div id="tabs">
        <ul>
        <li><a href="#tabs-1">Financial Metrics</a></li>
        <li><a href="#tabs-2">Executive Compensation</a></li>
        </ul>
   <div id="tabs-1">
    Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis.
    Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero
    ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis
    lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.
    </div>
    <div id="tabs-2">
    Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis.
    Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero
    ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis
    lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.
    </div>
    </div>
  </div>
</div>

PHP:

<?php
$year=$_POST['year'];
$keyword=$_POST['keyword'];
$servername = "";
$username = "";
$password = "";
$dbname = "";

// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql_big_selects = "SET SQL_BIG_SELECTS=1";
$conn->query($sql_big_selects);
$sql_ent_list = "SELECT OrganizationName AS OrgName FROM header h JOIN filing f on h.FilerEIN=f.EIN WHERE OrganizationName LIKE '%".$keyword."%' AND TaxYr=".$year;
echo $sql_ent_list;
$result_ent_list = $conn->query($sql_ent_list);

        if ($result_ent_list->num_rows > 0) {
    // output data of each row
    while($row = $result_ent_list->fetch_assoc()) {
        $peer = $row["OrgName"];
        echo $peer;
    }
} else {
    echo "0 results";
}
$conn->close();
?>

0 个答案:

没有答案