Foros del Web » Programando para Internet » Javascript » Frameworks JS »

Consulta ajax

Estas en el tema de Consulta ajax en el foro de Frameworks JS en Foros del Web. Hola amigos tengo la siguiente consulta estoy trabajando con el Dialog modal form de jquery http://jqueryui.com/demos/dialog/#modal-form el ejemplo funcina sin conexion a base de datos ...
  #1 (permalink)  
Antiguo 30/05/2012, 22:42
 
Fecha de Ingreso: septiembre-2010
Mensajes: 1.853
Antigüedad: 13 años, 7 meses
Puntos: 6
Consulta ajax

Hola amigos tengo la siguiente consulta


estoy trabajando con el Dialog modal form de jquery http://jqueryui.com/demos/dialog/#modal-form el ejemplo funcina sin conexion a base de datos , como hago para que al abrirse el dialog modal form e insertar los datos en base de datos se actualiza la tabla

el inconveniente es para actualizar la tabla cada vez que se ingresen los datos

gracias
  #2 (permalink)  
Antiguo 31/05/2012, 07:37
Avatar de IsaBelM
Colaborador
 
Fecha de Ingreso: junio-2008
Mensajes: 5.032
Antigüedad: 15 años, 10 meses
Puntos: 1012
Respuesta: Consulta ajax

quizás, ..... ajax??
__________________
if(ViolenciaDeGénero) {alert('MUJER ASESINADA');}
  #3 (permalink)  
Antiguo 31/05/2012, 07:51
 
Fecha de Ingreso: enero-2010
Mensajes: 97
Antigüedad: 14 años, 2 meses
Puntos: 3
Respuesta: Consulta ajax

mira tienes que tener 2 archivos, el archivo javascript que sera el encargado de hacer la llamada ajax y el archivo php que sera el que realice la sentencia a la base de datos.

En javascript tienes que hacer la llamada con algo como esto(de manera sincrona):

function createAjax()
{
var xmlhttp=false;
try
{
// Creacion del objeto AJAX para navegadores no IE
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
// Creacion del objet AJAX para IE
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(E) { xmlhttp=false; }
}
if (!xmlhttp && typeof XMLHttpRequest!="undefined") { xmlhttp=new XMLHttpRequest(); }

return xmlhttp;
}


//funcion que ejecuta el ajax llamando al archivo php y devolviendo un objeto JSON que manda el php
function executeAjax(vars)
{
var ajax = createAjax();
ajax.open("POST", "ajax.php", false);
ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
ajax.send(vars);
return ajax.responseText;
}



----------------------------


llamarias a executeAjax y te devolveria lo que el php devuelva.

Última edición por reonevk; 31/05/2012 a las 08:07
  #4 (permalink)  
Antiguo 31/05/2012, 23:41
 
Fecha de Ingreso: septiembre-2010
Mensajes: 1.853
Antigüedad: 13 años, 7 meses
Puntos: 6
Respuesta: Consulta ajax

amigos gracias por responder estoy intentando implementar el codigo me resultan varios inconvenientes:

el siguiente codigo sirve para abrir el dialog modal form
Código Javascript:
Ver original
  1. <script>
  2.     $(function() {
  3.         // a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
  4.         $( "#dialog:ui-dialog" ).dialog( "destroy" );
  5.        
  6.         var name = $( "#name" ),
  7.             email = $( "#email" ),
  8.             password = $( "#password" ),
  9.             allFields = $( [] ).add( name ).add( email ).add( password ),
  10.             tips = $( ".validateTips" );
  11.  
  12.         function updateTips( t ) {
  13.             tips
  14.                 .text( t )
  15.                 .addClass( "ui-state-highlight" );
  16.             setTimeout(function() {
  17.                 tips.removeClass( "ui-state-highlight", 1500 );
  18.             }, 500 );
  19.         }
  20.  
  21.         function checkLength( o, n, min, max ) {
  22.             if ( o.val().length > max || o.val().length < min ) {
  23.                 o.addClass( "ui-state-error" );
  24.                 updateTips( "Length of " + n + " must be between " +
  25.                     min + " and " + max + "." );
  26.                 return false;
  27.             } else {
  28.                 return true;
  29.             }
  30.         }
  31.  
  32.         function checkRegexp( o, regexp, n ) {
  33.             if ( !( regexp.test( o.val() ) ) ) {
  34.                 o.addClass( "ui-state-error" );
  35.                 updateTips( n );
  36.                 return false;
  37.             } else {
  38.                 return true;
  39.             }
  40.         }
  41.        
  42.         $( "#dialog-form" ).dialog({
  43.             autoOpen: false,
  44.             height: 300,
  45.             width: 350,
  46.             modal: true,
  47.             buttons: {
  48.                 "Create an account": function() {
  49.                     var bValid = true;
  50.                     allFields.removeClass( "ui-state-error" );
  51.  
  52.                     bValid = bValid && checkLength( name, "username", 3, 16 );
  53.                     bValid = bValid && checkLength( email, "email", 6, 80 );
  54.                     bValid = bValid && checkLength( password, "password", 5, 16 );
  55.  
  56.                     bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
  57.                     // From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
  58.                     bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. [email protected]" );
  59.                     bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
  60.  
  61.                  
  62.                     if ( bValid ) {
  63.                         $( "#users tbody" ).append( "<tr>" +
  64.                             "<td>" + name.val() + "</td>" +
  65.                             "<td>" + email.val() + "</td>" +
  66.                             "<td>" + password.val() + "</td>" +
  67.                         "</tr>" );
  68.            
  69.                         $( this ).dialog( "close" );
  70.                    
  71.                     }
  72.                 },
  73.                 Cancel: function() {
  74.                     $( this ).dialog( "close" );
  75.                 }
  76.             },
  77.             close: function() {
  78.                 allFields.val( "" ).removeClass( "ui-state-error" );
  79.             }
  80.         });
  81.  
  82.         $( "#create-user" )
  83.             .button()
  84.             .click(function() {
  85.                 $( "#dialog-form" ).dialog( "open" );
  86.             });
  87.     });
  88.     </script>



estoy tratando de guardar en base de datos por tal motivo utilizo ajax pero no me funciona,este codigo ajax lo coloco en entre

if ( bValid ) {


$( this ).dialog( "close" );

}


Código Javascript:
Ver original
  1. $.ajax({
  2.               url:'archivo.php',
  3.                   type:'post',
  4.                   data:'name='+name+'&email='+email+'&password='+password,
  5.                   }).done(function(data) {
  6.                
  7.                alert(name+" "+email+" "+password);
  8.                
  9.    
  10.                   })

que estoy haciendo mal?
  #5 (permalink)  
Antiguo 02/06/2012, 23:14
 
Fecha de Ingreso: septiembre-2010
Mensajes: 1.853
Antigüedad: 13 años, 7 meses
Puntos: 6
Respuesta: Consulta ajax

hola amigos ya logro insertar en la base de dato por media de la ventana modal,

en la pagina.php tengo una tabla html donde listo los registros de la base de datos necito que al momento de insertar los datos mediante la ventana modal aparezca el nuevo registro en la tabla sin necesidad de recargar la pagina.

tengo unas preguntas para lo que me recomendo reonevk

"mira tienes que tener 2 archivos, el archivo javascript que sera el encargado de hacer la llamada ajax y el archivo php que sera el que realice la sentencia a la base de datos.

En javascript tienes que hacer la llamada con algo como esto(de manera sincrona):

function createAjax()
{
var xmlhttp=false;
try
{
// Creacion del objeto AJAX para navegadores no IE
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
// Creacion del objet AJAX para IE
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(E) { xmlhttp=false; }
}
if (!xmlhttp && typeof XMLHttpRequest!="undefined") { xmlhttp=new XMLHttpRequest(); }

return xmlhttp;
}


//funcion que ejecuta el ajax llamando al archivo php y devolviendo un objeto JSON que manda el php
function executeAjax(vars)
{
var ajax = createAjax();
ajax.open("POST", "ajax.php", false);
ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
ajax.send(vars);
return ajax.responseText;
}
"

en donde de mi codigo llama la function createAjax() ya que utilizo la venta modal
  #6 (permalink)  
Antiguo 03/06/2012, 23:02
 
Fecha de Ingreso: septiembre-2010
Mensajes: 1.853
Antigüedad: 13 años, 7 meses
Puntos: 6
Respuesta: Consulta ajax

hola amigos no he logrado implentar el codigo.


este es el codigo que estoy utilizando hasta el momento logro abrir la venta modal , validar los campos e insertar en la base de datos.

pero no logro que la tabla se actualize al insertar en la base toca darle f5 o refrescar la pagina para ver los cambios.

Código Javascript:
Ver original
  1. <script>
  2.     $(function() {
  3.         $( "#tabs" ).tabs();
  4.     });
  5.     </script>
  6.     <script>
  7.     $(function() {
  8.         // a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
  9.         $( "#dialog:ui-dialog" ).dialog( "destroy" );
  10.        
  11.         var name = $( "#name" ),
  12.             email = $( "#email" ),
  13.             password = $( "#password" ),
  14.             allFields = $( [] ).add( name ).add( email ).add( password ),
  15.             tips = $( ".validateTips" );
  16.  
  17.         function updateTips( t ) {
  18.             tips
  19.                 .text( t )
  20.                 .addClass( "ui-state-highlight" );
  21.             setTimeout(function() {
  22.                 tips.removeClass( "ui-state-highlight", 1500 );
  23.             }, 500 );
  24.         }
  25.  
  26.         function checkLength( o, n, min, max ) {
  27.             if ( o.val().length > max || o.val().length < min ) {
  28.                 o.addClass( "ui-state-error" );
  29.                 updateTips( "Length of " + n + " must be between " +
  30.                     min + " and " + max + "." );
  31.                 return false;
  32.             } else {
  33.                 return true;
  34.             }
  35.         }
  36.  
  37.         function checkRegexp( o, regexp, n ) {
  38.             if ( !( regexp.test( o.val() ) ) ) {
  39.                 o.addClass( "ui-state-error" );
  40.                 updateTips( n );
  41.                 return false;
  42.             } else {
  43.                 return true;
  44.             }
  45.         }
  46.        
  47.         $( "#dialog-form" ).dialog({
  48.             autoOpen: false,
  49.             height: 300,
  50.             width: 350,
  51.             modal: true,
  52.             buttons: {
  53.                 "Create an account": function() {
  54.                     var bValid = true;
  55.                     allFields.removeClass( "ui-state-error" );
  56.  
  57.                     bValid = bValid && checkLength( name, "username", 3, 16 );
  58.                     bValid = bValid && checkLength( email, "email", 6, 80 );
  59.                    
  60.  
  61.                     //bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
  62.                     // From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
  63.                     bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. [email protected]" );
  64.                     bValid = bValid && checkLength( password, "password", 5, 16 );
  65.                     bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
  66.                    
  67.                 var namec = $("#name").val();
  68.                 var emailc = $("#email").val();
  69.                 var passwordc = $("#password").val();
  70.                  
  71.                     if ( bValid ) {
  72.                         /*$( "#users tbody" ).append( "<tr>" +
  73.                             "<td>" + name.val() + "</td>" +
  74.                             "<td>" + email.val() + "</td>" +
  75.                             "<td>" + password.val() + "</td>" +
  76.                         "</tr>" );*/
  77.                 $.ajax({
  78.                   url:'archivo.php',
  79.                   type:'post',
  80.                   data:'name='+namec+'&email='+emailc+'&password='+passwordc,
  81.                   }).done(function(data) {
  82.                  //alert(titleq+" "+arrayOpt+" "+startq+" "+endq);
  83.                  
  84.                  alert(namec+" "+emailc+" "+passwordc);
  85.                  
  86.                
  87.    
  88.                   });
  89.            
  90.                         $( this ).dialog( "close" );
  91.                    
  92.                     }
  93.                 },
  94.                 Cancel: function() {
  95.                     $( this ).dialog( "close" );
  96.                 }
  97.             },
  98.             close: function() {
  99.                 allFields.val( "" ).removeClass( "ui-state-error" );
  100.             }
  101.         });
  102.  
  103.         $( "#create-user" )
  104.             .button()
  105.             .click(function() {
  106.                 $( "#dialog-form" ).dialog( "open" );
  107.             });
  108.     });
  109.     </script>

Código HTML:
Ver original
  1.  
  2. <div class="demo">
  3.  
  4. <div id="tabs">
  5.     <ul>
  6.         <li><a href="#tabs-1">Boletines</a></li>
  7.         <li><a href="#tabs-2">Periodistas</a></li>
  8.     </ul>
  9.     <div id="tabs-1">
  10.       <div id="users-contain" class="ui-widget">
  11.     <h1>Boletines</h1>
  12.     <button id="create-user">Create new user</button>
  13.     <br>
  14.     <br>
  15. <table width="268%" class="ui-widget ui-widget-content" id="users">
  16.         <thead>
  17.             <tr class="ui-widget-header ">
  18.                 <th width="16%">Destinatario</th>
  19.                 <th width="16%">Email</th>
  20.                 <th width="10%">Telefono</th>
  21.                 <th width="13%">Telefono</th>
  22.                 <th width="13%">Telefono</th>
  23.                 <th width="9%">Fax</th>
  24.                 <th width="9%">Web</th>
  25.                 <th width="14%">Direcci&oacute;n</th>
  26.             </tr>
  27.         </thead>
  28.          <?php
  29.          
  30.         $cont = 1;
  31.        
  32.         foreach($fl as $field)
  33.        
  34.         {
  35.        
  36.     ?>
  37.         <tbody>
  38.             <tr>
  39.                 <td><?php echo $cont?></td>
  40.                 <td><?php echo $field['name']?></td>
  41.                 <td><?php echo $field['email']?></td>
  42.                 <td></td>
  43.                 <td></td>
  44.                 <td></td>
  45.                 <td></td>
  46.                 <td></td>
  47.             </tr>
  48.         </tbody>
  49.             <?php
  50.    
  51.         $cont++;
  52.        
  53.         }
  54.  
  55.     ?>
  56.     </table>
  57.     <p><div id="dialog-form" title="Create new user">
  58.     <p class="validateTips">All form fields are required.</p>
  59.  
  60.     <form>
  61.     <fieldset>
  62.         <label for="name">Name</label>
  63.         <input type="text" name="name" id="name" class="text ui-widget-content ui-corner-all" />
  64.         <label for="email">Email</label>
  65.         <input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all" />
  66.         <label for="password">Password</label>
  67.         <input type="password" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />
  68.     </fieldset>
  69.     </form>
  70. </div></p>
  71.       </div>
  72. </div>
  73.     <div id="tabs-2">
  74.         <p>Periodistas</p>
  75.     </div>
  76.   </div>
  77.  
  78. </div><!-- End demo -->
  79. <!-- End demo-description -->
  80. </body>

Etiquetas: ajax
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 19:16.