Form -> mySQL, html email, and html output page?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

Form -> mySQL, html email, and html output page?

Post by Sinemacula »

Hi, I'm pretty new at both PHP and mySQL, mostly patching together bits of knowledge from looking at how other scripts are put together, so it won't be surprising to me if I find I haven't done things the best way to start with... anyway, I have the following situation with which I need some guidance:

I have an html form, which users will complete and submit. When they hit "submit" I want the form data added to a mySQL database, and I want the data displayed in a web page, with a particular layout, including tables, graphics, etc... This part I've managed so far by having the form "post" to a php page that includes php code to add the data to the database:

Code: Select all

<?PHP
if ($submit == "Submit Assessment")
	&#123;
	$db = mysql_pconnect("localhost", "xxxx", "yyyy");
	if (!$db) &#123; 
echo( "<p>Unable to connect to the " . 
"database server at this time.</p>" ); 
exit(); 
&#125;
	mysql_select_db("my_database", $db);
	$sql = ("INSERT INTO my_table (Cust_Name,Cust_Co,Cust_Email,Cust_Phone,Referral,
Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12) VALUES ("$Cust_Name","$Cust_Org","$Cust_Email","$Cust_Phone",
"$Referral","$Q1","$Q2","$Q3","$Q4","$Q5","$Q6","$Q7",
"$Q8","$Q9","$Q10","$Q11","$Q12");"); 
	$result = mysql_query($sql, $db);
	&#125;
		
?>
...and uses <? php echo $field;> in the html code that follows to create the html formatted webpage that displays the data (and some other stuff).

These parts are working fine (if not the most elegant solution). However, I would also like to have the same data, also formatted in html, sent to the submitter in an email.

Is there some snippet of code I can add to this existing page that will do this? Do I need to rework the code completely? Or do I need a separate page?

Any guidance you can offer this newbie is greatly appreciated.

Thanks,
Sinemac
crazyjimsmith
Forum Commoner
Posts: 28
Joined: Sat Jan 11, 2003 1:46 pm
Location: Sydney
Contact:

Hello

Post by crazyjimsmith »

Um just add it to the bottom of your script. I did the same with mine.
[/list]
Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

Getting closer...

Post by Sinemacula »

Thanks for your suggestion crazyjim! I am now much closer, I think, to having this work... however, I'm still encountering some challenges, and wasn't able to figure it out based on what I found on the php.net site.

So, here's what I've got at this point:
  • 1. An html form with POST action to a submit.php file.
    2. A submit.php file which connects to the mySQL database and inserts the data submitted, and then also displays a heavily html formatted page with the data by using <?PHP echo $Fieldname; ?> statements.
    3. A mailit.php file which is called from the submit.php file by using an <?PHP include('mailit.php')?> statement. The mailit.php file is coded as follows:

    Code: Select all

    <?php
    if ($submit == "Submit Assessment")
    
    $subject = "Trimergence Self-Assessment Results" ; 
    $message = '<html>... //THIS IS BASICALLY THE SAME HTML THAT IS USED IN THE SUBMIT.PHP FILE TO DISPLAY THE DATA - AS I DO WANT VERY SIMILAR HTML FORMATTING FOR THE EMAIL
    $headers  = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers .= "From: Trimergence Feedback<support@trimergence.com>\n";
    $headers .= "X-Sender: <support@trimergence.com>\n"; 
    $headers .= "X-Mailer: PHP\n"; //mailer 
    $headers .= "X-Priority: 2\n"; //1 UrgentMessage, 3 Normal 
    $headers .= "Return-Path: <support@trimergence.com>\n"; 
    $headers .= "bcc: scott@trimergence.com"; // BCCs to, separete multiple with 
    $toaddress = $Cust_Email;
    mail($toaddress, $subject, $message, $headers); 
    ?>
So, the problem now is that the data doesn't show up in the email... the <?PHP echo $Fieldname; ?> is just left blank in the email. I've tried several separate things to fix this... the most recent was to break up the html coding like this

Code: Select all

$message = '<html> etc., etc...'; 
$message .=<?PHP echo $Fieldname; ?>;
$message .='more html stuff... etc. etc.';
$message . =<?PHP echo $Fieldname2; ?>;
etc. etc. etc.
and this:

Code: Select all

$message = '<html> etc., etc...'; 
$message .='<?PHP echo $Fieldname; ?>';
$message .='more html stuff... etc. etc.';
$message . ='<?PHP echo $Fieldname2; ?>';
etc. etc. etc.
...to name just two... but neither of those worked either... in fact the html email was blank after the end of the first html section.

Any ideas? Or better ways of doing this?

Thanks,
Scott
Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

I think I've got it...

Post by Sinemacula »

After a bunch more messing about, and more digging around php.net, I finally got it to work... although I'm not 100% sure which of the changes I made did it :? -- I think part of it had to do with needing some "\n"... but I don't know for sure if that was the main thing.

Anyway, thanks for your help... it's now working as I was wanting.

Scott
Post Reply