Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    Ajax Problemi di Insert in un database

    Ciao ragazzi ho creato uno script "inserisciCommento.js" in questo modo:

    codice:
    var myRequest = null;
    var div = null;
    
    function CreateXmlHttpReq(handler) {
     var xmlhttp = null;
      try {
        xmlhttp = new XMLHttpRequest();
      } catch(e) {
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
      }
      xmlhttp.onreadystatechange = handler;
      return xmlhttp;
    } 
    
    
    function myHandler() { 
        if (myRequest.readyState == 4 && myRequest.status == 200) {
            e = document.getElementById(div);
            e.innerHTML = myRequest.responseText;
        }
    }
     
    function sendDati(n, m, t, id, div, sub){ 
    	var name = document.getElementById(n);
    	var mail = document.getElementById(m); 
        var testo = document.getElementById(t); 
    	div = document.getElementById(div);
        var subscribe = document.getElementById(sub);		
    	var espressione = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;
    	var r = Math.random(); 
        if((mail.value == "") || (mail.value == "undefined") || (testo.value == "") || (testo.value == "undefined") || !espressione.test(mail.value)) {
    		alert("I campi Email e Commento vanno compilati obbligatoriamente e in maniera corretta!.");
    		return false;
     	} else {
    		myRequest = CreateXmlHttpReq(myHandler);
    		var e = document.getElementById(div); 
    		var url = "inserisciCommento.php?";
    		url+="name="+escape(name.value);
    		url+="&mail="+escape(mail.value);
    		url+="&testo="+escape(testo.value);
    		url+="&subscribe="+escape(subscribe.value);
    		url+="&postId="+escape(id);
    		url+="&rand="+escape(r);
    		e.innerHTML = "Invio Commento..."; 
    		try {
    			myRequest.open("GET",url, true); 
        		myRequest.send(null); 
           	} catch (err) {
        		displayError(err.toStrong());
        	} 
        	return true;
    	}
    }
    che sta all'inderno della cartella index_files

    poi nella "root" ho creato due files php:

    index.php:

    codice:
    [....]
    <div id="commento$id[$i]" style="display:none; width: 796px; padding-right: 2px;">
    
    
    <form name="commentaPost$id[$i]">
    <p id="commento3">Nome:<input type="text" id="name$id[$i]" name="name" size="50" /></p>
    
    
    <p id=\"commento3\">Email*:<input type="text" id="mail$id[$i]" name="mail" size="50"/></p>
    
    " .
    <p id="commento3">Commento*:</p>
    
    <textarea name="testo" id="testo$id[$i]" style="width: 796px; margin: 0 auto; padding: 0 auto;" cols="80%" rows="10" tabindex="4"></textarea>
    
    <p id="commento3"><input type="checkbox" name="subscribe" id="subscribe$id[$id]" value="subscribe" style="width: auto;" /> Notifica commenti via e-mail</p> 
    
    <input style="float: right" type="submit" value="Invia Post!" onclick="return sendDati('name$id[$i]','mail$id[$i]','testo$id[$i]', '$id[$i]', 'commento$id[$i]', 'subscribe$id[$id]')">
    </form>
    </div>
    [....]
    inserisciCommento.php:

    codice:
    nclude_once "connectToDatabase/database.php";
     
     $idpost = $_REQUEST['idpost'];
     $name = $_REQUEST['name'];
     $email = $_REQUEST['mail'];
     $text = $_REQUEST['testo'];
     $subscribe = $_REQUEST['subscribe'];
     ($subscribe == "subscribe") ? $sub = "Yes" : $sub = "No";
     $data = date("d-m-y");
     $ora = date("H:i");
     
    
     
     echo $data." ".$ora;
     
     $databaseCommento = new dataBase();
     $databaseCommento->connetti();
     
     $databaseCommento->query("INSERT INTO Commenti SET IdPost = '$idpost', Nome = '$name', Email = '$email', Commento = '$text', Data = '$data', Ora = '$ora', Stato = 'Convalidare', Subscribe = '$sub'");
     
     echo ("Il tuo post è in attesa di essere moderato. Verrai avvisato via mail della pubblicazione.");
     
     $databaseCommento->disconnetti();

    In pratica una volta inserito il record nel database dovrebbe cambiare il div...solo che non inserisce il record e ricarica la pagine index.php nella funzione ci entra sicuramente perchè fa il controllo sulla mail e sul testo.

    Sapreste dirmi dov'è l'errore?
    Grazie mille

  2. #2
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649

    Re: Ajax Problemi di Insert in un database

    Originariamente inviato da blindevil
    ...solo che non inserisce il record e ricarica la pagine index.php
    quindi probabilmente invia
    e tu hai un return true che viene comunque eseguito
    e se si verificano errori javascript invierai ugualmente

    inizierei a bloccare questo,
    verificando con firebug quello che succede all' invio

    ciao

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.