Foros del Web » Programando para Internet » Jquery »

[SOLUCIONADO] Recoger Datos con ajax y php

Estas en el tema de Recoger Datos con ajax y php en el foro de Jquery en Foros del Web. Lo que pasa es que necesito modificar un regsitro y estoy intentando con este codigo antes de haerlo en mi proyecto pero no se como ...
  #1 (permalink)  
Antiguo 30/06/2016, 15:14
 
Fecha de Ingreso: diciembre-2015
Mensajes: 2
Antigüedad: 8 años, 3 meses
Puntos: 0
Recoger Datos con ajax y php

Lo que pasa es que necesito modificar un regsitro y estoy intentando con este codigo antes de haerlo en mi proyecto pero no se como mostrarlo


este es el script

function editarRegistro(id){

var url = 'datos.php';
$.ajax({
type:'POST',
url:url,
data:'id='+id,
success: function(valores){
var datos = eval(valores);
$('#nombre').val(datos[0]);
$('#apellido').val(datos[1]);
$('#pass').val(datos[2]);
$("#formulario").html(); creo q este es el error no se como hacer q me muestre estos datos

}
});
return false;
}



este es el formulario




<div id="joder">
<form id="formulario" method="post" >
Nombre: <input type="text" name="nombre" id="name" /><br/><br/>
Apellidos: <input type="text" name="apellido" >
Pass: <input type="text" name="pass" >



<a href="javascript:agregaRegistro();"
class="btn btn-danger btn-rounded btn-sm"><span class="fa fa-pencil"></span>Registrar</a>

<div id="resultado"></div>
</form>



</div>





el codigo php para hacer el proceso datos.php




$id = $_POST['id'];

//OBTENEMOS LOS VALORES DEL PRODUCTO
$query= "select * from joder2 where id='$id'";
$resultado =$conexion->query($query);
$valores2=$resultado->fetch_assoc();

$datos = array(
0 => $valores2['name'],
1 => $valores2['email'],
2 => $valores2['pass'],
);
echo json_encode($datos);

muchas gracias por su ayuda
  #2 (permalink)  
Antiguo 30/06/2016, 18:48
alvaro_trewhela
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: Recoger Datos con ajax y php

Yo uso este "template:" pero en js

Código HTML:
Ver original
  1.     <head>
  2.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  3.         <title>AJAX</title>
  4.        
  5.         <script type="text/javascript">
  6.        
  7.        
  8.         function createREQ() {
  9.                 try {
  10.                 req = new XMLHttpRequest(); /* p.e. Firefox */
  11.                 }
  12.                 catch(err1){
  13.                     try {
  14.                     req = new ActiveXObject('Msxml2.XMLHTTP'); /* algunas versiones IE */
  15.                     }
  16.                     catch (err2) {
  17.                         try {
  18.                         req = new ActiveXObject("Microsoft.XMLHTTP"); /* algunas versiones IE */
  19.                         }
  20.                         catch (err3) {
  21.                         req = false;
  22.                         }
  23.                     }
  24.                 }
  25.         return req;
  26.         }
  27.        
  28.        
  29.         function someXHTTPfunction(){
  30.         var http = new createREQ();
  31.         http.open("POST", "somePHPscript.php", true);
  32.         http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  33.         var var1 = document.getElementById("someElement1").value;
  34.         var var2 = document.getElementById("someElement2").value;
  35.         http.send("var1="+var1+"&var2="+var2); 
  36.        
  37.         http.onreadystatechange = function(){
  38.                 if(http.readyState == 4 && http.status == 200){
  39.                 httpXML = http.responseXML;
  40.                 }
  41.             };
  42.         //ALL THE XML VALUE IN: httpXML
  43.         }
  44.         </script>
  45.    
  46.     </head>
  47.     <body>
  48.         <input type="text" id="someElement1" /><br/>
  49.         <input type="text" id="someElement2" /><br/>
  50.         <button onclick="someXHTTPfunction();">MAKE SOME XHTPP</button>
  51.     </body>
  52. </html>

y en php

Código PHP:
Ver original
  1. <?php
  2.  
  3. $posts = array($_POST["var1"], $_POST["var2"]); //here you can use the posts as array
  4.  
  5.  
  6. //here the PHP code...
  7.  
  8.  
  9. //here below create the xml response....
  10. header("Content-type: text/xml");
  11.  
  12. echo '<?xml version="1.0" encoding="UTF-8" ?>
  13. <posts>
  14.     <ele1>
  15.     '.$posts[0].'
  16.     </ele1>
  17.     <ele2>
  18.     '.$posts[1].'
  19.     </ele2>
  20. </posts>';
  21. ?>

eso uso yo y me anda de 10, saludos


Última edición por alvaro_trewhela; 30/06/2016 a las 19:15
  #3 (permalink)  
Antiguo 06/07/2016, 11:42
 
Fecha de Ingreso: agosto-2015
Ubicación: La Habana
Mensajes: 72
Antigüedad: 8 años, 7 meses
Puntos: 4
Respuesta: Recoger Datos con ajax y php

el parametro data de la funcion $.ajax deberia ser:

Cita:
data: { nombrevariable: "valorvariable", nombrevariable2: "valorvariable2"},
en tu caso:

Cita:
data: { id:$('#id').val()},

Etiquetas: ajax, formulario, html, input, javascript, js, php, recoger, valor
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 13:27.