![Antiguo](http://static.forosdelweb.com/fdwtheme/images/statusicon/post_old.gif)
24/02/2015, 09:29
|
| | Fecha de Ingreso: febrero-2015 Ubicación: colobmia
Mensajes: 5
Antigüedad: 10 años Puntos: 0 | |
Respuesta: Agregar consulta en ajax a una base de datos php ej1.php
<html>
<head>
<script language="javascript" type="text/javascript">
function nuevoAjax()
{
var xmlhttp=false;
try
{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(E) { xmlhttp=false; }
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp=new XMLHttpRequest(); }
return xmlhttp;
}
function traerDatos()
{
var cod=document.getElementById("cod").value;
var campo1=document.getElementById("c1");
var campo2=document.getElementById("c2");
var ajax=nuevoAjax();
ajax.open("POST", "ej2.php", true);
ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajax.send("v="+cod);
ajax.onreadystatechange=function()
{
if (ajax.readyState==4)
{
var respuesta=ajax.responseXML;
campo1.value=respuesta.getElementsByTagName("nombr e")[0].childNodes[0].data;
campo2.value=respuesta.getElementsByTagName("apell ido")[0].childNodes[0].data;
}
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Ejemplo</title>
</head>
<body>
<input type="text" id="cod"> Codigo <input type="button" id="b1" value="Buscar" onClick="traerDatos();">
<input type="submit" name="button" id="button" value="Enturnar">
<br><br>
<input type="text" id="c1"> Nombre<br><br>
<input type="text" id="c2"> Apellido
</body>
</html>
ej2.php
<?php
$v=$_POST["v"];
$conexion=mysql_connect("localhost", "root", "");
mysql_select_db("ajax", $conexion);
$resultado=mysql_query("SELECT nombre, apellido FROM ejemplo WHERE id='$v'");
$registro=mysql_fetch_row($resultado);
$xml="<?xml version='1.0' encoding='ISO-8859-1'?>";
$xml.="<datos>";
$xml.="<nombre><![CDATA[$registro[0]]]></nombre>";
$xml.="<apellido><![CDATA[$registro[1]]]></apellido>";
$xml.="</datos>";
header("Content-type: text/xml");
echo $xml;
?> |