AJAX - evitar recargar pagina

 
Vista:
sin imagen de perfil

evitar recargar pagina

Publicado por mario (4 intervenciones) el 29/12/2013 02:08:33
hola amigos, estoy intentando hacer una prueba al visualizar unos articulos, que cuando pulse sobre el boton comprar, me muestre el ALERT sin recargarme la pagina, pero no me muestra el alert y el mensaje, he visto un tutorial que me ha ayudado mucho en el codigo ya que no tengo idea de ajax estoy empezando, podrian decirme que puede estar mal en el codigo para que no me muestre el mensaje.

Muchas gracias.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
include ("conectar.php");
session_start();?>
 
<!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">
<script type="text/javascript">
	function CreateXmLHttp(){
		if (window.XMLHttpRequest){
			XmlHttp=new XMLHttpRequest();
			} else if (window.ActiveXobject){
				XmlHttp= new ActiveXobject("Microsoft.XMLHttp");
		}
	}
</script>
 
<head>
 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
 
 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
 
<link rel="stylesheet" href="tienda.css"/>
<title>Documento sin título</title>
 
<script type="text/javascript">
        $(window).scroll(function()
        {
            if ($(this).scrollTop() >20)
            {
                $(".container").addClass("fixed");
                $("#body").addClass("fijada");
            }else{
                $(".container").removeClass("fixed");
                $("#body").addClass("fijada");
            }
        });
    </script>
</head>
 
<body id="body">
<div id="menu_lateral">
 
 	  <li><a href="#" >FACIAL</a> </li>
       <li><a href="#">FACIAL</a> </li>
        <li><a href="#">FACIAL</a> </li>
       <li><a href="#" >FACIAL</a> </li>
       <li><a href="#" >FACIAL</a> </li>
        <li><a href="#" >FACIAL</a> </li>
          <li><a href="#" >FACIAL</a> </li>
 </div>
 
<?php
 
 
$consulta=mysql_query("SELECT * FROM productos"); //selecciona todos los productos de la tabla.
 
	 	while($filas=mysql_fetch_array($consulta))
			{
 
		$id= $filas['id'];
		$imagen= $filas['imagen'];
		$nombre=$filas['nombre'];
		$descripcion=$filas['descripcion'];
		$precio=$filas['precio'];
		$existencias=$filas['existencias'];
		$fecha=$filas['fecha'];
 
		?>
 
 
 
    <table id="tabla" width="225" border="0">
 
  <tr>
    <td id="imagen"><img src="<?php echo $imagen; ?>" width="200px" height="175px" /> </td>
  </tr>
  <tr align="left">
    <td  id="titulo" width="225"  ><?php echo $nombre ?></td>
  </tr>
  <tr>
    <td  id="descripcion" width="225"  ><?php echo $descripcion ?></td>
  </tr>
  <tr>
    <td id="precio" width="225"><?php echo $precio?>
    		<form action="" method="post" name="comprar">
        	 <input name="id_txt" type="hidden" value="<?php echo $id ?>" id="id_txt"/>
        	 <input name="imagen" type="hidden" value="<?php echo $imagen ?>" id="imagen"/>
             <input name="titulo" type="hidden" value="<?php echo $nombre ?>" id="titulo"/>
             <input name="descripcion" type="hidden" value="<?php echo $descripcion ?>" id="descripcion"/>
             <input name="precio" type="hidden" value="<?php echo $precio ?>" id="precio"/>
     	  	 <input  type="button" value="Comprar" id="comprar" onclick="comprar()"/>
 
      </form>
 
 
    </td>
  </tr>
 
 
 <?php }?>
</table>
 
 
 
</body>
 
<script type="text/javascript">
<!--document.getElementById("id_txt").focus();-->
 
function comprar(){
	var titulo= document.getElementById("titulo").value;
 
 
	CreateXmLHttp();
	xmlHttp.onreadystatechange = procesardatos;
	xmlHttp.open("POST","procesa.php");
	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttp.send("titulo="+titulo);
}
 
function procesardatos(){
	if(xmlHttp.readystate==4 && xmlHttp.status==200){
		alert(xmlHttp.responseText);
	}
}
</script>
</html>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de xve
Val: 90
Oro
Ha mantenido su posición en AJAX (en relación al último mes)
Gráfica de AJAX

evitar recargar pagina

Publicado por xve (222 intervenciones) el 29/12/2013 19:52:29
Hola Mario, he visto tu código, y no entiendo muy bien el problema que nos comentas...

El alert(xmlHttp.responseText); muestra la respuesta que recibe del archivo procesa.php, no?

Puedes comentarnos con mas detalle?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar