Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 04-12-2008
Member
 
Join Date: Aug 2008
Posts: 46
PHP Form: Email + MySQL

Here is the complete code for combining the dispatch of form by both email and MySQL.
As I commented in bold text so that you can find at most.

Code:
<? php 
/ / Mysql parameters to replace yours 
define ( 'DB_SERVER', 'localhost'); / / server mysql 
define ( 'DB_SERVER_USERNAME', 'root'); / / username 
define ( 'DB_SERVER_PASSWORD', 'password'); / / password 
define ( 'DB_DATABASE', 'test'); / / database name 

/ / Email settings to be replaced by votes 
define ( 'MAIL_DESTINATAIRE', 'webmaster@6ma.fr'); / / replace your email 
define ( 'MAIL_SUJET', 'Message form example.com'); 

$ msg_erreur = "Error. The following fields must be filled: <br/>"; 
$ msg_ok = "Your application has been taken into account." 
$ message = $ msg_erreur; 

/ / Connect to server mysql 
$ connect = mysql_connect (DB_SERVER, DB_SERVER_USERNAME, 
DB_SERVER_PASSWORD) 
or die ( 'Could not connect:'. mysql_error ()); 
/ / Selection of the database 
mysql_select_db (DB_DATABASE, $ connect); 

/ / Verification fields 
if (empty ($ _POST [ 'Gender'])) 
$ message .= "Your civility <br/>"; 
if (empty ($ _POST [ 'name'])) 
$ message .= "Your name <br/>"; 
if (empty ($ _POST [ 'address'])) 
$ message .= "Your address <br/>"; 
if (empty ($ _POST [ 'Postcode'])) 
$ message .= "Your zip code <br/>"; 
if (empty ($ _POST [ 'city'])) 
$ message .= "Your city <br/>"; 
if (empty ($ _POST [ 'comments'])) 
$ message .= "Your message <br/>"; 

/ / If a field is empty, we display the error message 
if (strlen ($ message)> strlen ($ msg_erreur)) ( 

echo $ message; 

/ / Otherwise it's ok 
Else () 

/ / On slashes and removes unnecessary space 
foreach ($ _POST as $ index => $ value) ( 
$ index = stripslashes (trim ($ value)); 
) 

/ / On list areas of interest online 
Interest = $ _POST [ 'interest']; 
$ sqlinterets =''; 
for ($ i = 0; $ i <count ($ interest); $ i + +) 
( 
$ sqlinterets interest .= $ [$ i]; 
$ sqlinterets .= ','; 
) 


/ / Preparing the mail header 
$ mail_entete = "MIME-Version: 1.0 \ r \ n"; 
$ mail_entete .= "From: ($ _POST [ 'name'])" 
."<{$_ POST [ 'email'])> \ r \ n "; 
$ mail_entete .= 'Reply-To:'. $ _POST [ 'email']. "\ r \ n"; 
$ mail_entete .= 'Content-Type: text / plain; charset = "iso-8859-1"; 
$ mail_entete .= "\ r \ nContent-Transfer-Encoding: 8bit \ r \ n"; 
$ mail_entete .= 'X-Mailer: PHP /'. phpversion (). "\ r \ n"; 

/ / Preparation of the body of the mail 
$ mail_corps = "Message: Gender $ $ name \ n"; 
$ mail_corps .= "Address: $ address, $ Postcode $ city, $ country \ n"; 
$ mail_corps .= "His main interests: $ sqlinterets \ n \ n \ n"; 
mail_corps .= $ $ comments; 

/ / Sending mail 
$ res_mail = mail (MAIL_DESTINATAIRE, MAIL_SUJET, $ mail_corps, $ mail_entete); 


foreach ($ _POST as $ index => $ value) ( 
/ / Use addslashes () instead of mysql_real_escape_string () 
/ / If you are not on PHP5 or if the function is problematic 
$ index = mysql_real_escape_string (trim ($ value)); 
) 

/ / Insert the data in mysql 
$ sql = "INSERT INTO form VALUES ('', '$ Gender', '$ name', '$ address',' $ Postcode ',' $ city ',' $ country ',' $ sqlinterets', '$ comments ', Now ())"; 
$ res_sql = mysql_query ($ sql); 

/ / If the mail was sent by mail and mysql 
if ($ res_mail & & $ res_sql) ( 
echo $ msg_ok; 
/ / Mysql if returned an error 
) Else if (! $ Res_sql) ( 
echo mysql_error (); 
/ / If the mail was not sent 
) Else if (! $ Res_mail) ( 
echo 'Error. The form has not been sent by email. '; 
) 

) 
>
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "PHP Form: Email + MySQL"
Thread Thread Starter Forum Replies Last Post
How to Send Form Data to Email.?? Beter 2 Burn Out Software Development 5 04-01-2010 08:50 PM
email the web form detail Ekpah Windows Software 3 24-11-2009 11:01 PM
Mysql Error : Can't connect to local mysql server through socket ' var lib mysql mysql.sock' 2 roshan45 Software Development 3 07-11-2009 09:36 PM
How to build a multipage form in php MySQL? Dusto Software Development 3 13-07-2009 11:47 PM
PHP form and sending via email Gyan Guru Guides & Tutorials 3 14-11-2008 12:21 PM


All times are GMT +5.5. The time now is 04:40 PM.