Ver Mensaje Individual
  #4 (permalink)  
Antiguo 25/01/2010, 09:19
ingluis
 
Fecha de Ingreso: enero-2009
Mensajes: 197
Antigüedad: 15 años, 3 meses
Puntos: 0
Respuesta: Traer a un combo datos

tengo lo siguiente:

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="Traer datos" onClick="traerDatos();"><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;
?>