如何使用AJAX从数据库获取数据并在页面上显示 [英] How to use AJAX to get data from DB and display on page
问题描述
我在一个项目中工作,并且被困在这里,我不知道为什么无法从数据库中获取列表\
I'm working in a project and I'm stuck here, I don't know why I can't get the list from my database\
这是我的 JAVASCRIPT
$(document).ready(function(){
$.ajax({
url:'datos.php?accion=ac',
success:function(datos){
for(x = 0;x<datos.length;x++){
//$("#PAIS").append("<option value='"+datos[x].id_pais+"'>"+datos[x].pais+"</option>");
$("#PAIS").append(new Option( datos[x].pais, datos[x].id_pais));
}
}
})
$("#PAIS").change(function(){
//var felix=$('#PAIS option:selected').val();
//alert(felix);
$.ajax({
url:'datos.php?accion=ad',
alert('hola22');
success:function(datos1){
console.log("hola");
for(x = 0;x<=datos1.length;x++){
//$("#PAIS").append("<option value='"+datos[x].id_pais+"'>"+datos[x].pais+"</option>");
$("#REGION").append(new Option( datos1[x].region, datos1[x].id_region));
}
}
})
});
})
还有我的functions.php:
And my functions.php:
<?php
$server="localhost";
$usr="root";
$passwd="";
$data="combo";
$db=mysqli_connect($server,$usr,$passwd,$data) or die ("Error en la conexion1");
$Accion = $_GET['accion'];
if($Accion=="ac"){
header('Content-Type: application/json');
$paises = array();
$Consulta = mysqli_query($db,"SELECT * FROM paises")or die ("Error en la conexion7");
while($Fila=mysqli_fetch_assoc($Consulta)){
$paises[] = $Fila;
}
echo json_encode($paises);
}
if($Accion=="ad"){
header('Content-Type: application/json');
$regiones = array();
$Consulta1 = mysqli_query($db,"SELECT * FROM regiones WHERE id_pais=4");//.$_REQUEST['id_pais']);
while($Fila=mysqli_fetch_assoc($Consulta1)){
$regiones[] = $Fila;
//echo json_encode($Fila);
}
echo json_encode($regiones);
}
?>
好吧,我的问题是我真的不知道第一个真正的工作原理是什么:D,但是当我调用 url:datos.php = ad 时,此代码块不起作用:/
Well, my problem it's that I really don't know how the first really works :D, but when I'm calling url:datos.php=ad this block doesn't work :/
推荐答案
首先,您会发现阅读以下有关AJAX的简单示例很有帮助.不要只是阅读它们,将它们复制到您的服务器并使它们工作.更改一些名称或值-看看它们如何工作.
First, you will find it helpful to review these simple examples about AJAX. Do not just read them, copy them to your server and make them work. Change a few names or values -- see how they work.
接下来,这是一篇帖子,概述了PHP/网页/AJAX如何协同工作.花几分钟并仔细阅读.看看您是否可以遵循逻辑.我敢打赌,灯泡会为您服务.
Next, here is a post that gives an overview to how PHP / web page / AJAX all work together. Take a few minutes and read it carefully. See if you can follow the logic. I bet the lightbulb will come on for you.
使您的代码尽可能地标准.不要采取任何捷径.请使用完整的 $.ajax()
结构,而不要使用 $.post()
或 $.get()
的快捷方式(两者都 $.ajax()
的快捷方式形式.不要跳过任何东西.随着情况的改善,您可以开始采用一些快捷方式.但是现在,请确保您的AJAX代码块如下所示:
Make your code as standard as possible. Don't take any shortcuts. Use the full $.ajax()
structure, not the shortcuts of $.post()
or $.get()
(these are both shortcut forms of $.ajax()
. Don't skip anything. As you get better, you can start to take some shortcuts. But for now, make sure your AJAX code block looks like this:
var var_value = $('#someElement').val();
$.ajax({
type: 'post',
url: 'your_ajax_processor.php',
data: 'post_var_name=' +var_value,
success: function(dataz){
if (dataz.length) alert(dataz);
$('body').append(dataz);
}
});
在您的PHP中,您将收到在 $ _ POST
数组变量中发布的值.如果在AJAX中将变量 post_var_name
命名为变量(如上例所示),那么访问内容的方式就是
In your PHP, you will receive the value you posted in the $_POST
array variable. If, in AJAX, you named your variable post_var_name
(as we did in the example above), then that is how you access the contents:
$myVar = $_POST['post_var_name'];
遇到麻烦时,最好进行一些测试.(1)在PHP方面,注释掉所有内容,并在顶部添加回显命令,例如:
When you are having trouble, a great idea is to put in some tests. (1) On the PHP side, comment out everything and at the top put in an echo command, like:
<?php
echo 'I got here';
die();
返回网页,在AJAX成功功能中,仅警告您获得什么:
Back on the web page, in the AJAX success function, just alert what you get:
success: function(d){
alert(d);
}
到那时,您知道两件事:
At that point, you know two things:
-
您的AJAX到PHP的通信正在运行,并且
Your AJAX -to- PHP communications are working, and
您将看到什么价值传递给了PHP.
You see what value got passed over to PHP.
然后,您可能会做类似的事情
Then, you might do something like this
js/jQuery:
var var_value = $('#someElement').val();
$.ajax({
type: 'post',
url: 'your_ajax_processor.php',
data: 'post_var_name=' +var_value,
success: function(dataz){
if (dataz.length) alert(dataz);
$('body').append(dataz);
}
});
PHP:
<?php
$myVar = $_POST['post_var_name'];
//Now you can do something with variable `$myVar`, such as:
$out = '
<div class="red-background"> '.$myVar.' </div>
';
//This content is received in the AJAX code block using the variable name you specified: "dataz"
echo $out;
这篇关于如何使用AJAX从数据库获取数据并在页面上显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!