[英]grabing echoed data from php in ajax
$(document).ready(function() {
$(document).ready(function () {
var request = $.ajax({
url: 'inc/everything.php',
type: "POST",
dataType: "html",
success: function (data) {
if ("row:has('tacobell');") {
$('#oriental').html(name);
}
}
});
});
.encasing {
width: 193px;
height: 400px;
float: left;
}
.result-ingredient {
z-index: 13;
width: 150px;
margin: 10px;
float: left;
}
<div class=" encasing">
<img class="result-ingredient" src="img/chicken.png">
<div id ="chicken"> </div>
</div>
<div class=" encasing">
<img class="result-ingredient" src="img/beef.png">
<div id ="beef"> </div>
</div>
<div class=" encasing">
<img class="result-ingredient" src="img/pork.png">
<div id ="pork"> </div>
</div>
<div class=" encasing">
<img class="result-ingredient" src="img/oriental.png">
<div id ="oriental"> </div>
</div>
<?php
include 'database.php';
$sql = "SELECT * FROM `answers` WHERE 1 ORDER BY `choices` ASC ";
$result = mysql_query($sql, $conn);
if (!$result) {
var_dump($result);
$message .= 'DB Error occured';
die($message);
}
while ($row = mysql_fetch_assoc($result)) {
echo "Name:" . ($row['name']) . " " . "choices:" . $row['choices'];
echo "<br>";
}
?>
There has to be a better way to display all of those queries in html. 必须有一种更好的方法来以html显示所有这些查询。 I want the persons name to appear under the item they chose earlier, which corresponds to a value in the database.
我希望人员名称出现在他们之前选择的项目下,该名称与数据库中的值相对应。 Its not exactly working and i was wondering if i'm just going about this the wrong way?
它不能完全正常工作,我想知道我是否正以错误的方式进行操作?
edit: 编辑:
i don't know how to get ajax to pick up the right data i want. 我不知道如何获取Ajax来获取我想要的正确数据。 My idea was to check if any of the rows has a certain value then just post the names from that but its not working.
我的想法是检查是否有任何行具有特定值,然后仅从中发布名称,但不起作用。
this is what the database prints out now 这就是数据库现在打印出来的内容
Name: trisha choices: chicken,tacobell 名称:翠莎选择:鸡,tacobell
Name: sarah choices: chicken,mcrab,sriracha,tacobell 名称:萨拉选择:鸡肉,麦克拉布,斯拉拉查,塔可贝
Name: rachel choices: chicken,peas,corn,lettuce,mcrab,sriracha,tacobell 名称:瑞秋选择:鸡肉,豌豆,玉米,生菜,mcrab,sriracha,tacobell
Yes. 是。 Use
REGEXP
to make 1 query. 使用
REGEXP
进行1个查询。
SELECT `name`,`choices` AS choice_number,
CASE
WHEN choices = 1 THEN 'oriental'
WHEN choices = 2 THEN 'pork'
WHEN choices = 3 THEN 'beef'
WHEN choices = 4 THEN 'chicken'
WHEN choices = 5 THEN 'shrimp'
END as choice_string
FROM `answers`
WHERE `choices` REGEXP '[1|2|3|4|5]';
Then, in your loop, you can use $row['name']
, $row['choice_number']
, and $row['choice_string']
. 然后,在循环中,可以使用
$row['name']
, $row['choice_number']
和$row['choice_string']
。
I see you're using jQuery $.ajax, which has the response of the PHP file in data
(parameter of the success
function), which is fine. 我看到您使用的是jQuery $ .ajax,它在
data
中具有PHP文件的响应( success
函数的参数),这很好。 You're then writing the data
by using the html
method, which I don't see a reason to change. 然后,您将使用
html
方法写入data
,但我认为没有理由进行更改。
I would use JSON on the server, which makes it nicer to iterate over in the Javascript. 我将在服务器上使用JSON,这使得在Javascript中进行迭代变得更好。
SELECT name FROM answers WHERE choices = 1 OR choices = 2 OR choice = 3....
Then, 然后,
$resp = array();
while ($row = mysql_fetch_assoc($result)) {
$resp[] = $row;
}
echo json_encode($resp);
On the client side: 在客户端:
$(document).ready(function() {
var request = $.ajax({
url: 'php/everything.php',
type: "POST",
data: datastring,
dataType: "json",
success: function(data) {
$.each(data, function (index, value) {
$('#oriental').append(value);
});
}
});
request.done(function(msg) {
$("#log").html(msg);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.