javascript、php、mysql数据显示在列表中
我试图用php和java脚本显示mysql中的数据。我设法在页面上显示数据,但无法将其发送到脚本。下面是我的档案 script1.jsjavascript、php、mysql数据显示在列表中,javascript,php,mysql,Javascript,Php,Mysql,我试图用php和java脚本显示mysql中的数据。我设法在页面上显示数据,但无法将其发送到脚本。下面是我的档案 script1.js $(document).ready( function() { done(); }); function done() { setTimeout( function() { updates(); done(); }, 200); } function updates() { $.getJSON("
$(document).ready( function() {
done();
});
function done() {
setTimeout( function() {
updates();
done();
}, 200);
}
function updates() {
$.getJSON("cocktail.php", function(data){
$("ul") .empty ();
$each(data.result, function(){
$("ul").append("<li>Poza: "+this['poza']+"</li> <li>Nume: "+this['nume']+"</li><li>Compozitie: "+this['compozitie']+"</li><br/>");
});
});
}
$(文档).ready(函数(){
完成();
});
函数完成(){
setTimeout(函数(){
更新();
完成();
}, 200);
}
函数更新(){
$.getJSON(“cockbox.php”,函数(数据){
$(“ul”).empty();
$each(data.result,function(){
$(“ul”)。追加(“Poza:“+this['Poza']+” Nume:“+this['Nume']+” Compozitie:“+this['Compozitie']+”
);
});
});
}
鸡尾酒会
<?php include ('includes/header_js.php');?>
<?php include_once ('includes/connection.php');?>
<div class="body_bg">
<h2>Arta Cocktail-urilor</h2>
<div class="clr"> </div>
<?php
$sql = "SELECT * FROM cocktail";
$res = mysql_query($sql);
$result = array ();
while($row = mysql_fetch_array ($res) )
{
array_push($result, array('poza' => $row[1],
'nume' => $row[2],
'compozitie' =>$row[3]));
}
echo json_encode(array("result" => $result));
?>
</div><!--end of body_bg-->
<?php include ('includes/footer.php'); ?>
阿尔塔鸡尾酒酒店
connection.php
<?php
$connection = mysql_connect('localhost', 'root'. '');
if(!$connection){
die('Nu s-a putut conecta la baza de date.' .mysql_error());
}
$db_select = mysql_select_db('first_class', $connection);
if(!$db_select){
die('Eroare de conexiune'.mysql_error());
}
?>
header.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>First Class Cocktail</title>
<link rel="stylesheet" href="stylesheets/style.css" type="text/css" />
</head>
<body>
<table></table>
<script type="text/javascript" src="javascripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="javascripts/script1.js"></script>
<div class="container">
<div class="header">
</div><!--end of logo-->
<div class="menu">
<ul>
<li><a href="index.php" class="active"><span>Acasa </span></a></li>
<li><a href="galerie.php"><span>Galerie</span></a></li>
<li><a href="cocktail.php"><span>Cocktail</span></a></li>
<li><a href="about.php"><span> Despre Noi </span></a></li>
<li><a href="contact.php"><span> Contact </span></a></li>
</ul>
</div><!--end of menu-->
<div class="clr"></div><!--end of clr-->
头等鸡尾酒
我在哪里失败了???提前感谢您浪费时间阅读本文!
谢谢大家! 您正在生成一个多级数组:
array_push($result, array('poza' => $row[1], etc...);
将创建一个类似于:
$result = array(
0 => array('poza' => ....)
);
意思是你必须使用
$("ul").append("<li>Poza: "+this[0]['poza']+ etc...);
^^^---- note this
同样,您的JS代码似乎假设只有一行数据从数据库中出来,但您的数据库处理代码被设置为处理多行数据。我无法判断哪一个是正确的,但您应该知道。问题是
cockbox.php
包含HTML,而$.getJSON
函数需要JSON(且仅限于JSON)。对于类似于application/JSON
的JSON,您还应该返回一个有效的Content-Type
头。最后,您的cockbox.php
将如下所示:
<?
include_once('includes/connection.php');
header('Content-Type: application/json');
$sql = 'SELECT poza, nume, compozitie FROM cocktail';
$res = mysql_query($sql);
$results = array();
while( $row = mysql_fetch_assoc($res) ) {
$results[] = $row;
}
echo json_encode( array('results' => $results) );
?>
$(document).ready(function () {
var updateList = function () {
$.getJSON('cocktail.php', function( data ) {
var list = $('ul');
list.empty();
$.each(data.results, function( index, result ) {
list.append(
'<li>Poza: ' + result.poza + '</li>' +
'<li>Nume: ' + result.nume + '</li>' +
'<li>Compozitie: ' + result.compozitie + '</li>'
);
});
});
};
window.setInterval(updateList, 200);
});
应该是
$.each(data.result, function ( index, value ) {
你也应该更换
this['poza']
与
最后,您的JavaScript将如下所示:
<?
include_once('includes/connection.php');
header('Content-Type: application/json');
$sql = 'SELECT poza, nume, compozitie FROM cocktail';
$res = mysql_query($sql);
$results = array();
while( $row = mysql_fetch_assoc($res) ) {
$results[] = $row;
}
echo json_encode( array('results' => $results) );
?>
$(document).ready(function () {
var updateList = function () {
$.getJSON('cocktail.php', function( data ) {
var list = $('ul');
list.empty();
$.each(data.results, function( index, result ) {
list.append(
'<li>Poza: ' + result.poza + '</li>' +
'<li>Nume: ' + result.nume + '</li>' +
'<li>Compozitie: ' + result.compozitie + '</li>'
);
});
});
};
window.setInterval(updateList, 200);
});
$(文档).ready(函数(){
var updateList=函数(){
$.getJSON('cockbox.php',函数(数据){
变量列表=$('ul');
list.empty();
$.each(data.results,function)(索引,结果){
list.append(
“Poza:”+result.Poza+“ ”+
“Nume:”+result.Nume+“ ”+
“复合材料:”+result.Compozitie+“ ”
);
});
});
};
setInterval(updateList,200);
});
sooocockbox.php
是否显示正确的数据?但是script1.js
无法使用$.getJSON
获取它。。。对吗?你有什么错误吗?updates
函数是否实际被调用?数据是空白还是…?在$.getJSON(“cockbox.php”,function(data){
行之后,$data的值是多少?$每个
都是一个语法,错误也是…应该是…。页面cockbox.php它显示来自mysql的数据,但以原始方式:{“result”:[{“poza”:“\/images\/cockle\/mojito.jpg\/>,“nume”:“Mojito”,“compozitie”:“15cl rom,gheata,menta”}]}脚本应该将它排列在一个更干净的列表中,这就是我想要成功的。
$(document).ready(function () {
var updateList = function () {
$.getJSON('cocktail.php', function( data ) {
var list = $('ul');
list.empty();
$.each(data.results, function( index, result ) {
list.append(
'<li>Poza: ' + result.poza + '</li>' +
'<li>Nume: ' + result.nume + '</li>' +
'<li>Compozitie: ' + result.compozitie + '</li>'
);
});
});
};
window.setInterval(updateList, 200);
});