Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Jul 2004
    Messaggi
    8

    Creare codice HTML pulsante in modo dinamico

    Premesso che mastico javascript da solo una settimana, mi e' stato chiesto di creare un form per un pagamento online utilizzando paypal. Form e controlli sono ok, mi rimane da sistemare un'ultima cosa, e sinceramente non so più dove sbattere la testa.

    Il problema e' il seguente: In base alle opzioni selezionate nel form (composto da radio, check e select) devo creare dinamicamente un pulsante per effettuale il pagamento su Paypal. Il problema e' che quando eseguo la pagina, invece che creare il tasto, mi inserisce tutto il codice sottoforma di testo.

    Vi posto il codice della funzioncina che genera il tasto nella speranza che voi riusciate dove io ho miseramente fllito

    codice:
    function PP_button(){
    
    	child1 = "<form name='_xclick' target='paypal' action='https://www.paypal.com' method='post'>";
    	child1 += "<input type='hidden' name='cmd' value='_cart'>";
    	child1 += "<input type='hidden' name='upload' value='1'>";
    	child1 += "<input type='hidden' name='business' value='"+pp_account+"'>";
    	child1 += "<input type='hidden' name='currency_code' value='EUR'>";
    	
    	if(document.formpag.prezzo1.checked==true || document.formpag.prezzo2.checked==true){
    		var name1 = mem_type+" with reduction";
    		var amount1 = totale_parz;
    		child2 = "<input type='hidden' name='item_name_1' value='"+name1+"'>";
    		child2 += "<input type='hidden' name='amount_1' value='"+amount1+",00'>";
    	} else {
    		var name1 = mem_type;
    		var amount1 = totale_parz;
    		child2 = "<input type='hidden' name='item_name_1' value='"+name1+"'>";
    		child2 += "<input type='hidden' name='amount_1' value='"+amount1+",00'>";
    	}
    	
    	if((document.formpag.acc_pers1.value == 0 && document.formpag.acc_pers2.value == 0) && (document.formpag.dinner1.value == 0 && document.formpag.dinner2.value == 0)){
    	
    		child5 = "<input type='submit' name='PP_sub' value='Pay now' >";
    		child5 += "</form>";
    		
    		var check = document.getElementById("pp_btn").childNodes[0];
    		if(check==null){
    			var code_node1 = document.createTextNode(child1+child2+child5);
    			document.getElementById("pp_btn").appendChild(code_node1);
    		} else {
    			var code_node1 = document.createTextNode(child1+child2+child5);
    			document.getElementById("pp_btn").appendChild(code_node1);
    		
    			document.getElementById("pp_btn").removeChild(document.getElementById("pp_btn").childNodes[0]);
    		}
    	
    	} else if((document.formpag.acc_pers1.value != 0 || document.formpag.acc_pers2.value != 0) && (document.formpag.dinner1.value == 0 && document.formpag.dinner2.value == 0)){
    
    		var name2 = "Accompanying person";
    		var amount2 = parseInt(person[1]);
    		var quantity2 = person[0];
    		child3 = "<input type='hidden' name='item_name_2' value='"+name2+"'>";
    		child3 += "<input type='hidden' name='amount_2' value='"+amount2+",00'>";
    		child3 += "<input type='hidden' name='quantity_2' value='"+quantity2+"'>";
    		
    		child5 = "<input type='submit' name='PP_sub' value='Pay now' >";
    		child5 += "</form>";
    		
    		var check = document.getElementById("pp_btn").childNodes[0];
    		if(check==null){
    			var code_node1 = document.createTextNode(child1+child2+child3+child5);
    			document.getElementById("pp_btn").appendChild(code_node1);
    		} else {
    			var code_node1 = document.createTextNode(child1+child2+child3+child5);
    			document.getElementById("pp_btn").appendChild(code_node1);
    		
    			document.getElementById("pp_btn").removeChild(document.getElementById("pp_btn").childNodes[0]);
    		}
    	
    	} else if ((document.formpag.acc_pers1.value == 0 && document.formpag.acc_pers2.value == 0) && (document.formpag.dinner1.value != 0 || document.formpag.dinner2.value != 0)){
    	
    		var name2 = "Gala dinner";
    		var amount2 = parseInt(dinner[1]);
    		var quantity2 = dinner[0];
    		child3 = "<input type='hidden' name='item_name_2' value='"+name2+"'>";
    		child3 += "<input type='hidden' name='amount_2' value='"+amount2+",00'>";
    		child3 += "<input type='hidden' name='quantity_2' value='"+quantity2+"'>";
    		
    		child5 = "<input type='submit' name='PP_sub' value='Pay now' >";
    		child5 += "</form>";
    		
    		var check = document.getElementById("pp_btn").childNodes[0];
    		if(check==null){
    			var code_node1 = document.createTextNode(child1+child2+child3+child5);
    			document.getElementById("pp_btn").appendChild(code_node1);
    		} else {
    			var code_node1 = document.createTextNode(child1+child2+child3+child5);
    			document.getElementById("pp_btn").appendChild(code_node1);
    		
    			document.getElementById("pp_btn").removeChild(document.getElementById("pp_btn").childNodes[0]);
    		}
    		
    	} else if ((document.formpag.acc_pers1.value != 0 || document.formpag.acc_pers2.value != 0) && (document.formpag.dinner1.value != 0 || document.formpag.dinner2.value != 0)){
    	
    		var name2 = "Accompanying person";
    		var amount2 = parseInt(person[1]);
    		var quantity2 = person[0];
    		var name3 = "Gala dinner";
    		var amount3 = parseInt(dinner[1]);
    		var quantity3 = dinner[0];
    		child3 = "<input type='hidden' name='item_name_2' value='"+name2+"'>";
    		child3 += "<input type='hidden' name='amount_2' value='"+amount2+",00'>";
    		child3 += "<input type='hidden' name='quantity_2' value='"+quantity2+"'>";
    	
    		child4 = "<input type='hidden' name='item_name_3' value='"+name3+"'>";
    		child4 += "<input type='hidden' name='amount_3' value='"+amount3+",00'>";
    		child4 += "<input type='hidden' name='quantity_3' value='"+quantity3+"'>";
    
    		child5 = "<input type='submit' name='PP_sub' value='Pay now' >";
    		child5 += "</form>";
    
    		var check = document.getElementById("pp_btn").childNodes[0];
    		if(check==null){
    			var code_node1 = document.createTextNode(child1+child2+child3+child4+child5);
    			document.getElementById("pp_btn").appendChild(code_node1);
    		} else {
    			var code_node1 = document.createTextNode(child1+child2+child3+child4+child5);
    			document.getElementById("pp_btn").appendChild(code_node1);
    		
    			document.getElementById("pp_btn").removeChild(document.getElementById("pp_btn").childNodes[0]);
    		}
    
    	}	
    }
    e nel body:
    codice:
    <div id="pp_btn"></div>
    Dal mio modesto punto di vista il problema risiede in quel "createTextNode", purtroppo pero' non so in che modo agirare il problema.

    P.S. Provando con innerHTML il tutto funziona su IE ma non su firefox, quindi non posso utilizzarlo.


    Grazie

  2. #2
    Utente di HTML.it L'avatar di floyd
    Registrato dal
    Apr 2001
    Messaggi
    3,837
    non devi usare createTextNode
    dovresti creare i tag con createElement e appendChild, ma con innerHTML deve funzionare anche su ff http://www.devguru.com/technologies/xml_dom/index.asp

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.