Ver Mensaje Individual
  #10 (permalink)  
Antiguo 28/03/2011, 09:28
johhan16
 
Fecha de Ingreso: junio-2010
Ubicación: Venezuela, Zulia
Mensajes: 686
Antigüedad: 13 años, 9 meses
Puntos: 55
Respuesta: Consulta Sobre Ajax - MySQL

ok fijate el ejemplo que hice para ti sencillo
http://prueba.centerhipico.com/comentario.php

los codigos usados

ajax_comentario.js
Código HTML:
//ESTA PRIMERA PARTE DEL CODIGO SE HACE PARA CREAR EL OBJETO EN AJAX Y FUNCIONE EN LOS EXPLORADORES
function objetoAjax(){
	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;
}
//AQUI TERMINA ESTA FUNCION QUE NO CAMBIA


//ESTA FUNCIONE SI PUEDE CAMBIAR SEGUN SEA EL CASO
function ingresaComentario(comentario){
	//donde se mostrará el resultado
	divResultado = document.getElementById('respuesta');
	
	
		//instanciamos el objetoAjax
		ajax=objetoAjax();
		//uso del medotod GET
		ajax.open("GET", "comentario_ajax.php?comentario="+comentario);
		ajax.onreadystatechange=function() {
			
			if (ajax.readyState==4) {
				//mostrar resultados en esta capa
				divResultado.innerHTML = ajax.responseText
			}
		}
		//como hacemos uso del metodo GET
		//colocamos null
		ajax.send(null)
}
comentario.php

Código PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Prueba de Comentarios</title>
<script type="text/javascript" src="ajax_comentario.js"></script>


<script>
    function valida_envia(){
    
   
           if (document.forms.form1.elements['comentario'].value.length == 0) {
                alert('Por favor ingrese algun comentario');
                document.form1.comentario.focus() 
                return 0;
        }    
    
        else{ 
        
    ingresaComentario(document.forms.form1.elements['comentario'].value);            
        }
        return true;
   }
</script>

</head>

<body>
<form id="form1" name="form1" method="post" action="">
<table width="80%" border="0" align="center">
  <tr>
    <td colspan="2"><div align="center">Dime que piensas de mi.</div></td>
  </tr>
  <tr>
    <td width="46%"><div align="right">Agrega un Comentario</div></td>
    <td width="54%"><textarea name="comentario" cols="50" id="comentario"></textarea>
      <label>
      <input type="button" name="comentar" onclick="valida_envia(),this.form.comentario.value = ''" id="comentar" value="Comentar" />
    </label></td>
  </tr>
  </table>
</form>

<div id="respuesta">
<table width="80%" border="0" align="center">  
   <?php 
   
include_once('cone.php');
   
$link=conectarse();
   
   
$sql=mysql_query("select * from comentarios",$link);
   while(
$array=mysql_fetch_array($sql)) {
   
?>
   <tr>
    <td width="46%"></td>
    <td width="54%"><?php echo $array['comentario']; ?></td>
   </tr>
    <?php ?>
</table>
</div>
</body>
</html>
comentario_ajax.php

Código PHP:
<?php 

include_once('cone.php');
$link=conectarse();

$comentario=$_GET['comentario'];

mysql_query("insert into comentarios (comentario) values ('$comentario')",$link);

$sqlmysql_query("select * from comentarios",$link);
$filas mysql_fetch_row($sql);
if (
$filas 0) {
$sql2mysql_query("select * from comentarios",$link);
?>
<table width="80%" border="0" align="center">  
<?php 
while($array=mysql_fetch_array($sql2)) {
   
?>
   <tr>
    <td width="46%"></td>
    <td width="54%"><?php echo $array['comentario']; ?></td>
   </tr>
<?php ?>
</table>

  <?php } else { ?>
<div align="center">
<tr>
    <td colspan="2"><div align="center">NO HAY COMENTARIOS AUN</div></td>
</tr>
</div>
  <?php  ?>
cone.php

Código PHP:
<?php
function Conectarse()
{
      
$link mysql_connect('localhost','usuario','clave');
   
      
mysql_select_db('base_datos'$link);
      return 
$link;

}
?>
espero que te sirva aunque por supuesto le faltan muchos ajustes

edito: actualice el comentario.php para que funcione un poco mejor
__________________
<?php echo "No te metas a lo hondo del Mar si no sabes nadar, primero aprende a nadar" ?>
...Error en linea: 1 o.O

Última edición por johhan16; 28/03/2011 a las 09:48