Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP email issue with Flash MX 2004 2

Status
Not open for further replies.

SteveHigh

Technical User
Jan 17, 2007
158
0
0
GB
Hello

I am very 'new' to PHP in that it has been forced on me by a Flash site that I bought and which insists on PHP.

I have an 'old' site which uses ASP. A site visitor is invited to complete an online form. Having pressed 'Submit', the form fields are stored in an MS Access database, a copy of the visitor's data is sent by email to the visitor himself (his own copy, so to speak), and a copy sent to the Webmaster (this is all done through CDONTS).

My Flash site has a name field, one for e.mail address, another for country, and one for message.

Is there a PHP script which performs the same functions as those I have mentioned above? (it would also need to verify/authenticate email addresses), or would it be easier to work with ASP despite the fact that my Flash vendor works with PHP?

If it helps, I can post my ASP script.

Many thanks.

Steve
 
by all means post the asp script.

but the answer is "yes". this is all easily done in php (apart from verification of email which is somewhat more complex depending on what you need [address is valid - easy, address exists - more difficult]).
 
there's a PHP function for sending email.



The Flash movie doesn't <b>use</b> php as such. The movie itself doesn't do any processing other than passing data to the script. It just so happens that the movie is passing this to a PHP file right?

If you have access to the .fla you could change it to send the data to whatever file you wanted.

Failing that, you need to write a script that will gather the specific data being passed to it. Join it together into a readable message and put that message into the mail() function linked to above.


If you even know a little ASP then you should have no problem doing this using the PHP manual (again, you can get there from the link above).

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.
 
Thank you for your messages.

I will download the links a little later and digest what you have all kindly contributed to.

In the meantime, here is the code I have:

<%
Dim sFullNameTitle,sFullName,sEmailTitle,sEmailAddress,sBusinessTitle,sBusinessName,sCountryTitle,sCountryName,MessageName,MessageTitle
sFullNameTitle = "Fullname"
sFullName = Request.Form("Fullname")

sEmailTitle = "Email"
sEmailAddress = Request.Form("Email")

sBusinessTitle = "Business"
sBusinessName = Request.Form("Business")

sCountryTitle = "Country"
sCountryName = Request.Form("Country")

MessageName = "Message"
MessageTitle = Request.Form("Message")


Dim ObjMail
Set ObjMail = Server.CreateObject("CDONTS.NewMail")
ObjMail.BodyFormat = 0
ObjMail.MailFormat = 0
ObjMail.To = "steve@whatever.com"
ObjMail.CC = sEmailAddress
ObjMail.From = sEmailAddress
ObjMail.Subject = "Feedback"
ObjMail.Body = sFullNameTitle & vbcrlf&_
sFullName & vbcrlf&_
sEmailTitle & vbcrlf&_
sEmailAddress & vbcrlf&_
sBusinessTitle & vbcrlf&_
sBusinessName & vbcrlf&_
sCountryTitle & vbcrlf&_
sCountryName & vbcrlf&_
MessageName & vbcrlf&_
MessageTitle
ObjMail.Send
Set ObjMail = Nothing
%>


Thanks again.

Steve
 
From the information you have you can easily convert that ASP script to PHP.

Here are some hints.

REQUEST.FORM("Business") would translate to $_REQUEST['business'];


In ASP you are joining the bits together using '&' characters. In PHP you would do $something."\r\n".$somethingElse

You would use the mail() function to send the mail in the form

mail(<from>,<to>,<subject>,<body>,<other headers>);

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
Sorry I got that mail() function wrong.


mail ($to,$subject,$message,$additional_headers, $additional_parameters);

in it's simplest form

mail('myaddress@mail.com','Test message','Here is a test message');


--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
this should work provided that you have set up your mail server properly. I would recommend using phpmailer over mail though.

Code:
<?php
$emailFields = array(
'Fullname',
'Email',
'Business',
'Country',
'Message');

//general validation
$isError = false;
$errorMessage = '';

foreach ($emailFields as $field){
	if (empty($_POST[$field])){
		$errorMessage .= "You must supply a value for $field <br/>";
		$isError=true;
	}
	$$field = cleanUp($_POST[$field]);
}

//validate email address
//taken from regularexpressions.info
$pattern = '/^\\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\\b$/i';
if (!preg_match($pattern, $Email)){
	$errorMessage .= "The email address you have entered ($email) is not valid.<br/>";
	$isError = true;
}	

//bailout nicely if errors
if ($isError){
	echo $errorMessage;
	exit();
} else {
	$time = date ('H:i:s');
	$date = date ('j M, Y');
	$message = <<<TXT
Feedback was left on $date at $time
by $Fullname (email: $Email)
Business: $Business in $Country

Message left was 
---
$Message
---
TXT;

	$headers = "From:$Email\r\n";
	$r = @mail('steve@whatever.com', 'Feedback', $message, $headers);
	if ($r){
		echo "mail sent successfully<br/>";
	} else {
		echo "Error sending mail<br/>";
	}
	if (true === ($result = writetoMysql(array($Fullname, $Email, $Business, $Country, $Message)))){
		echo "database write successful";
	} else {
		echo $result;
	}
	exit();
}
function cleanUp($value){
	return trim($value);
}

function writeToMysql($params){
        //fill in these details
	$host = '';
	$db = '';
	$dbUser = '';
	$dbPassword = '';
        $table = '';
	try{
		$pdo = new PDO("mysql:host={$host};dbname={$db}", $dbUser, $dbPassword);
		$stmnt = $pdo->prepare("Insert into $table (Fullname, Email, Business, Country, Message) values (?,?,?,?,?)");
		if (!$stmnt){
			return "Error: " . $pdo->errorInfo() ."<br/>";
		}
		if	($stmnt->execute($params)){
			return true;
		} else {
			return "Error: " . $stmnt->errorInfo() . "<br/>";
		}
	} catch (PDOException $e) {
		return "Error: " . $e->getMessage() . "<br/>";
	}
}
?>
 
These are really useful, detailed and informative replies. My hosting service has informed me that both ASP and PHP are fine.

Rather than try to 'compare' both scripts, I will try to use PHP without reference to any ASP, in the same way that foreign languages are best learnt raw, and not by comparing or referencing them to the language we know.

I can see from jpadie's code that the script checks for empty fields and authenticates email addresses. At the moment, I do not have an userface in Flash for these messages or the others, such as

echo "mail sent successfully<br/>";
} else {
echo "Error sending mail<br/>";

to be displayed, so I will need to create one.

What is of some concern is this:

function writeToMysql($params){

and this:

$pdo = new PDO("mysql:host={$host};dbname={$db}",

while I have this:

'Open MS Access database, store form field values, and close

set conn=Server.CreateObject("ADODB.Connection")

conn.Open "driver={Microsoft Access Driver (*.mdb)};DBQ=D:\business\name.mdb;"
set rs = Server.CreateObject("ADODB.recordset")

SQL="INSERT INTO users (fullname, email, business, country, message) VALUES ('" & _
fullname & "', '" & email & "', '" & business & "', '" & country & "', '" & message & "')"

rs.Open SQL, conn

Set rs=Nothing

conn.Close
Set conn=Nothing
%>

That is, I am not using mySQL, but MS Access. Can I not use MS Access with PHP - excuse my naivete!

Many thanks again - I'm grateful.

Steve






 
sure. the db thing was just by way of example. pdo supports an odbc connection which is probably the easiest way of support MS Access.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top