javascript - 使用 php 和 javascript 搜索 MYSQL 并在同一个 html 表中显示结果

标签 javascript php jquery html mysql

如果下拉列表中选择的值显示 html 表中有关下拉列表中所选值的数据,我有一个下拉列表,我有搜索框,现在搜索工作正常,它显示结果而不刷新页面,问题现在是它在同一页面上显示下拉html表格和搜索值结果,但我想在同一个html表格上显示搜索结果,请参阅下面的代码,任何人都可以指导我这样做,谢谢。

<html>

<select name="client" id="client" style="margin:-8px 0 0 1px;background-color:#E8E8E8;width:104px;position: absolute;"> 
<option value="">Select Client</option>
<?php
i am connection to mysql
$sql=mysql_query("xxxxxxxxxx");
$clientid=$_GET['clientid'];
while($row=mysql_fetch_assoc($sql))
{
if(strlen($_GET['clientid'])>0 && $_GET['clientid']==$row['clientid'])
{
print' <option id="client" name="client" value="'.$row['clientid'].'">'.$row['clientid'].' </option>';
}
else{
print' <option id="client" name="client" value="'.$row['clientid'].'">'.$row['clientid'].' </option>';
}
}
?>
</select>

     <form id="lets_search" action="" style="width:0px;margin:-27px 0 0;text-align:left;">
                   <input type="text" name="region" id="region"> 
                   <input type="text" name="country" id="country">
                   <input type="submit" value="search" name="search" id="search">
          </form>

     <div id="content"></div>

    <table id="CPH_GridView1"  >
    <thead class="fixedHeader">
    <tr>
    <th style=" width:103px">Region  </th>
    <th style=" width:102px" >Country </th>

    <tbody id="fbody" class="fbody" style="width:1660px" >
    <div id="content">
    <?php
    $client_id  = $_POST['title'];
    if($client_id!=""){
    $sql_selectsupplier  = "xxxxxxxxxxx";
    echo '   <td style="width:103px" class=" '.$rows["net_id"].'">'.$rows["clientid"].'</td>
             <td style="width:102px" id="CPH_GridView1_clientid" class=" '.$rows["net_id"].'">'.$rows["region"].'</td>';

    </div>
    </tbody>
    </table>

    </html>

//javascript on the same page

<script type="text/javascript">
      $(function() {
        $("#lets_search").bind('submit',function() {
         var valueregion = $('#region').val();
          var valuecountry = $('#country').val();
          $.post('clientnetworkpricelist/testdb_query.php',{valueregion:valueregion,valuecountry:valuecountry}, function(data){
             $("#content").html(data);
           });
           return false;
        });
      });
    </script>

testdb_query.php

<?php

$dbHost = 'localhost'; // usually localhost
$dbUsername = 'xxxxxx';
$dbPassword = 'xxxxxxxxxxxx';
$dbDatabase = 'xxxxxxxxxxxxxx';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");

$region=$_POST['valueregion'];
$country=$_POST['valuecountry'];
$clientid=$_POST['clientid'];

if (strlen($region) > 0 && $region!="" ){

                $sql_search.= " AND s.region = '".$region."' ";

                }

if (strlen($country) > 0 && $country!="" ){

                $sql_search.= " AND s.country = '".$country."' ";

                }
$query = mysql_query("SELECT * FROM supplierprice s,$clientid c WHERE s.supp_price_id = c.net_id $sql_search");

echo '<table>';

while ($data = mysql_fetch_array($query)) {

  echo '
  <tr>
        <td style="font-size:18px;">'.$data["region"].'</td>        
    <td style="font-size:18px;">'.$data["country"].'</td>
</tr>';

}

echo '</table>';
?>

最佳答案

为了获得最佳实践,请将 php 代码与 html 分开 - 在渲染 html 之前从数组中的数据库获取所有数据,然后在 html 中使用 foreach 来解析每一行。

将数据库登录和连接放在不同的文件中,并在页面顶部使用 require_once() 包含它

显示错误以便更好地理解您的脚本

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

注释“i am connection to mysql”这一行,因为它会带来这种格式的错误

连接数据库后初始化

$sql_search = ""; // otehrwise it will bring a notice when calling "$sql_search.="

并检查http_request,以免首次访问没有$_POST数据的页面时出现任何错误

if ( $_SERVER['REQUEST_METHOD'] === 'POST')
{
   //code for displaying the new table with the post data
}

关于javascript - 使用 php 和 javascript 搜索 MYSQL 并在同一个 html 表中显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21451414/

相关文章:

javascript - Angular UI TimePicker 无法正常工作

php - MYSQL - 剩余数量 - 带序列号的产品 - 多表

jquery - 设置jCarousel中可见图片的数量

javascript - Bootstrap 工具提示位置(动态元素)

javascript - 如何使用 $key 按名称获取元素

javascript - jQuery 可拖动区域(圆形)

javascript - 简单视差滚动 - 内容在横幅上滑动

javascript - 使用 jquery 从 JSON 对象的所有行中删除属性?

php - 无法使用 jQuery AJAX 获取从 PHP-MYSQL 生成的数据的值

php - 我如何使用 MySQL Left Join 按另一个表的列排序?