Visualizzazione dei risultati da 1 a 5 su 5

Discussione: Codifica ajax

  1. #1
    Utente di HTML.it L'avatar di newlink
    Registrato dal
    Oct 2011
    Messaggi
    206

    Codifica ajax

    Salve a tutti, ho un problema, credo di codifica, con uno script javascript+ajax+php.

    Praticamente, ho scritto una funzione che preleva il testo da un input e tramite jquery la manda ad una pagina php dove viene inserta nel db. Dato che il passaggio avviene tramite GET ho preferito passare la stringa tramite Base64 per evitare errori, quello che ho notato è che prima del caricamento nel db mi vengono persi alcuni caratteri tra cui il \n. Sapete dirmi dove può essere il problema o indicarmi un metodo alternativa

    codice:
    function add_comment(id_post, userid, width)
     {
      var text_comment;
      text_comment = document.getElementById('etesto_form_'+id_post).value;
      text_comment = Base64.encode(text_comment);
    
      $.ajax({
        type: "GET",
        url: "add_comment.php",
        data: "id_post="+id_post+"&testo_form="+text_comment,
        success: function(risul)
        {
         if(risul == 1)
          {
           post_comment_reset(id_post);
           refresh_post_comment(id_post, userid, width);
          }
        }
      });
     }
    
    var Base64 = {
     
    	// private property
    	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
     
    	// public method for encoding
    	encode : function (input) {
    		var output = "";
    		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
    		var i = 0;
     
    		input = Base64._utf8_encode(input);
     
    		while (i < input.length) {
     
    			chr1 = input.charCodeAt(i++);
    			chr2 = input.charCodeAt(i++);
    			chr3 = input.charCodeAt(i++);
     
    			enc1 = chr1 >> 2;
    			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
    			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
    			enc4 = chr3 & 63;
     
    			if (isNaN(chr2)) {
    				enc3 = enc4 = 64;
    			} else if (isNaN(chr3)) {
    				enc4 = 64;
    			}
     
    			output = output +
    			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
    			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
     
    		}
     
    		return output;
    	},
     
    	// public method for decoding
    	decode : function (input) {
    		var output = "";
    		var chr1, chr2, chr3;
    		var enc1, enc2, enc3, enc4;
    		var i = 0;
     
    		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
     
    		while (i < input.length) {
     
    			enc1 = this._keyStr.indexOf(input.charAt(i++));
    			enc2 = this._keyStr.indexOf(input.charAt(i++));
    			enc3 = this._keyStr.indexOf(input.charAt(i++));
    			enc4 = this._keyStr.indexOf(input.charAt(i++));
     
    			chr1 = (enc1 << 2) | (enc2 >> 4);
    			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
    			chr3 = ((enc3 & 3) << 6) | enc4;
     
    			output = output + String.fromCharCode(chr1);
     
    			if (enc3 != 64) {
    				output = output + String.fromCharCode(chr2);
    			}
    			if (enc4 != 64) {
    				output = output + String.fromCharCode(chr3);
    			}
     
    		}
     
    		output = Base64._utf8_decode(output);
     
    		return output;
     
    	},
     
    	// private method for UTF-8 encoding
    	_utf8_encode : function (string) {
    		string = string.replace(/\r\n/g,"\n");
    		var utftext = "";
     
    		for (var n = 0; n < string.length; n++) {
     
    			var c = string.charCodeAt(n);
     
    			if (c < 128) {
    				utftext += String.fromCharCode(c);
    			}
    			else if((c > 127) && (c < 2048)) {
    				utftext += String.fromCharCode((c >> 6) | 192);
    				utftext += String.fromCharCode((c & 63) | 128);
    			}
    			else {
    				utftext += String.fromCharCode((c >> 12) | 224);
    				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
    				utftext += String.fromCharCode((c & 63) | 128);
    			}
     
    		}
     
    		return utftext;
    	},
     
    	// private method for UTF-8 decoding
    	_utf8_decode : function (utftext) {
    		var string = "";
    		var i = 0;
    		var c = c1 = c2 = 0;
     
    		while ( i < utftext.length ) {
     
    			c = utftext.charCodeAt(i);
     
    			if (c < 128) {
    				string += String.fromCharCode(c);
    				i++;
    			}
    			else if((c > 191) && (c < 224)) {
    				c2 = utftext.charCodeAt(i+1);
    				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
    				i += 2;
    			}
    			else {
    				c2 = utftext.charCodeAt(i+1);
    				c3 = utftext.charCodeAt(i+2);
    				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
    				i += 3;
    			}
     
    		}
     
    		return string;
    	}
     
    }
    Grazie mille

  2. #2
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    Il problema starà sicuramente nella funzione di codifica. Perfettamente inutile visto che esiste una funzione nativa per la codifica base64:

    codice:
    atob(text_comment)
    Ma poiché tu non stai mandando un file binario, non ti serve la codifica base64. Ti occorre semplicemente la codifica riservata agli url:

    codice:
    alert(escape("Ciao mondo!"));
    Cancella tutto quel codice inutile.

    codice:
    function add_comment(id_post, userid, width)
     {
      var text_comment;
      text_comment = document.getElementById('etesto_form_'+id_post).value;
    
      $.ajax({
        type: "GET",
        url: "add_comment.php",
        data: "id_post="+id_post+"&testo_form="+escape(text_comment),
        success: function(risul)
        {
         if(risul == 1)
          {
           post_comment_reset(id_post);
           refresh_post_comment(id_post, userid, width);
          }
        }
      });
     }
    N.B. Al php arriverà il testo già automaticamente riconvertito.

  3. #3
    Utente di HTML.it L'avatar di newlink
    Registrato dal
    Oct 2011
    Messaggi
    206
    Ciao grazie per la risposta, ho provato ad utilizzare la funzione escape ma il problema rimane... Gli a capo non vengono inseriti. Ti allego la codifica che utilizzo nel file php anche se il problema non dovrebbe essere qui perchè quando non usavo il passaggio tra pagine lo script funzionava.

    Codice PHP:
      $testo_form strip_tags($testo_form);
      
    $testo_form mysql_real_escape_string($testo_form);
      
    $testo_form str_replace('\r\n''
    '
    $testo_form);
      
    $testo_form stripslashes(stripslashes($testo_form)); 

  4. #4
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    Non sono pratico di php. Ti posso solo dire che facendo come ti ho detto al php arriva il messaggio già pronto, non lo devi decodificare (come sarebbe stato se avessi invece seguito la via della codifica base64). Fatti spostare nella sezione php.

  5. #5
    Utente di HTML.it L'avatar di newlink
    Registrato dal
    Oct 2011
    Messaggi
    206
    Ho risolto il problema, non so bene perché in questo caso bisogna scrivere:

    Codice PHP:
    $testo_form str_replace('\n''
    '
    $testo_form); 

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 © 2026 vBulletin Solutions, Inc. All rights reserved.