Question

I have a form with two options, when onclick AJAX call to a php file which creates a temporary table with all possible results. So if the users select another option again it would load faster.

The problem is that the ajax call to the php file always creates and inserts the elements all over again, how can I avoid this?, how can I create the temporary table once.

is it possible to call the php file only once?

Sorry for the bad expression but I'm kinda new to PHP,AJAX, etc.

My ajax call:

<script type="text/javascript">
        function showAliveData(str,str2){
            if (str=="null" || str2=="null")
              {
              document.getElementById("resultados").innerHTML="";
              return;
              } 
            if (window.XMLHttpRequest)
              {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
              }
            else
              {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
            xmlhttp.onreadystatechange=function()
              {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                document.getElementById("resultados").innerHTML=xmlhttp.responseText;
                $("#tablaResultados").tablesorter("update");
                }
              }
            document.getElementById("resultados").innerHTML='<div id="loader">La conexión con Google Analytics puede tardar unos segundos, por favor espere..</div>';  
            xmlhttp.open("GET","test.php?seccion="+str+"&elemento="+str2,true);
            xmlhttp.send();
        }
    </script>

HTML:

<form name="form">
            <select name="seccion">
                <option value="null" style="color:#D3D3D3" disabled="disabled" selected="selected">Sección</option>
                    <option value="Home">Home</option><br/>
                    <option value="Promociones">Promociones</option>
                        <option value="all">- Todas las secciones -</option>                    
            </select>
            <select name="elemento">
                <option value="null" style="color:#D3D3D3" disabled="disabled" selected="selected">Elemento</option>                                
                    <option value="Vitrina">Vitrina</option><br/>
                    <option value="Banner">Banner</option>
                    <option value="Destinos destacados">Destinos destacados</option>
                    <option value="Box">Box</option>
                    <option value="all">- Todos los elementos -</option>                    
            </select>

            <input type="button" value="Alive" onclick="showAliveData(seccion.value,elemento.value)">

The PHP file:

Basically
-Creates the temporary table
-Inserts all data
-display the data the user selected

Thank you in advance!

Était-ce utile?

La solution

By default, all the temporary tables are deleted by MySQL when your database connection gets terminated.

Perhaps temporary tables aren't for you in this scenario as they will not persist between AJAX calls.

Now, temporary tables are inherently supposed to be fast and efficient, so if it's not OK that your temporary table is created each time, you could bypass it on subsequent requests by "caching" your temporary table results in a session variable (which you would use instead of the temp table if it exists).

Ref: http://www.tutorialspoint.com/mysql/mysql-temporary-tables.htm

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top