php - 如何将ajax排序结果放入html div中

标签 php javascript mysql html ajax

嗨,我已经做了一些 AJAX、PHP&MySQL 排序,它在表格中给出了结果,如下面的代码所示,我的问题是 如何将 $result 引入 html div.

请帮忙

使用 PHP 代码

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'root', '');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("security_software", $con);

$sql="SELECT * FROM internet_security ORDER by '".$q."' DESC" ;


$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>id</th>
<th>title</th>
<th>image</th>
<th>description</th>
<th>rating</th>
<th>download</th>
<th>buy</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['title'] . "</td>";
  echo "<td>" . $row['image'] . "</td>";
  echo "<td>" . $row['description'] . "</td>";
  echo "<td>" . $row['rating'] . "</td>";
  echo "<td>" . $row['download'] . "</td>";
  echo "<td>" . $row['buy'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?> 

我想要这些 HTML Div 的结果

<div class="category-container">
    <div class="category-image"></div>
    <div class="category-link"><a href="#">#</a></div>
    <div class="category-desc"><p>#</p> </div>          
    <div class="rating5" >Editors' rating: </div>        
    <div class="category-download-btn"><a href="#">Download </a></div><
    <div class="category-buy-btn"><a href="#">Buy</a></div>
</div>

最佳答案

我不知道你为什么在返回ajax响应时创建表。我建议您创建 json 响应作为 ajax 的结果。使用此结果 JSON,您可以创建表,也可以在 html 中呈现它们。 在发送ajax请求的php代码中:ajax.php

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'root', '');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("security_software", $con);

$sql="SELECT * FROM internet_security ORDER by '".$q."' DESC" ;


$result = mysql_query($sql);
$response = array();
$i=0;
while($row = mysql_fetch_array($result))
  {
  $response[$i]['id']           =$row['id'];
  $response[$i]['title']        = $row['title'];
  $response[$i]['image']        = $row['image'];
  $response[$i]['description']  = $row['description'];
  $response[$i]['rating']       = $row['rating'];
  $response[$i]['download']     = $row['download'];
  $response[$i]['buy']          = $row['buy'];
  $i++;
  }
mysql_close($con);

echo json_encode($response);
?>

在你得到这个ajax响应的html文件中,我给你提示如何使用这个ajax响应:

<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script type="text/javascript">
    $.ajax({
        url: 'ajax.php',
        dataType: 'json',
        success: function(response){
            data = '';
            $.each(response,function(i,val){
              data = '<div class="category-image">'+val.image+'</div>'+
            '<div class="category-link"><a href="#">'+val.id+'</a></div>'+
            '<div class="category-desc"><p>'+val.description+'</p> </div>'+
            '<div class="rating5" >'+val.rating+'</div>'+ 
            '<div class="category-download-btn"><a href="'+val.download+'">Download </a></div>'+
            '<div class="category-buy-btn"><a href="'+val.buy+'">Buy</a></div>';
            $('<div>').attr('id',i).html(data).appendTo('#response');
        });
            });
        }
    });
  </script>
</head>

<body>
<div id='response'></div>   
</body>
</html>

关于php - 如何将ajax排序结果放入html div中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10596947/

相关文章:

php - Laravel Eloquent 枢轴将变量传递给模型

javascript - 可以缩小 iframe 的内容吗?

python - 尝试在试用 django 项目中下载 mysql 时出错

mysql - Request Tracker 4.2.8 将数据库从 sqlite 迁移到 mysql

php - 多维数组放入ini文件中

javascript - 使用 PHP 和 jquery 创建的文件

php - 从我自己的安全系统中退出

javascript - 通过文本查找选择选项值

javascript - 使用 ng-repeat 映射嵌套值

php - 将 php 脚本更改为 PDO 导致 MySQL 更新查询期间出现语法错误