30 Juni 2013

Penggunaan Combo Box ( Insert ) dalam Flash


combobox flashAnda telah mengetahui cara konek ke database untuk membuat item di komponen combobox (baca tutorial ini). Dengan database yang sama kita akan menambahkan data melalui Flash. Tutorial ini untuk pemula, bukan untuk programer ulung atau Flash developer veteran.

Requirement:
- standalone server (XAMPP, LAMP, WAMP, etc) to test this tutorial in local environment.
- Flash CS3, AS3
- Dreamweaver or Notepad to create php file.
Requirement:
- standalone server (XAMPP, LAMP, WAMP, etc) untuk mengujinya di server lokal.
- Flash CS3, AS3
- Dreamweaver or Notepad untuk membuat file .php.
Di tutorial sebelumnya, kamu sudah mesti punya database dengan tabel bernama "kuliner", juga punya "comboxml.php" untuk menghasilkan xml dari database. Tutorial ini menggunakan file "comboxml.php" yang sama.
Buat 4 mc button dan letakkan di stage. Namai "edit_btn", ""submit_btn", "hapus_btn" (to delete), "tambah_btn" (to insert).  Anda punya 3 komponen textArea di stage: "ket", "ket2", "img" (untuk image/swf path) dan "xid" (untuk id data), yang terakhir ini tidak begitu diperlukan. Buat juga "status, satu  dynamic text untuk menampung pesan koneksi, apakah berhasil atau error.






//you can delete this labelling action if you have added labels manually
 
tambah_btn.teks.text="ADD";
 
 submit_btn.teks.text="SAVE";
 
tambah_btn.buttonMode=true;
 
tambah_btn.mouseChildren = false;//set it false if you want to show the mouse hand. default is "true" and no mouse hand shown above button with textfield
 
tambah_btn.addEventListener(MouseEvent.CLICK, tambahData);
 
 
//user is inserting data and dont need others button
 
submit_btn.visible=false;
 
edit_btn.visible=false;
 
hapus_btn.visible=false;
 
 
//"tambahData" means "insertData"
 
//we should remove and unload the loaded image/swf from stage
 
function tambahData(event:MouseEvent):void {
 
 removeChild(loader);
 
 loader.unload();
 
 
//submit button (to saving the data) now visible after "tambah_btn" is clicked
 
tambah_btn.visible=false;
 
 submit_btn.visible=true;
 
 tambah_btn.teks.text="";
 
edit_btn.visible=false;
 
 hapus_btn.visible=false;
 
//clear all textfield
 
xid.text="";
 
 ket.text="";
 
 ket2.text="";
 
 img.text="";
 
 status_txt.text = "Fill in all fields except the id";
 
}
 
</strong>///User have to fill in all forms otherwise flash will throw an error of the empty textfield.
 
// If all fields are good, we send the data to php file. This is the actionscript to do that.
 
//I prefer to create a layer beneath the existing one and named it "as_insert".
 
ket.restrict = "A-Za-z 0-9";
 
ket2.restrict = "A-Za-z 0-9";
 
img.restrict = "A-Za-z 0-9 .\\-/";
 
ket.maxChars=50;
 
ket2.maxChars=500;
 
img.maxChars=50;
 
 
//set the "status" text for error
 
var errorsFormat:TextFormat = new TextFormat();
 
errorsFormat.color = 0xFFFFFF;
 
var variables:URLVariables = new URLVariables();
 
  
 
var varSend:URLRequest = new URLRequest("crud.php");
 
varSend.method = URLRequestMethod.POST;
 
varSend.data = variables;
 
var varLoader:URLLoader = new URLLoader;
 
varLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
 
varLoader.addEventListener(Event.COMPLETE, completeHandler);
 
 
function completeHandler(event:Event):void {
 
 
 //if sending is complete, we clear the form fields
 
 ket.text = "";
 
 ket2.text = "";
 
 img.text = "";
 
 status_txt.text = event.target.data.kode;// optional, only if you want to get what the message from mysql query
 
}
 
 
submit_btn.buttonMode=true;
 
submit_btn.mouseChildren = false;
 
  
 
// submit button has an event listener for the submit button and sending the data
 
submit_btn.addEventListener(MouseEvent.CLICK, ValidateAndSend);
 
function ValidateAndSend(event:MouseEvent):void {
 
 
 //validate the form fields
 
 if (!ket.length) {
 
 status_txt.text = "Enter a name.";
 
 status_txt.setTextFormat(errorsFormat);
 
 } else if (!ket2.length) {
 
 status_txt.text = "Enter description.";
 
 status_txt.setTextFormat(errorsFormat);
 
 } else if (!img.length) {
 
 status_txt.text = "Enter the path.";
 
 status_txt.setTextFormat(errorsFormat);
 
 } else {
 
  
 
 //if validate, send the message
 
 
 
 variables.action = "adding";
 
 variables.name = ket.text;
 
 variables.description = ket2.text;
 
 variables.image = img.text;
 
 // Send the data to the php file
 
 varLoader.load(varSend);
 
 play();//this is important thing. empty the 1st frame in order to refresh the  combobox and showing the change. all assets and scripts are placed in 2nd  frame with stop();
 
 }
 


Variabel varSend akan mengirim data ke  php file: "crud.php" yang akan bertugas menambahkan data ke  database.
Jika Anda mengikuti tutorial ini dengan benar, ketika mengetik  "comboxml.php" di browser address (alamat lengkapnya seperti "http://localhost/your_folder/comboxml.php"), Anda akan melihat hasilnya adalah XML. Berikut ini "crud.php". Ganti username, password, database sesuai yang Anda punya.
 crud.php:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?
 
$server = "localhost";
 
$username = "root";
 
$password = "";
 
$database = "your database";
 
  
 
$nameTable = "kuliner";
 
 
if (!mysql_connect($server, $username, $password)) {
 
 $vartoflash = 'kode=Connection failed&';   
 
 
} elseif (!mysql_select_db($database)) {
 
 $vartoflash = 'kode=Database select failed&';
 
 
} else {
 
 
//if no error, begin the insert
 
  
 
if ($_POST['action'] == "adding") {
 
 
 $insertQ = "INSERT INTO $nameTable (id, nama, `desc`, gambar)VALUES (NULL, '".addslashes($_POST['name'])."', '".addslashes($_POST['description'])."', '".addslashes($_POST['image'])."')";
 
 
//"desc" is a MySQL reserved word, should use the backtick
 
  
 
 if (!mysql_query ($insertQ)) {
 
 $msg = mysql_error();
 
 $vartoflash = 'kode=Query failed&msg='.$msg;
 
 } else {
 
 // inserted record get the original id
 
 $id = mysql_insert_id();
 
 $vartoflash = 'kode=Success&id='.$id.'&';
 
 }
 
 
};// end adding
 
 
};
 
 
echo $vartoflash;//flash will use this output. it is important to check where the problem is when something going wrong
 
?>

0 komentar:

Poskan Komentar

Silahkan TInggalkan Komentar Anda di Blog Saya..
No SPAM and No PORN...
Terimakasih Telah meninggalkan Jejak Anda

Related Posts Plugin for WordPress, Blogger...
Award Pertama
Photobucket
Langganan Artikel Si Boersan :

Enter your email address:

Delivered by FeedBurner

Monggo Di Copy Linknya :
Text Link

Banner Link

Komunitas Blogger Indonesia Komunitas Blogwalking