Ver Mensaje Individual
  #2 (permalink)  
Antiguo 10/04/2012, 06:05
quimfv
Colaborador
 
Fecha de Ingreso: marzo-2008
Ubicación: Sabadell
Mensajes: 4.897
Antigüedad: 16 años, 1 mes
Puntos: 574
Respuesta: problemas con menu ajax y boton volver

Si se ha hecho el borrado en bbdd simplemente vuelve a llamar la pagina de borrado se cargará con lo que haya en la bbdd....

Junta las dos ultimas paginas que nos muestras (sin el header), pero rodeando de un condicional las primeras lineas de la ultima si no llega nada por post que no haga el update ... y si el resto de la pagina (la segunda que nos muestras).... despues de la seleccion rellamas a la misma pagina, por AJAX, pero con el post lleno asi se ejectura el UPDATE y despues te mostrará los productos que queden...

Se trata de recargar solo el div "response" no recargar todo el index...

Te adjunto las funciones de js que deben substiuir el action del formulario...

Algo asi


Código PHP:
Ver original
  1. <?php
  2. include_once 'lib.php';
  3. $conexion= mysql_connect($dbhost, $dbuser, $dbpassword);
  4. mysql_select_db($database, $conexion);
  5.  
  6. // Si hay una seleccion modificamos la bbdd
  7.  
  8. if (isset($_POST['seleccion'])){
  9.        foreach ($_POST['seleccion'] as $id){
  10.             echo $id."<br>";
  11.        }  
  12.        // Generamos una lista de los ID's (campo value= ..)
  13.        //que tenemos en nuestro array.
  14.        $lista=implode(',',$_POST['seleccion']);
  15.         // Y lo aplicamos al SQL correspondiente y ejecutamos la consulta.
  16.         mysql_query("UPDATE stock set activo = 0 WHERE id_stock IN(".$lista.")");
  17. }
  18.  
  19.  
  20. // Haya o no seleccion mostramos el contenido de stock
  21.  
  22. $result = mysql_query("SELECT id_stock, codigo, descrip, pre_min, pre_may, disponibles  FROM stock where activo = '1'", $conexion);
  23.  
  24. ?>
  25. <form name='borrar'>
  26.     <table id="etiquetas" border='1'>
  27. <tr style="background-color: #ffff99">
  28.     <th>ID</th>
  29.     <th>Codigo</th>
  30.     <th style='width: 500px'>Descrip</th>
  31.     <th>Prec.Min</th>
  32.     <th>Prec.May</th>
  33.     <th>Disponibles</th>
  34.     <th></th>
  35. </tr>
  36. <?php
  37. $i=0;
  38. while($row = mysql_fetch_array($result)){
  39. ?>
  40.     <tr>
  41.         <td><?php echo $row['id_stock'] ?></td>
  42.         <td><?php echo $row['codigo'] ?></td>
  43.         <td style='width: 500px'><?php echo $row['descrip'] ?></td>
  44.         <td align=right><?php echo "$"; echo $row['pre_min'] ?></td>
  45.         <td align=right><?php echo "$"; echo $row['pre_may'] ?></td>
  46.         <td align=center><?php echo $row['disponibles'] ?></td>
  47.         <td><input name='seleccion[]' type='checkbox' value=<?php echo $row['id_stock']?>></td>
  48.      </tr>
  49. <?php    
  50.     $i++;
  51. }
  52. ?>
  53. </table>
  54. <br>
  55. <input type='button' name='submit' value='enviar' onclick='clSdInFrZnPost("response","stock.php",this.form)'>
  56. </form>

Funciones que te serviran para recargar solo el div "response", deberás modificar la primera para que cargue el array de la selección...


Código Javascript:
Ver original
  1. function clSdInFrZnPost(id,url,formulari){
  2.     var parametres="";
  3.     var j=0;
  4.     for (i=0;i<formulari.elements.length;i++){
  5.         if (formulari.elements[i].type!="button"){
  6.             j++;
  7.             if(j!=1)parametres+="&";
  8.             if (formulari.elements[i].type=="checkbox"){
  9.                 if(formulari.elements[i].checked){
  10.                     parametres+=formulari.elements[i].id+"="+true;
  11.                 }else{
  12.                     parametres+=formulari.elements[i].id+"="+false;
  13.                 }
  14.             }else if (formulari.elements[i].type=="radio"){
  15.                     if(formulari.elements[i].checked){
  16.                         parametres+=formulari.elements[i].id
  17.                                                               +"="+formulari.elements[i].value;
  18.                     }
  19.             }else{
  20.             parametres+=formulari.elements[i].id
  21.                                                    +"="+formulari.elements[i].value;
  22.             }
  23.         }
  24.     }
  25.          //Si descomentas este alert veras que parametros lanzas....
  26.     //alert(parametres);
  27.     clientSideIncludePost(id, url, parametres)
  28. }
  29.  
  30. function clientSideIncludePost(id, url, parametres) {
  31.   var req = false;
  32.   // For Safari, Firefox, and other non-MS browsers
  33.   if (window.XMLHttpRequest) {
  34.     try {
  35.       req = new XMLHttpRequest();
  36.     } catch (e) {
  37.       req = false;
  38.     }
  39.   } else if (window.ActiveXObject) {
  40.     // For Internet Explorer on Windows
  41.     try {
  42.       req = new ActiveXObject("Msxml2.XMLHTTP");
  43.     } catch (e) {
  44.       try {
  45.         req = new ActiveXObject("Microsoft.XMLHTTP");
  46.       } catch (e) {
  47.         req = false;
  48.       }
  49.     }
  50.   }
  51.  //req.expires=-1;
  52.  var element = document.getElementById(id);
  53.  if (!element) {
  54.   alert("Bad id " + id +
  55.    "passed to clientSideInclude." +
  56.    "You need a div or span element " +
  57.    "with this id in your page.");
  58.   return;
  59.  }
  60.   if (req) {
  61.     // Synchronous request, wait till we have it all
  62.     req.open('POST', url, false);
  63.     req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  64.     req.send(parametres);
  65.     element.innerHTML = req.responseText;
  66.   } else {
  67.     element.innerHTML =
  68.    "Sorry, your browser does not support " +
  69.       "XMLHTTPRequest objects. This page requires " +
  70.       "Internet Explorer 5 or better for Windows, " +
  71.       "or Firefox for any system, or Safari. Other " +
  72.       "compatible browsers may also exist.";
  73.   }
  74.    
  75. }
__________________
Quim
--------------------------------------------------
Ayudar a ayudar es una buena práctica!!! Y da buenos resultados.

Última edición por quimfv; 10/04/2012 a las 06:18