PDA

View Full Version : [Flash & ASP] Example of sendAndLoad();



Digitalosophy
March 6th, 2007, 03:49 PM
This may be usefull for beginners who would like to step into sending and loading data between Flash and ASP. The Flash file simply passes some data to an ASP page which in return passes some data back to Flash. The example show how Flash can then accept that data and do whatever you want with it.

This is a great technique for using master/detail sets, contact forms, querying a database etc.

Flash file:



gatherData = function () {
myData = new LoadVars();
myData.fromFlash = input_txt.text;
myData.toFlash = this.toFlash;
myData.onLoad = function() {
display_txt.html = true;
display_txt.htmlText = myData.toFlash;
};
myData.sendAndLoad("sample.asp", myData, "POST");
};

sendButton.onPress = function() {
gatherData();
};


ASP file:


<%

Dim fromFlash, toFlash, successFlash, failedFlash

fromFlash = Request.Form("fromFlash")

successFlash = "Text was entered.<br>The text that was entered was " & fromFlash
failedFlash = "Text was not entered"

If fromFlash <> "" Then
Response.Write "toFlash="&successFlash
Else
Response.Write "toFlash="&failedFlash
End If

%>
Again this is a simple example, nothing crazy is going on here but you can easily expand this to do whatever you'd like.

Cheers

Kimi-92
March 7th, 2007, 02:17 PM
hey
can i send asp data to example a movieclip?

Digitalosophy
March 7th, 2007, 02:40 PM
Of course, you would want to do this in the onLoad(); function.

crapitoutjim
April 24th, 2007, 11:57 AM
Hi There,

Is this the same principal for sending to php as well?

Thanks

Digitalosophy
April 24th, 2007, 01:01 PM
Yes, Flash stays the same - you would just have to convert the ASP file into a PHP file. I haven't used PHP in sometime so this may be a tad off with the syntax but...



<?php
$fromFlash = $_POST['fromFlash'];
$successFlash = "Text was entered.<br>The text that was entered was " & $fromFlash;
$failedFlash = "Text was not entered";

If ($fromFlash <> ""){
echo "&toFlash=".$successFlash;
}else{
echo "&toFlash=".$failedFlash;
}

?>

project10
May 17th, 2007, 09:01 AM
Hi Digitalosophy

Nice starter, could you give me a hand with some code please, I'm making an asfunction call - 'asfunction:gatherData, 24' and I want to get that '24' (it's going to be dynamic once the thing is finished) in to myData.fromFlash = input_txt.text;

How do I do that?

Then...

The detail page comes back with 6 variables 5 of which I want to display in text boxes, that's easy, got that. But the 6th variable is a URL to an MP3 and I want to put that into a function that calls a loadsound() that will be started with a press of a button. How the heck do I do that?!?

I've been on this for a couple of days now and am sweating please help.

Thanks

Ant

Digitalosophy
May 17th, 2007, 09:18 PM
I'm not sure I fully understand the 24 question.

As far as the loading a sound question just use the sound variable in your sound object, same principals.

You'll need to post what code you have for me to help any further, I don't fully understand either question. :thumb:

project10
May 17th, 2007, 11:43 PM
Well I've got it working, but my problem is I don't really understand how the LoadVars works properly. This is the initial code waiting for the asfunction call from the HTML text box:

function myFunction (newURL) {

myData = new LoadVars();
myData.MainID = newURL;
myData.toFlash = this.toFlash;
myData.onLoad = function(success) {
if (success) {
strName.text = myData.Name;
strVitals.htmlText = myData.Vitals;
strPhoto.htmlText = myData.Photo;
strTrackName.text = myData.TrackName;
strBiog.text = myData.Biog;
MP3URL = myData.TrackURL;
} else {
strName.text = "error";
strVitals.htmlText = "nada";;
strPhoto.htmlText = "nada";
strTrackName.text = "nada";
strBiog.text = "nada";
MP3URL = "nada.mp3";
};
};
Play();
};

Then when it reaches a frame where the text boxes appear there is the LoadVars call:

myData.sendAndLoad("getdetails.asp", myData, "POST");

What I don't understand is why can't I put all the code in the first frame? Is it because the text boxes aren't displayed yet? Do they need to be on the stage (but maybe hidden) at the time of all the code? Do they need to be symbols?

Ant

patologico
May 10th, 2011, 01:48 PM
Despues de una semana, a 8 horas diarias, tras buscar infinitamente sobre el problema de que el archivo .asp no devolvía la variable que le había pasado desde mi flash...
Por fin resolvi el problema.

Trabajando con este tutorial:
http://www.cristalab.com/tutoriales/enviar-correo-electronico-en-flash-usando-loadvars-c7l/

El problema era que cuando indica que tras pasarle las variables al .asp me devolvería las variables que le acababa de pasar (para confirmar que efectivamente le estaban llegando las variables), nunca llegaban los datos o llegaba la variable como "undefined".

La cosa estaba en el documento .asp, cuando uno declaraba logicamente primero las variables:



<%
Dim nombre=Request.Form("nombre")
Dim email=Request.Form("email")
Dim clave=Request.Form("clave")

Response.Write("mensaje=Bienvenido " & nombre & "<br />Entra con tu clave '" & clave & "' :D")
Response.Write("<br />Att: The Cristalab Team")
%>


En realidad, y no se por qué, pero tanteando resulta que el problema estaba en el declarar primero las variables, por eso nunca llegaba la segunda sección de "response.write", sino que se quedaba enganchado en ese error. La cosa es no declarar las variables (DIM) sino directamente agregarles el valor, asi:


<%
nombre=Request.Form("nombre")
email=Request.Form("email")
clave=Request.Form("clave")

Response.Write("mensaje=Bienvenido " & nombre & "<br />Entra con tu clave '" & clave & "' :D")
Response.Write("<br />Att: The Cristalab Team")
%>

Ahora sí, efectivamente mi flash manda las variables "nombre", "email" y "clave", y .asp me las devuelve y las visualizo en mi misma ventana del flash como confirmación.

Bueno, espero que esto le ahorre unas tantas horas a quienes sigan cayendo en este problema. Suerte.