PDA

View Full Version : Flash form not communicating with database



PixelPaul
September 11th, 2008, 05:51 PM
Hello!

For the past few days I've been trying to set up a flash form which will take a visitor's details and then write them to a database. Looking at all the tutorials I thought it was going to be easy, although now a couple of days down the line, I regret thinking that.

Basically I've followed this (http://www.kirupa.com/developer/actionscript/forms_database.htm) tutorial many times and couldn't get it to work. I presumed my server doesn't support asp. How do I check if it does?

Anyway, after this I followed a php to txt file tutorial which I forget the address of now, checking it against this (http://www.kirupa.com/developer/actionscript/flash_php_email.htm) one to see if I was doing it correctly. I know that's a form variables to email tutorial but I changed the email to make it write to a text file. This would take the variables from the form and the php would write it to a txt file. OK. So I couldn't get that to work either. Here's what I've got so far:

A flash form shown online here (http://www.coatesdesign.net/test/).
The flash actionscript is as follows:
In frame1:


formData = new LoadVars();
function checkValues() {
formData.bday1 = bdaybox1.selectedItem.label;
formData.bday2 = bdaybox2.selectedItem.label;
formData.bday3 = bdaybox3.selectedItem.label;
formData.gender = genderRadio.selection.label;
formData.name = name_txt.text;
formData.surname = surname_txt.text;
formData.address1 = address1_txt.text;
formData.address2 = address2_txt.text;
formData.address3 = address3_txt.text;
formData.address4 = address4_txt.text;
formData.postcode = postcode_txt.text;
formData.email = email_txt.text;
formData.mobile = mobile_txt.text;
formData.referer = referer_txt.text;
}

On the submit button:

on (press) {
checkValues ()
formData.sendAndLoad("processForm.php", formData, "POST");
formData.onLoad = function(ok) {
if (ok) {
trace("Data saved");
}
};
}


I've used the flash debugger and the variables are in place and I think this is all working ok.

The php script is as follows:

<?php
//Capture data from $_POST array
$name = $_POST['name'];
$surname = $_POST['surname'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$address3 = $_POST['address3'];
$address4 = $_POST['address4'];
$postcode = $_POST['postcode'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$referer = $_POST['referer'];
$bday1 = $_POST['bday1'];
$bday2 = $_POST['bday2'];
$bday3 = $_POST['bday3'];
$gender = $_POST['gender'];

//Make one big string in a format Flash understand
$toSave ="$name|$surname|$address1|$address2|$address3|$addr ess4|$postcode|$mobile|$email|$referer|$bday1|$bda y2|$bday3|$gender";
//Open a file in write mode
$fp = fopen("database.txt", "w");
if(fwrite($fp, $toSave)) echo "writing=Ok&";
else echo "writing=Error&"
fclose($fp);
?>

I'm no newbie to actionscript but have only ever used php for contact form emails. So I'm guessing that's where my mistake lies. I've also tried setting permissions on my web space so everything is writable and executable and all that with no difference.

I'd REALLY appreciate it if anyone can help as I've been stuck on this for longer than I can remember being stuck on anything!

Let me know if there's anything I've missed out.

Cheers,
Paul

tfg
September 12th, 2008, 06:53 AM
where does "database.txt" live? is it in the same directory as the php script?

i think using fopen in "a+" mode would be better than opening it in "w" mode as "w" mode will overwrite the previous contents.

the web server will need write permissions to the file (but not execute) so depending on which group it is part of you should chmod accordingly.

add an html form to the bottom of your php script for now so you can test the form without having to use flash. then put error_reporting(E_ALL); at the top of your script under the first <?php tag. that will give you more idea as to hy it's falling over.

PixelPaul
September 12th, 2008, 11:18 AM
Thanks for the reply tfg. I appreciate it.

The database.txt is in the same directory as the php script and everything else.

I've changed the fopen to "a+" mode.

I added the error_reporting(E_ALL); to it and created a html form (I wasn't exactly sure how to put it inside the php script file). This worked! It's writing to the txt file. But the php was giving errors, which I've listed at the bottom of this post. I'm guessing this is because I have no html file to redirect to? I'm not sure.

The html form is here (http://www.coatesdesign.net/htmltest/registerform.html), and here's the html code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Wooden Cauliflower</title>
</head><body>
<form name="form1" id="form1" method="post" action="processForm.php">

<table width="557" border="0" align="center" cellpadding="0" cellspacing="0">

<tbody>
<tr class="style6">
<td width="285"><div align="right" class="style15">First Name</div></td>
<td width="20" class="style6">&nbsp;</td>
<td width="280" class="style6">
<input name="name" type="text" class="required validate-alpha" id="name" value="" size="30"> </td>
</tr>
<tr class="style6">
<td><div align="right" class="style15">Last Name</div></td>
<td class="style6">&nbsp;</td>
<td class="style6"><input class="required validate-alpha" name="surname" value="" size="30" type="text"></td>
</tr>
<tr class="style6">
<td><div align="right" class="style15">Address Line 1</div></td>
<td class="style6">&nbsp;</td>
<td class="style6"><input name="address1" type="text" class="required" id="address1" value="" size="30"></td>
</tr>
<tr class="style6">
<td><div align="right" class="style15">Address Line 2</div></td>
<td class="style6">&nbsp;</td>
<td class="style6"><input class="required" name="address2" value="" size="30" type="text"></td>
</tr>
<tr class="style6">
<td><div align="right" class="style15">Address Line 3</div></td>
<td class="style6">&nbsp;</td>
<td class="style6"><input name="address3" size="30" type="text"></td>
</tr>
<tr class="style6">
<td><div align="right" class="style15">Town or City</div></td>
<td class="style6">&nbsp;</td>
<td class="style6"><input class="required" name="address4" value="" size="30" type="text"></td>
</tr>
<tr class="style6">
<td><div align="right" class="style15">Postcode</div></td>
<td class="style6">&nbsp;</td>
<td class="style6"><input class="required" name="postcode" value="" size="30" type="text"></td>
</tr>
<tr class="style6">
<td><div align="right" class="style15">Email Address</div></td>
<td class="style6">&nbsp;</td>
<td class="style6"><input class="required validate-email" name="email" value="" size="30" type="text"></td>
</tr>
<tr class="style6">
<td><div align="right" class="style15">Mobile No</div></td>
<td class="style6">&nbsp;</td>
<td class="style6"><input class="required validate-number" name="mobile" value="" size="30" type="text"></td>
</tr>
<tr class="style6">
<td><div align="right" class="style15">Where did you hear about us?</div></td>
<td class="style6">&nbsp;</td>
<td class="style6"><input name="referer" value="" size="30" type="text"></td>
</tr>
<tr class="style6">
<td><div align="right" class="style15">Gender</div></td>
<td class="style6">&nbsp;</td>
<td class="style6"><span class="style19">Male
<input name="gender" id="radio" value="Male" type="radio">
Female
<input name="gender" id="radio2" value="Female" type="radio">
</span></td>
</tr>
<tr class="style6">
<td><div align="right" class="style15">Date of Birth</div></td>
<td nowrap="nowrap" class="style6">&nbsp;</td>
<td nowrap="nowrap" class="style6">
<select name="bday1" class="required validate-selection">
<option selected="selected"> </option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="bday2" class="required validate-selection">
<option selected="selected"> </option>
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
<option value="4">Apr</option>
<option value="5">May</option>
<option value="6">Jun</option>
<option value="7">Jul</option>
<option value="8">Aug</option>
<option value="9">Sep</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>
<select name="bday3" class="required validate-selection">
<option selected="selected"> </option>
<option value="2005">2005</option>
<option value="2004">2004</option>
<option value="2003">2003</option>
<option value="2002">2002</option>
<option value="2001">2001</option>
<option value="2000">2000</option>
<option value="1999">1999</option>
<option value="1998">1998</option>
<option value="1997">1997</option>
<option value="1996">1996</option>
<option value="1995">1995</option>
<option value="1994">1994</option>
<option value="1993">1993</option>
<option value="1992">1992</option>
<option value="1991">1991</option>
<option value="1990">1990</option>
<option value="1989">1989</option>
<option value="1988">1988</option>
<option value="1987">1987</option>
<option value="1986">1986</option>
<option value="1985">1985</option>
<option value="1984">1984</option>
<option value="1983">1983</option>
<option value="1982">1982</option>
<option value="1981">1981</option>
<option value="1980">1980</option>
<option value="1979">1979</option>
<option value="1978">1978</option>
<option value="1977">1977</option>
<option value="1976">1976</option>
<option value="1975">1975</option>
<option value="1974">1974</option>
<option value="1973">1973</option>
<option value="1972">1972</option>
<option value="1971">1971</option>
<option value="1970">1970</option>
<option value="1969">1969</option>
<option value="1968">1968</option>
<option value="1967">1967</option>
<option value="1966">1966</option>
<option value="1965">1965</option>
<option value="1964">1964</option>
<option value="1963">1963</option>
<option value="1962">1962</option>
<option value="1961">1961</option>
<option value="1960">1960</option>
<option value="1959">1959</option>
<option value="1958">1958</option>
<option value="1957">1957</option>
<option value="1956">1956</option>
<option value="1955">1955</option>
<option value="1954">1954</option>
<option value="1953">1953</option>
<option value="1952">1952</option>
<option value="1951">1951</option>
<option value="1950">1950</option>
<option value="1949">1949</option>
<option value="1948">1948</option>
<option value="1947">1947</option>
<option value="1946">1946</option>
<option value="1945">1945</option>
<option value="1944">1944</option>
<option value="1943">1943</option>
<option value="1942">1942</option>
<option value="1941">1941</option>
<option value="1940">1940</option>
<option value="1939">1939</option>
<option value="1938">1938</option>
<option value="1937">1937</option>
<option value="1936">1936</option>
<option value="1935">1935</option>
<option value="1934">1934</option>
<option value="1933">1933</option>
<option value="1932">1932</option>
<option value="1931">1931</option>
<option value="1930">1930</option>
<option value="1929">1929</option>
<option value="1928">1928</option>
<option value="1927">1927</option>
<option value="1926">1926</option>
<option value="1925">1925</option>
<option value="1924">1924</option>
<option value="1923">1923</option>
<option value="1922">1922</option>
<option value="1921">1921</option>
<option value="1920">1920</option>
<option value="1919">1919</option>
<option value="1918">1918</option>
<option value="1917">1917</option>
<option value="1916">1916</option>
<option value="1915">1915</option>
<option value="1914">1914</option>
<option value="1913">1913</option>
<option value="1912">1912</option>
<option value="1911">1911</option>
<option value="1910">1910</option>
<option value="1909">1909</option>
<option value="1908">1908</option>
<option value="1907">1907</option>
<option value="1906">1906</option>
<option value="1905">1905</option>
</select> </td>
</tr>
</tbody></table>
<p align="center" class="style5">
<input value="Submit" type="submit">
</p>
</form></body></html>

and the php script is now like this:
<?php
error_reporting(E_ALL);
//Capture data from $_POST array
$name = $_POST['name'];
$surname = $_POST['surname'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$address3 = $_POST['address3'];
$address4 = $_POST['address4'];
$postcode = $_POST['postcode'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$referer = $_POST['referer'];
$bday1 = $_POST['bday1'];
$bday2 = $_POST['bday2'];
$bday3 = $_POST['bday3'];
$gender = $_POST['gender'];

//Make one big string in a format Flash understand
$toSave ="$name|$surname|$address1|$address2|$address3|$addr ess4|$postcode|$mobile|$email|$referer|$bday1|$bda y2|$bday3|$gender";
//Open a file in write mode
$fp = fopen("database.txt", "a+");
fwrite($fp, $toSave)
fclose($fp);
?>

THE ERRORS:
It said there was an error on line 24 which is fclose($fp);
So I deleted that to see what would happen and then it said line 17 $gender = $_POST['gender']; was the problem.

Thank you for your help.
Any ideas about the php errors?

tfg
September 12th, 2008, 12:27 PM
add a semi-colon to the command fwrite($fp, $toSave) on line 23. and put fclose($fp); back in on line 24. missing the semi-colon would be the reason why you got that error.

are you getting the other errors before or after submitting the form?

if they disappear after you've submitted the form then don't worry about it. normally on a form script you'd put in an if statement to detect whether or not the form had been submitted before executing the code. in this case though as you'd normally be calling it with flash you don't need to detect if the form has been submitted. thus the code is executing anyway meaning that it's trying to access the $_POST variables which won't exist until after the submission.

if it's writing to the file ok then it's probably quite safe to assume that the php script is working normally in which case you can get rid of the html form and double check the actionscript instead. unfortunately i don't know much about actionscript and can't really help with that.

PixelPaul
September 13th, 2008, 12:34 PM
add a semi-colon to the command fwrite($fp, $toSave) on line 23. and put fclose($fp); back in on line 24. missing the semi-colon would be the reason why you got that error.

It's working fine now! I put the flash interface back in after following the corrections above and it works brilliantly now. Thanks so much tfg, you've been a great help!

tfg
September 15th, 2008, 06:46 AM
glad to be of service :)