• beyond tellerrand – play. Register Now!
Zurück   Flashforum > Flash > Stuff

Antwort
 
LinkBack Themen-Optionen Ansicht
Alt 23-11-2007, 16:59   #1 (permalink)
Alter User
 
Benutzerbild von Linowitch
 
Registriert seit: Nov 2006
Ort: München
Beiträge: 484
[stuff] Flexible Formular Klasse

Hallo,

ich habe eine kleine Formular Klasse gebaut, da ich flexibilität über alles liebe und stundenlange tipperei nicht ausstehen kann.

Ich hab hier schon diverse Formulare gesehen, bei denen man zur Anpassung aber trotzdem noch in den Quelltext eingreifen musste, deswegen gebe ich sie jetzt mal frei und hoffe auf rege Kritik, Wünsche, Verbesserungen, Lob oder was man einem sonst noch so alles an den Kopf schmeißen kann. :-)

Die Textfelder müssen selbstverständlich selbst auf der Bühne platziert werden, es geht hier nur um den Progammierten Teil.

ActionScript:
  1. class cFormular
  2. {
  3.     //----------------------------------------------
  4.     // Vars
  5.     //----------------------------------------------
  6.     private var ScriptPath:String;
  7.     private var ItemName:Array;
  8.     private var ItemValue:Array;
  9.     private var Enter:Array;
  10.     private var Tab:Array;
  11.     private var InfoText:TextField;
  12.     public var checkEmail:Boolean;
  13.    
  14.     //----------------------------------------------
  15.     // Constructor
  16.     //----------------------------------------------
  17.     public function cFormular()
  18.     {
  19.         if(arguments[0].toString()!=undefined){
  20.             this.ScriptPath = arguments[0].toString();
  21.         } else {
  22.             this.ScriptPath = "formular.php";
  23.         }
  24.         this.ItemName = new Array();
  25.         this.ItemValue = new Array();
  26.         this.Enter = new Array();
  27.         this.Tab = new Array();
  28.         this.checkEmail = true;
  29.     }
  30.    
  31.     //----------------------------------------------
  32.     // Set Path to php script
  33.     //----------------------------------------------
  34.     public function setScriptPath(ScriptPath:String):Void
  35.     {
  36.         this.ScriptPath = ScriptPath;
  37.     }
  38.    
  39.     //----------------------------------------------
  40.     // add new Item
  41.     //----------------------------------------------
  42.     public function addItem(Name:String, Value:TextField, Enter:Boolean, Tab:Number):Void
  43.     {
  44.         this.ItemName.push(Name);
  45.         this.ItemValue.push(Value);
  46.         this.Enter.push(Enter);
  47.         this.Tab.push(Tab);
  48.         this.allocateTab();
  49.     }
  50.    
  51.     //----------------------------------------------
  52.     // allocate Tabindex
  53.     //----------------------------------------------
  54.     private function allocateTab():Void
  55.     {
  56.         for(var i=0;i<this.ItemName.length;i++){
  57.             if(this.Tab[i]!=null){
  58.                 this.ItemValue[i].tabEnabled = true;
  59.                 this.ItemValue[i].tabIndex = this.Tab[i];
  60.                 this.selectItem(1);
  61.             }
  62.         }
  63.     }
  64.    
  65.     //----------------------------------------------
  66.     // Selection.setFocus
  67.     //----------------------------------------------
  68.     public function selectItem(Tab:Number):Void
  69.     {
  70.         for(var i=0;i<this.ItemName.length;i++){
  71.             if(this.Tab[i]!=null){
  72.                 if(this.Tab[i]==Tab) Selection.setFocus(this.ItemValue[i]);
  73.             }
  74.         }
  75.     }
  76.    
  77.     //----------------------------------------------
  78.     // add InfoTextField
  79.     //----------------------------------------------
  80.     public function addInfoTextField(InfoText:TextField):Void
  81.     {
  82.         this.InfoText = InfoText;
  83.     }
  84.    
  85.     //----------------------------------------------
  86.     // send formular
  87.     //----------------------------------------------
  88.     public function send(To:String, Subject:String):Void
  89.     {
  90.         if(!this.check()) return;
  91.         var Send:LoadVars = new LoadVars();
  92.         var Load:LoadVars = new LoadVars();
  93.        
  94.         Send.To = escape(To);
  95.         Send.Subject = escape(Subject);
  96.         if(arguments[2].toString()!=undefined) Send.Caption = escape(arguments[2].toString());
  97.         else Send.Caption = escape(Subject);
  98.        
  99.         for(var i=0;i<this.ItemName.length;i++){
  100.             Send["name"+i] = escape(this.ItemName[i]);
  101.             Send["value"+i] = escape(this.ItemValue[i].text);
  102.         }
  103.         var scope = this;
  104.         Load.onLoad = Delegate.createPacked(this, onLoad, Load);
  105.         Send.sendAndLoad(this.ScriptPath, Load, "_POST");
  106.         if(this.InfoText!=undefined) this.InfoText.text = "Daten werden gesendet!";
  107.     }
  108.    
  109.     //----------------------------------------------
  110.     // check all Items and start error event
  111.     //----------------------------------------------
  112.     private function check():Boolean
  113.     {
  114.         var errorCode:Number = 0;
  115.         var errorEmail:Number = 0;
  116.         for(var i=0;i<this.ItemName.length;i++){
  117.            
  118.             // all fields filled?
  119.             if(this.Enter[i]){
  120.                 if(this.ItemValue[i].text==""){
  121.                     errorCode = 101;
  122.                 }
  123.             }
  124.            
  125.             // email correct?
  126.             if(this.checkEmail){
  127.                 if(this.ItemName[i].toString().toUpperCase()=="EMAIL"){
  128.                     if(this.ItemValue[i].text.indexOf('@')<3||this.ItemValue[i].text.indexOf('.')<6){
  129.                         errorEmail = 102;
  130.                     }
  131.                 }
  132.             }
  133.         }
  134.         if(errorCode!=0){
  135.             this.onError(errorCode);
  136.             return false;
  137.         } else if(errorEmail!=0){
  138.             this.onError(errorEmail);
  139.             return false;
  140.         }
  141.         return true;
  142.     }
  143.  
  144.     //----------------------------------------------
  145.     // onLoad Event
  146.     //----------------------------------------------
  147.     public function clearItems():Void
  148.     {
  149.         for(var i=0;i<this.ItemName.length;i++){
  150.             this.ItemValue[i].text = "";
  151.         }
  152.     }
  153.    
  154.     //----------------------------------------------
  155.     // onLoad Event
  156.     //----------------------------------------------
  157.     public function onLoad(evt):Void
  158.     {
  159.         if(evt.back){
  160.             this.clearItems();
  161.             this.selectItem(1);
  162.             this.InfoText.text = "Daten wurden erfolgreich gesendet";
  163.         } else this.InfoText.text = "Daten konnten nicht gesendet werden";
  164.     }
  165.  
  166.     //----------------------------------------------
  167.     // error event
  168.     //----------------------------------------------
  169.     public function onError(target:Number):Void
  170.     {
  171.         if(this.InfoText!=undefined){
  172.             switch(target){
  173.                 case 101:
  174.                     this.InfoText.text = "Sie haben nicht alle felder Ausgefüllt!";
  175.                 break;
  176.                 case 102:
  177.                     this.InfoText.text = "Sie müssen eine gültige Email eingeben!";
  178.                 break;
  179.             }
  180.         }
  181.     }
  182.    
  183. /*###################################################
  184. # error Code
  185. # 101 = fields not filled
  186. # 102 = invalid email address
  187. ###################################################*/
  188.  
  189. }

PHP-Code:
<?php
    
$num 
= (count($_POST)-3) / 2;

$To utf8_decode(urldecode($_POST['To']));
$Subject utf8_decode(urldecode($_POST['Subject']));
$Message utf8_decode(urldecode($_POST['Caption'])) . "\n";

for(
$i =0;$i<$num;$i++){
    
$name "name".$i;
    
$value "value".$i;
    
$Message .= utf8_decode(urldecode($_POST[$name])) . ": ";
    
$Message .= utf8_decode(urldecode($_POST[$value])) . "\n";
}

if(@
mail($To$Subject$Message)){
    echo 
"&back=true";
} else {
    echo 
"&back=false";
}
?>
Anwendungsbeispiel:
ActionScript:
  1. var Formular:cFormular = new cFormular();
  2. //Optional kann auch
  3. // var Formular:cFormular = new cFormular("PfadZumScript.php");
  4. // oder
  5. // Formular.setScriptPath("PfadZumScript.php");
  6. //gewählt werden
  7.  
  8. Formular.addItem("Nachname", _root.InstanzName.TextFieldName, true, 1);
  9. Formular.addItem("Vorname", _root.InstanzName.TextFieldName, true, 2);
  10. Formular.addItem("Email", _root.InstanzName.TextFieldName, true, 3);
  11. Formular.addItem("Anfrage", _root.InstanzName.TextFieldName, true, 4);
  12.  
  13. // syntax
  14. //addItem(Name:String, Value:TextField, Enter:Boolean, Tab:Number):Void
  15. // Enter steht für Pflichtfeld
  16. // Tab für die Tabulator reihenfolge
  17.  
  18. // Zur anzeige der Info Texte
  19. Formular.addInfoTextField(_root.InstanzName.TextFieldName);
  20.  
  21. _root.Btn_Send.onRelease = function()
  22. {
  23.     Formular.send("To", "Subject", "ÜberschriftDesFormulares");
  24. }
  25.  
  26. _root.Btn_Reset.onRelease = function()
  27. {
  28.     Formular.clearItems();
  29.     Formular.selectItem(1);
  30. }

Seid bitte gnädig das ist meine erste Veröffentlichung

Hoffe es gefällt, hilft, oder inspiriert

Gruß Linowitch
__________________
Gruß Linowitch
Wer glaubt gut zu sein hat aufgehört besser zu werden!

Website
Linowitch ist offline   Mit Zitat antworten
Alt 23-11-2007, 17:05   #2 (permalink)
Alter User
 
Benutzerbild von Linowitch
 
Registriert seit: Nov 2006
Ort: München
Beiträge: 484
Hab noch was vergessen, ohne die Delegate klasse gehts natürlich auch nicht.

Interval in einer klasse mit fnkt -> geltungsbereich ?
__________________
Gruß Linowitch
Wer glaubt gut zu sein hat aufgehört besser zu werden!

Website
Linowitch ist offline   Mit Zitat antworten
Antwort

Lesezeichen

Themen-Optionen
Ansicht

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks sind an
Pingbacks sind an
Refbacks sind an



Alle Zeitangaben in WEZ +1. Es ist jetzt 23:29 Uhr.

Domains, Webhosting & Vserver von Host Europe
Unterstützt das Flashforum!
Adobe User Group


Copyright ©1999 – 2012 Marc Thiele