Ver Mensaje Individual
  #3 (permalink)  
Antiguo 15/12/2011, 06:38
lemonstar
 
Fecha de Ingreso: diciembre-2011
Mensajes: 9
Antigüedad: 12 años, 4 meses
Puntos: 0
Respuesta: Incremento de variable Ajax y checkbox

Ok voy a intentar , espero que lo entiendan


1. En el index.html:

Código:
//aca carga la tabla con todos los registros
<div id="test2">
  <p>Cargando lista de películas, aguarde unos instantes...</p>
  <p><img src="./img/bigrotation2.gif" width="32" height="32" /></p>
</div>

//aca carga el espacio total
<div id="test3">
  <p>Espacio Total=</p>
</div>


2. En el script.js:

a)esta es la funcion AJAX que carga toda la tabla en el DIV TEST2

Código:
function CargaLista(str)
{
if (str=="")
  {

  document.getElementById("test2").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari

  xmlhttp2=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5

  xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
  }
  

xmlhttp2.onreadystatechange=function()
  {
  if (xmlhttp2.readyState==4 && xmlhttp2.status==200)
    {
    document.getElementById("test2").innerHTML=xmlhttp2.responseText;
    }
  }
  
xmlhttp2.open("GET","cargalista.php",true);

xmlhttp2.send();
}

A la primer función la llamo cuando se carga la página
Código:
$(document).ready(function() {CargaLista();}


En cargalista.php:

Código:
<?php

header("Content-Type: application/xml; charset=ISO-8859-1");

$host2="localhost";
$user2="lalala_user";
$passwd2="lalal_pass";
$dbname2="lala_pm2";
$tabla2="peliculas";


$conexion2 = mysql_connect ($host2, $user2, $passwd2) or die ("No se puede conectar con el servidor");

mysql_select_db ($dbname2) or die ("No se puede seleccionar la base de datos");

$inst_sql2="SELECT * FROM peliculas order by original";

$consulta2 = mysql_query ($inst_sql2, $conexion2) or die ("Fallo en la consulta");

$muestra_consulta2=mysql_num_rows($consulta2);


$sPattern2 = '/\s*/m'; 
$sReplace2 = '';
$size_total=0;

echo "<table id='hor-zebra' border='1'>
<tr>
<th> </th>
<th>Titulo</th>
<th>A&ntilde;o</th>
<th>Res</th>
<th>Formato</th>
<th>G&eacute;nero</th>
<th>IMDB</th>
</tr>";

if ($muestra_consulta2> 0) {
	

while ($fila2 = mysql_fetch_array($consulta2)) {


$id2=$fila2['ID'];

$estreno2=$fila2['Estreno'];
$res2=$fila2['Res'];
$formato2=$fila2['Formato'];
$genero2=$fila2['Genero'];
$imdb2=$fila2['Imdb'];
$size2=$fila2['Size'];
$original2=$fila2['Original'];

$nuevo_original2="id".preg_replace( $sPattern2, $sReplace2, $original2 );

	echo "<tr>";
//ACA ES DONDE LE VOY A PASAR EL VALOR DEL CHECKBOX A LA OTRA FUNCIÓN
	echo "<td>" ."<INPUT TYPE='checkbox' NAME='originales[]' Value='$original2' id='$nuevo_original2' onclick='Incrementa(this.value)'/>". "</td>";
	echo "<td>" . $original2 . "</td>";
	echo "<td>" . $estreno2 . "</td>";
	echo "<td>" . $res2 . "P"."</td>";
	echo "<td>" . $formato2 . "</td>";
	echo "<td>" . $genero2 . "</td>";
	echo "<td>" . "<a target='_blank' href=http://www.imdb.es/title/". $imdb2 . "><img src='./img/imdb_logo.png' width='43' height='21'/></a>" . "</td>";
	echo "</tr>";

   }
}

echo "</table>";

mysql_close ($conexion2);

?>




b)esta es la funcion AJAX que carga el incremento en el DIV TEST3

Código:
function Incrementa(str)
{
if (str=="")
  {

  document.getElementById("test3").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari

  xmlhttp3=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5

  xmlhttp3=new ActiveXObject("Microsoft.XMLHTTP");
  }
  

xmlhttp3.onreadystatechange=function()
  {
  if (xmlhttp3.readyState==4 && xmlhttp3.status==200)
    {
    document.getElementById("test3").innerHTML=xmlhttp3.responseText;
    }
  }
xmlhttp3.open("GET","gb.php?idoriginalgb="+str,true);

xmlhttp3.send();


}








El php donde tendría que incrementar (gb.php):

Código:
<?php

header("Content-Type: application/xml; charset=ISO-8859-1");

$idoriginal=$_GET["idoriginalgb"];

//Muestra el titulo de la pelicula
echo $idoriginal;



$host="localhost";

$user="lalalaaa";
$passwd="lalal";
$dbname="lalalala";
$tabla="peliculas";


$conexion = mysql_connect ($host, $user, $passwd) or die ("No se puede conectar con el servidor");


mysql_select_db ($dbname) or die ("No se puede seleccionar la base de datos");



$inst_sql="select Size from ($tabla) where Original ='$idoriginal'";





$consulta = mysql_query ($inst_sql, $conexion) or die ("Fallo en la consulta");



$muestra_consulta=mysql_num_rows($consulta);



if ($muestra_consulta> 0 ) {
	


while ($fila3 = mysql_fetch_array($consulta)) {
$size3=$fila3['Size'];

$size_total=$size_total+$size3;

echo "<table border='1'>
<tr>
<th>Espacio</th>
</tr>";
	echo "<tr>";
	echo "<td>" . $size_total . "</td>";
	echo "</tr>";

echo "</table>";
   }
   
}


mysql_close ($conexion);
	

?>


Eso fué todo, vuelvo a repetir Al cliquear en un checbox muestra correctamente en DIV TEST3 el valor con el nombre de película, el problema es al cliquear en otro checkbox en cambio de sumar el valor, cambia todo y eso es porque estoy llamando nuevamente a la función, ¿como debería incrementar una variable y donde guardarla para luego mostrarla???


GRACIASSSSS desde yaa por la ayuda

Última edición por lemonstar; 15/12/2011 a las 06:47