Hi.
I’ve got a form working with PHP. The code I’ve used is as follows.
var sendData_lv:LoadVars = new LoadVars();
var receiveData_lv:LoadVars = new LoadVars();
var formValidated:Boolean;
var errorMessages:Array = new Array();
receiveData_lv.onLoad = function():Void{
trace(this.sent);
if(this.sent == "OK"){
message0_txt.text = "Thank you. Your details have been registered";
}else{
message0_txt.text = this.sent;
}
}
submit_btn.onRelease = function() {
//this clears the error text field if they have been populate previously
clearTextFields();
errorMessages.length = 0; //empty the array so next time the submit button is clicked the array is not already populated
formValidated = checkForm();
if (formValidated) {
//the form is valid and so now we can send details to our PHP file
//populate LoadVars object with the field values
sendData_lv.name = name_txt.text;
sendData_lv.email = email_txt.text;
sendData_lv.number = number_txt.text;
//trace(sendData_lv.email);
//trace("valid");
sendData_lv.sendAndLoad("email.php?ck="+new Date().getTime(), receiveData_lv);
} else {
//populate textfields with the error messages
for (var i = 0; i<errorMessages.length; i++) {
_root["message"+i+"_txt"].text = errorMessages*;
trace(errorMessages*);
}
}
};
function checkForm():Boolean {
//check whether the name field is empty
if (name_txt.text == "") {
errorMessages.push("Please enter your name.");
}
if (email_txt.text == "") {
errorMessages.push("Please enter your email address.");
} else if (email_txt.text.indexOf("@") == -1 || email_txt.text.indexOf(".") == -1) {
errorMessages.push("Please enter a valid email address.");
}
if (number_txt.text == "") {
errorMessages.push("Please enter your telephone number");
}
//if at this point the array is empty i.e has length 0, then this in effect means the form has passed all checks
if (errorMessages.length == 0) {
return true;
} else {
return false;
}
}
function clearTextFields():Void {
for (var i = 0; i<errorMessages.length; i++) {
_root["message"+i+"_txt"].text = "";
;
}
}
This gives me 3 fields - Name, Email and Phone Number. I also wish to put a checkbox in the form.
I presume I would add something like
sendData_lv.checkBox = checkBox.text;
or something similar.
Basically I want the email I receive back from the server to state whether or not the checkbox was clicked or not (true or false?). Is there anyway of adding this data to the email?
The PHP code I have is as follows
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$subject = 'Callback - Telephone Number entered';
$headers = "From: $name <$email>
";
$headers .= "Reply-To: $name <$email>
";
//ENTER YOUR EMAIL ADDRESS HERE
$to = 'paul@safetyfirstdesign.co.uk';
//---------------------------
$success = mail($to, $subject, $number, $headers);
if($success){
echo '&sent=OK';
}else{
echo '&sent=Error';
}
?>
Thanks for any help.