Scriptino Ajax che non funge

camionistaxcaso

Nuovo Utente
14 Mag 2015
11
0
1
Ciao a tutti, come da titolo sto creando questo script per fare una richiesta asincrona. Diciamo che funzionerebbe a parte l' ultima parte dove, in base al parametro passato, invia le variabili con get o post. Insomma da proprio problemi nell' if finale. Grazie a tutti.

HTML:
function ajaxRequest(method, page, div, urlEncode)
  { 
  //alert('Funzione ajaxRequest() caricata correttamente');  
    
    objxmlhttp = xmlhttp();
    objxmlhttp.onreadystatechange=function()          
         {  
         //Simbolo di attesa          
           var caricamento="<img src='../../image/loading.gif' height='40px'>"            
           document.getElementById(div).innerHTML=caricamento;                     
           if (objxmlhttp.readyState==4 && objxmlhttp.status==200)            
           {                           
            setTimeout(
                  function()
                      {
                       document.getElementById(div).innerHTML=objxmlhttp.responseText;
                     }
                     ,2000
                        );                       
             }          
  
      }
     
     //mi si inchioda in questa parte   
     objxmlhttp.open(method, page, true);        
     if(method == "POST")
     {
     objxmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
     objxmlhttp.send(urlEncode);
     }
     else
     {
       objxmlhttp.send();
     }
      
  }
       </script>
      
<input type="button" name="delete_jobs_input" value="Elimina" 
onclick="ajaxRequest('GET', 'test_3.1.php', 'divPostDel', 'id_jobs=3')">


<div id="divPostDel"></div>
 
ciao, per simpatia e solidarietà cambierò il mio nick in "tutanKamion", però lascio a te confrontare il mio esempio con il tuo
Marino
HTML:
<!DOCTYPE html>
<html>
<head>
  <title>ajaxRequest</title>

  <script type="text/javascript">

function ajaxRequest(AjaxMethod, AjaxFile, AjaxHtml, AjaxParams)
{
//  alert('Funzione ajaxRequest caricata correttamente\n\r'
//	+ '\n\r'+AjaxMethod + '\n\r'+AjaxFile + '\n\r'+AjaxHtml + '\n\r'+AjaxParams); 

  var xhr, AjaxText;
  AjaxText = document.getElementById(AjaxHtml);

  AjaxFile = AjaxFile + "?timestamp=" + new Date().getTime();

  try
  {
    window.XMLHttpRequest 
      ? xhr = new XMLHttpRequest()
      : xhr = new ActiveXObject("Microsoft.XMLHTTP");
  }
  catch (e)
  {
    AjaxText.innerHTML = "AJAX non funziona sul tuo browser";
  }
  xhr.onreadystatechange = function()
  {
    /* Gli stati di una richiesta possono essere 5
     * 0 - UNINITIALIZED
     * 1 - LOADING
     * 2 - LOADED
     * 3 - INTERACTIVE
     * 4 - COMPLETE
     */

    AjaxText.innerHTML="<img src='loading.gif' height='40px'>"; 

    if (xhr.readyState == 4)
    {
      setTimeout ( function() {
        xhr.status == 200 
          ? AjaxText.innerHTML = xhr.responseText 
          : AjaxText.innerHTML = "Si è verificato un errore nel tentativo di usare AJAX";
      }, 2000 ); 
    }
  }

  if (AjaxMethod == "GET")
  {
    AjaxFile = AjaxFile + "&" + AjaxParams;
    xhr.open(AjaxMethod, AjaxFile, true);
    xhr.send(); 
  }
  else
  {
    xhr.open(AjaxMethod, AjaxFile, true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send(AjaxParams); 
  }
}

</script>
</head>
<body>
  <h1>ajaxRequest</h1><br /><br />

  <input type="button" name="delete_jobs_input" value="EliminaGET" 
    onclick="ajaxRequest('GET', 'esempio_5.php', 'divPostDel', 'id_jobs=3')" />

  <input type="button" name="delete_jobs_input" value="EliminaPOST" 
    onclick="ajaxRequest('POST', 'esempio_5.php', 'divPostDel', 'id_jobs=3')" />

  <br /> <br /> 

  <div id="divPostDel"> </div>
</body>
</html>

PHP:
<?php
$id_jobs = "NON DEF.";
$tabella  = "";

if(!empty($_GET))
{
  extract($_GET,  EXTR_OVERWRITE);

  $tabella.= '<h3>GET</h3>';
  $tabella.= '<table width="800" border="0" cellspacing="5" cellpadding="5">';
  while(list($chiave, $valore)=each($_GET))
    $tabella.= "<tr><td>".$chiave." : </td><td>".${$chiave}."</td></tr>";
  $tabella.= "</table>";
}

if(!empty($_POST))
{
  extract($_POST, EXTR_OVERWRITE);

  $tabella.= '<h3>POST</h3>';
  $tabella.= '<table width="800" border="0" cellspacing="5" cellpadding="5">';
  while(list($chiave, $valore)=each($_POST))
    $tabella.= "<tr><td>".$chiave." : </td><td>".${$chiave}."</td></tr>";
  $tabella.= "</table>";
}

echo "ho cancellato il job ".$id_jobs."<br />".$tabella;
?>
 
Leva il 5 dal nick perchè sei il numero 1! :D
Mi hai risolto anche un altro problema che mi dava e cioè che se facevo 2 richieste insieme la seconda non mi andava a buon fine. Non ho capito perchè l' open va messo per forza dentro ad ogni condizione :) Grazie sei stato superfantastico.
 
la open ho preferito inserirla nella condizione perché GET vuole i parametri nella url, POST li vuole nella send,
avrei dovuto quindi aggiungere altre condizioni, ma lo script era così "lineare" ("bello")

poi ho inserito anche il time nell'url per evitare problemi di cache, già affrontati in passato
ciao
Marino
 

Discussioni simili