【问题标题】:AJAX PHP Array from a SQL DatabaseSQL 数据库中的 AJAX PHP 数组
【发布时间】:2013-03-05 00:47:11
【问题描述】:

*我知道 MYSQL 被贬低了。我现在只是将它用作学习工具。

更新问题:

我更新了我的问题以使 lil 更有意义...

假设我想将输出数组(PHP 文件中的 json 数据)添加到 div id rvotes 中,如何在我的 HTML 页面上显示输出数组(PHP 文件中的 json 数据)?

JavaScript

<script>
$(document).ready(function () {
$('.answer').click(function (e) {
var color = $(this).attr("data-color");
$.ajax({
type: 'POST',
url: 'mm.php',
data: { color: color},
dataType: 'json',
cache: false,
success: function(showVotes) {
$('#rvotes').html(row[0]);
},
error: function (jqXHR) {

}

})
})
});
</script>

PHP

function showVotes()
{
 $sql = "SELECT * FROM mms";
 $result = mysql_query($sql) or die(mysql_error());
 $showresult = mysql_query("SELECT * from mms") or die("Invalid query: " . mysql_error());
 $response = array();
 while($row = mysql_fetch_assoc($showresult)) 
 $results[] = $row; 
 echo json_encode($results);
}

添加我的 HTML 代码

<div id="wrapper">

<div id="red" data-color="red" class="answer">
<a href="#"><img src="images/red.jpg" width="100%" /></a>
</div>

<div id="blue" data-color="blue" class="answer">
<a href="#"><img src="images/blue.jpg" width="100%" /></a>
</div>

<div id="green" data-color="green" class="answer">
<a href="#"><img src="images/green.jpg" width="100%" /></a>
</div>

<div id=rvotes>
TEST
</div>

<div id=bvotes>
TEST
</div>

如何将数组的输出显示回我的 HTML 页面?

【问题讨论】:

  • 那么你的问题是什么?
  • MySql 本身就很好。另外,我同意@JohnConde - 你在问什么?
  • 我编辑了文本。基本上我正在尝试检索 PHP 页面上 PHP 数组中的数据并将其拉入 HTML 文档。

标签: php ajax json


【解决方案1】:

我认为这与这个问题相似。

Get data from php array - AJAX - jQuery

您无法访问 php 数组,因为它在您的 ajax 中。您需要首先将其表示为 JSON,方法是将您的 php 数组传递给 json_encode() 函数并回显它。

echo json_encode($results);

它将作为参数传递给您的 ajax 回调。

在你成功的ajax回调中,

success: function(showVotes) {
$('#rvotes').html(showVotes[0]);
},

【讨论】:

  • 这不是评论材料比回答材料多吗?
  • 但是您是否能够确认它从 php 中获取了正确的数据?
  • #rvotes 是我的 div.... 我的 sql 看起来不错,它返回 [{"id":"1","color":"red","votes":"34 "},{"id":"2","color":"blue","votes":"28"},{"id":"3","color":"green","votes": "41"}] 如果您单独加载 php 页面并打印它
  • 首先的问题是你没有在你的代码中调用你的php函数。
  • 您的代码中没有 showResults。你是说showVotes吗?它没有在您的代码中作为函数调用。
【解决方案2】:

就个人而言,我会让我的 php 像这样吐出预渲染的 html:

 $sql = "SELECT * FROM mms";
 $result = mysql_query($sql) or die(mysql_error());
    //I cleaned up redundant queries here...
    //echo top border
   echo "<table border='1'>
   <tr>
   <th>Color</th>
   <th>Votes</th>
   </tr>";
 while($row = mysql_fetch_assoc($showresult)) 
while($row = mysql_fetch_assoc($showresult)
  {
  echo "<tr>";
  echo "<td>" . stripslashes($row['color']) . "</td>";
  echo "<td>" . stripslashes($row['votes']) . "</td>";
  echo "</tr>";
  }

在它吐出 html 之后,只需这样做:

<script>
$(document).ready(function () {
$('.answer').click(function (e) {
var color = $(this).attr("data-color");
$.ajax({
type: 'POST',
url: 'mm.php',
data: { color: color},
success: function (showVotes) {
$("#votes").html(showVotes); //this edits the text inside the div to be whatever your php spits out
},
error: function (jqXHR) {
}

})
})
});
</script>
<div id="votes></div>

【讨论】:

    猜你喜欢
    • 2015-04-19
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    相关资源
    最近更新 更多