Ver Mensaje Individual
  #1 (permalink)  
Antiguo 18/04/2013, 06:37
edie8
 
Fecha de Ingreso: noviembre-2011
Mensajes: 516
Antigüedad: 13 años, 2 meses
Puntos: 10
Comentarios comet

Buenas
Tengo un problema con un formulario, el caso es que quería hacer un comet que me encontre por internet pero al ponerle un campo de formulario, nose si se puede hacer de otra forma pero se me actualiza y no me deja escribir es decir pongo para escribir y cuando yevo un rato se actualiza y borra el contenido del textarea nose como se hace de otra forma ni si lo ago bien ya os digo que fue un codigo que encontre ace tiempo este es el codigo.
index.php
Código PHP:
Ver original
  1. <?php  
  2. if($_POST)    
  3. {
  4.     set_time_limit(0);      
  5.     header("Edge-control: no-store");  
  6.     sleep(1);
  7.     include('config.php');
  8.     $notificaciones=mysql_query("select * from messages",$conexion);
  9.     while($rs=mysql_fetch_assoc($notificaciones))
  10.     {
  11.     $imgs=mysql_query("select * from usuarios where id='$rs[user_id]'",$conexion);
  12.       while($im=mysql_fetch_array($imgs))
  13.     {
  14.         echo '<div style="width:50%; background:#CCCCCC; border-bottom:solid; border-bottom-color:#FFFFFF;">';
  15.         echo '<img src="'.$im['imagen'].'" height="8%"> ';
  16.         echo   $rs['estado']."<br>";
  17.         echo '<form name="nuevo_empleado1" action="" onSubmit="enviarDatosEmpleado1(); return false" style="margin-left:2%; margin-top:2%;">
  18. <textarea name="nombres1" type="text" cols="50" style="resize: none;"></textarea>
  19. <input type="submit" name="Submit" value="Compartir" />
  20. </form>';
  21.         echo '</div>';
  22.     }
  23.     $coment_de_comen=mysql_query("Select * from coment where id_comentario='$rs[msg_id]'",$conexion);
  24.     while($come=mysql_fetch_array($coment_de_comen))
  25.     {
  26.         echo "abra ssmss";
  27.     }
  28.     }  
  29.     mysql_close($conexion);    
  30.     exit();
  31.    
  32. }
  33. ?>
  34. <html>
  35. <head>    
  36. </head>
  37. <body style="">
  38. <div style="border:solid 1px; border-top-color:#666666; width:45%; background-color:#CCCCCC; height:10%;"><form name="nuevo_empleado" action="" onSubmit="enviarDatosEmpleado(); return false" style="margin-left:2%; margin-top:2%;">
  39. <textarea name="nombres" type="text" cols="50" style="resize: none;"></textarea>
  40. <input type="submit" name="Submit" value="Compartir" />
  41. </form></div>
  42.  
  43.  <div id="content">Cargando...</div>
  44. <script type="text/javascript" src="comet.js"></script>
  45. </body>
  46. </html>
comet.js
Código Javascript:
Ver original
  1. function objetoAjax(){
  2.  
  3. var xmlhttp=false;
  4.  
  5. try {
  6.  
  7.  
  8. xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  9.  
  10. } catch (e) {
  11.  
  12. try {
  13.  
  14. xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  15.  
  16. } catch (E) {
  17.  
  18. xmlhttp = false;
  19.  
  20. }
  21.  
  22.  
  23. }
  24. if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  25.  
  26.   xmlhttp = new XMLHttpRequest();
  27.  
  28.   }
  29.  
  30.   return xmlhttp;
  31.  
  32.   }
  33. function enviarDatosEmpleado(){
  34.  
  35.   //donde se mostrará lo resultados
  36.  
  37.  
  38.  
  39.  
  40.   //valores de los inputs
  41.  
  42.   nom=document.nuevo_empleado.nombres.value;
  43.  
  44.  
  45.   //instanciamos el objetoAjax
  46.  
  47.   ajax=objetoAjax();
  48.  
  49.   //uso del medotod POST
  50.  
  51.   //archivo que realizará la operacion
  52.  
  53.   //registro.php
  54.  
  55.  
  56.   ajax.open("POST", "registro.php",true);
  57.  
  58.   ajax.onreadystatechange=function() {
  59.  
  60.   if (ajax.readyState==4) {
  61.  
  62.   //mostrar resultados en esta cap
  63.  
  64.  
  65.   //llamar a funcion para limpiar los inputs
  66.  
  67.   LimpiarCampos();
  68.  
  69.   }
  70.  
  71.   }
  72.  
  73.   ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  74.  
  75.  
  76.   //enviando los valores
  77.  
  78.   ajax.send("nombres="+nom)
  79.  
  80.   }
  81.  
  82. function LimpiarCampos(){
  83.  
  84.  
  85.   document.nuevo_empleado.nombres.value="";
  86.   document.nuevo_empleado.nombres.focus();
  87.  
  88.   }
  89.   function enviarDatosEmpleado1(){
  90.  
  91.   //donde se mostrará lo resultados
  92.  
  93.  
  94.  
  95.  
  96.   //valores de los inputs
  97.  
  98.   nom1=document.nuevo_empleado1.nombres1.value;
  99.  
  100.  
  101.   //instanciamos el objetoAjax
  102.  
  103.   ajax1=objetoAjax();
  104.  
  105.   //uso del medotod POST
  106.  
  107.   //archivo que realizará la operacion
  108.  
  109.   //registro.php
  110.  
  111.  
  112.   ajax1.open("POST", "registro.php",true);
  113.  
  114.   ajax1.onreadystatechange=function() {
  115.  
  116.   if (ajax1.readyState==4) {
  117.  
  118.   //mostrar resultados en esta cap
  119.  
  120.  
  121.   //llamar a funcion para limpiar los inputs
  122.  
  123.   LimpiarCampos1();
  124.  
  125.   }
  126.  
  127.   }
  128.  
  129.   ajax1.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  130.  
  131.  
  132.   //enviando los valores
  133.  
  134.   ajax1.send("nombres1"+nom1)
  135.  
  136.   }
  137.  
  138. function LimpiarCampos1(){
  139.  
  140.  
  141.   document.nuevo_empleado1.nombres1.value="";
  142.   document.nuevo_empleado1.nombres1.focus();
  143.  
  144.   }
  145.  
  146. var xmlhttp = function()
  147. * * {
  148. * * * * var a;try{a = new XMLHttpRequest();}
  149. * * * * catch(e){try{a = new ActiveXObject('Msxml2.XMLHTTP');}
  150. * * * * catch(e){try{a = new ActiveXObject('Microsoft.XMLHTTP');}
  151. * * * * catch(e){alert('Your browser doesn\'t support ajax');a=false;}
  152. * * * * }}return a;
  153. * * };*
  154. * * window.onload = function()
  155. * * {
  156. * * * * var a = new comet();
  157. * * };*
  158. * * var comet = function()
  159. * * {
  160. * * * * var a = new xmlhttp();
  161. * * * * a.open('post',window.location+"?"+Math.random()+"="+Math.random(), true);
  162. * * * * a.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  163. * * * * a.onreadystatechange = function()
  164. * * * * {
  165. * * * * * * if(a.readyState == 4)
  166. * * * * * * {
  167. * * * * * * * * document.getElementById('content').innerHTML = a.responseText;
  168. * * * * * * * * window.setTimeout(function(){
  169. * * * * * * * * * * a = new comet();* * * * * * * * * *
  170. * * * * * * * * });
  171. * * * * * * * *
  172. * * * * * * }
  173. * * * * };
  174. * * * * a.send('algo=algo');* * * * * * * *
  175. * * };
y por ultimo este es el registro.php
Código PHP:
Ver original
  1. <?php
  2.  
  3. //Configuracion de la conexion a base de datos
  4.  
  5.   $bd_host = "localhost";
  6.  
  7.   $bd_usuario = "root";
  8.  
  9.  
  10.   $bd_password = "";
  11.  
  12.   $bd_base = "db";
  13.  
  14. $con = mysql_connect($bd_host, $bd_usuario, $bd_password);
  15.  
  16. mysql_select_db($bd_base, $con);
  17.  
  18. //variables POST
  19.  
  20.   $nom=$_POST['nombres'];
  21.   $nom1=$_POST['nombres1'];
  22.  
  23. if($nom!='')
  24. {//registra los datos del empleados
  25. $sql="INSERT INTO messages (estado,user_id) VALUES ('$nom','1')";
  26.  
  27. mysql_query($sql,$con);
  28. }
  29. else
  30. {
  31. $sql="INSERT INTO coment (estado) VALUES ('$nom1')";
  32. mysql_query($sql,$con);
  33. }
  34.  ?>
Nose si lo hago bien o que es lo que le pasa pero estaría muy agradecido si me ayudaraís gracias un saludo.