Hi Folks,
This is really interesting about flash. It can just read, but never write. We can read XML files in flash as well as data can be read, but we cannot write using flash. Due, to security reason, We cannot write anything into any file on neither the local file system nor the server file system using flash. Flash is not authorized to do so. Same is the case with databases. Flash cannot connect to databases as well. The problem seems to be a very serious one, but isnt. There is a very simple solution to this
Solution
Just use any scripting language like ASP, PHP, ASPX, etc. All you need to do is pass the variables that you wanna save to any scripting page and that scripting page would do the needful of connecting to database, or writing into any file. The action script code and the asp (i used asp as my scripting language) code for doing this is as follows
Flash Action Script Code
import mx.controls.*;
var send_lv:LoadVars = new LoadVars();
// define the component instances on the Stage.
var uname:TextInput;
var email:TextInput;
var phone:TextInput;
var address:TextArea;
var btnReset:Button;
var name_label:Label;
var email_label:Label;
var url_label:Label;
var comments_label:Label;
//set color of labels
name_label.setStyle(“color”, “0xFFFFFF”);
email_label.setStyle(“color”, “0xFFFFFF”);
url_label.setStyle(“color”, “0xFFFFFF”);
comments_label.setStyle(“color”, “0xFFFFFF”);
// set the tabbing order for the components
this.uname.tabIndex = 1;
this.email.tabIndex = 2;
this.phone.tabIndex = 3;
this.address.tabIndex = 4;
this.btnReset.tabIndex = 5;
Selection.setFocus(uname);
// when the clear button is clicked, set the form fields to empty strings.
var clearBtnListener:Object = new Object();
clearBtnListener.click = function(evt:Object) {
uname.text = “”;
email.text = “”;
phone.text = “”;
address.text = “”;
};
this.btnReset.addEventListener(“click”, clearBtnListener);
// when the next button is clicked, send the form values to the server using a LoadVars object.
var nextBtnListner:Object = new Object();
nextBtnListner.click = function(evt:Object) {
// if the name is blank, display an error message using the Alert component.
if (uname.text.length == 0) {
Alert.show(“Please enter your Name.”, “Error”, Alert.OK);
Selection.setFocus(uname);
return false;
}
//if email
if (email.text.length == 0) {
Alert.show(“Please enter your Email.”, “Error”, Alert.OK);
Selection.setFocus(email);
return false;
}
// if phone
if (phone.text.length == 0) {
Alert.show(“Please enter your Contact No.”, “Error”, Alert.OK);
Selection.setFocus(phone);
return false;
}
// make sure the comments_ta TextArea instance isn’t blank.
if (address.text.length == 0) {
Alert.show(“Please enter your Address.”, “Error”, Alert.OK);
Selection.setFocus(address);
return false;
}
// if all the required fields have been filled in, create a LoadVars object instance and populate it.
send_lv.name = uname.text;
send_lv.email = email.text;
send_lv.phone = phone.text;
send_lv.address = address.text;
gotoAndStop(5);
};
this.btnNext.addEventListener(“click”, nextBtnListner);
ASP File code to insert records(data passed by flash) into a database
<%
Dim conn,rs
set conn=server.CreateObject(“ADODB.Connection”)
conn.Open “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” & Server.mappath(“opinionpoll.mdb”)
set rs=server.createobject(“ADODB.recordset”)
rs.open “opinion”,conn,0,2
rs.addnew
rs.Fields(“name”) = request.form(“name”)
rs.Fields(“email”) = request.form(“email”)
rs.Fields(“phone”) = request.form(“phone”)
rs.Fields(“address”) = request.form(“address”)
rs.Fields(“Q1″) = request.form(“q1″)
rs.Fields(“Q2″) = request.form(“q2″)
rs.Fields(“Q4″) = request.form(“q4″)
rs.Fields(“Q5″) = request.form(“q5″)
rs.Fields(“Q3″) = request.form(“q3″)
rs.update
rs.close()
conn.Close
Response.Write(“Done”)
%>
Leave a reply