olly79 Posted May 6, 2009 Share Posted May 6, 2009 Hi all, Just wondering if someone could assist me with the following issue. I have a web form which simply consists of the following: <form action="" class="newsletter"> <strong>Enter your email</strong><br /> to receive our latest news and proposals <div> <inout type="text" value="" /><a href="#"><img alt"" src="images/button_sign_up.gif" /></a> </div> </form> What I wanted to do was send the submitted form to an email address and a database. I cobbled the following together for the database: function add_to_db($data){ $link = mysql_connect("localhost", "username", "password") or die("Could not connect: " . mysql_error()); mysql_select_db("dbname", $link) or die ('Can\'t use this DB : ' . mysql_error()); $values = ''; $flds = ''; foreach ($data as $f => $val){ $values .= (empty($values) ? "'".$val."'" : ",'".$val."'"); $flds .= (empty($flds) ? $f : ','.$f); } $sql = "INSERT INTO contact ({$flds}) VALUES({$values})"; $result = mysql_query($sql) or die("Invalid query: " . mysql_error()); mysql_close($link); } However, I'm not sure of where or how to add the email function for this so it send the submitted form to an email account as well as the database. Any help much appreciated. Quote Link to comment Share on other sites More sharing options...
premiso Posted May 6, 2009 Share Posted May 6, 2009 mail If your server allows the user of mail then that should get you where you want on how to email the content. Quote Link to comment Share on other sites More sharing options...
olly79 Posted May 6, 2009 Author Share Posted May 6, 2009 Hi, my issue is how to extend the code so it will also sent the data for email as well as database. Could you possibly assist me? Thanks Quote Link to comment Share on other sites More sharing options...
ignace Posted May 6, 2009 Share Posted May 6, 2009 <?php $GLOBALS['_db_connection']; function _display_error($error) { if (ini_get('display_errors') !== false) { echo $error; } } function db_connect($username, $password, $dbname = null, $host = 'localhost') { global $_db_connection; $_db_connection = mysql_connect($host, $username, $password) or _display_error(mysql_error()); if (null !== $dbname) { mysql_select_db($dbname) or _display_error(mysql_error()); } } function db_query($query) { global $_db_connection; return mysql_query($query, $_db_connection); } function db_close() { global $_db_connection; if ($_db_connection) { mysql_close($_db_connection); } } /** * In here comes all the signup logic */ function newsletter_signup($email) { $sql = "INSERT INTO contact (email_address) VALUES ('$email')"; db_query($sql); mail(..); } ?> Quote Link to comment Share on other sites More sharing options...
olly79 Posted May 6, 2009 Author Share Posted May 6, 2009 Hi, Sorry, I didn't make myself very clear. When someone enters their details into the simple web form that I have I want to send that data to an email address and equally I want to send their submitted email address to a database for storage. So, the PHP needs to firstly send the email address to the database and then send it to a specified email address, this is the part that I'm struggling with. Many thanks Quote Link to comment Share on other sites More sharing options...
premiso Posted May 6, 2009 Share Posted May 6, 2009 Could you possibly assist me? If you look at the PHP Manual for the Mail() function you will see that they give examples of how to send emails with PHP. I would suggest reading up on that and attempting to implement one of the examples that best suit your needs. There are also plenty of Google Resources available for how to send mail with PHP. If you cannot attempt to try it and if you fail provide the code for us to assist you, then perhaps posting this in the freelance section is the way you want to go and offer someone money to code it for you. We are here to help, not write the code for you. At least give it a try as you should have all the resources you need from the manual and or by searching google. Quote Link to comment Share on other sites More sharing options...
olly79 Posted May 6, 2009 Author Share Posted May 6, 2009 Hi, Yes, I agree...I have come up with the following and would appreciate it if you could cast your eyes over it and let me know if I'm correct: <?php function add_to_db($data){ $link = mysql_connect("localhost", "username", "password") or die("Could not connect: " . mysql_error()); mysql_select_db("dbname", $link) or die ('Can\'t use this DB : ' . mysql_error()); $values = ''; $flds = ''; foreach ($data as $f => $val){ $values .= (empty($values) ? "'".$val."'" : ",'".$val."'"); $flds .= (empty($flds) ? $f : ','.$f); } $sql = "INSERT INTO dbname ({$flds}) VALUES({$values})"; $result = mysql_query($sql) or die("Invalid query: " . mysql_error()); mysql_close($link); } empty($_POST['email']) ? $email='' : $email = $_POST['email']; $verif_box = $_REQUEST["verif_box"]; if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){ require_once 'includes/class.ext.mail.php'; $options = array( 'To' => 'name@someemail.co.uk', /* Sets the To email address for the message */ 'ToName' => '', /* Sets the From name of the message */ 'From' => '', /* Sets the From email address for the message */ 'FromName' => '', /* Sets the From name of the message */ 'Subject' => 'Contact form', /* Sets the Subject of the message */ 'Mailer' => 'mail', /* Method to send mail: ("mail", "sendmail", or "smtp") */ 'Host'=> '', /* Sets the SMTP hosts. All hosts must be separated by a semicolon. You can also specify a different port for each host by using this format: [hostname:port] (e.g. "smtp1.example.com:25;smtp2.example.com"). Hosts will be tried in order. */ 'Port' => 25, /* Sets the default SMTP server port */ 'SMTPAuth' => false, /* Sets SMTP authentication. Utilizes the Username and Password variables. */ 'Username' =>'', /* Sets SMTP username. */ 'Password' => '', /* Sets SMTP password. */ ); $fields = array( 'E.mail (E-mail address)' => 'email', 'Messaggio (Message)' => 'message' ); $mail = new ExtMail($options); if(empty($options['From'])) $mail->AddFromAddress($email,$name); $mail->AddAddress($options['To'],$options['ToName']); } ?> Quote Link to comment Share on other sites More sharing options...
premiso Posted May 6, 2009 Share Posted May 6, 2009 Does it work? If it works it is probably correct. Quote Link to comment Share on other sites More sharing options...
olly79 Posted May 7, 2009 Author Share Posted May 7, 2009 Hi, The Form isn't working and I tried amending a few things; however, I'm not sure of the correct process or where I have gone wrong in the code. Could someone please assist me and possibly cast your eyes over what I have done. Many thanks Quote Link to comment Share on other sites More sharing options...
premiso Posted May 7, 2009 Share Posted May 7, 2009 Ok, let's start out by "How" the form is not working. It looks like you pulled in a 3rd party app to utilize this. That is fine, however, I do not know how this form gets submitted, so if you setup your form properly. Might I ask, why did you go for a 3rd party app to handle this instead of just using the mail function? Quote Link to comment Share on other sites More sharing options...
olly79 Posted May 7, 2009 Author Share Posted May 7, 2009 Hi, I'm simply trying to cobble things together as I have never done this before and need to get the form working ASAP. I'm not even sure as to how to process the following webform: <form action="" class="newsletter"> <strong>Enter your email</strong><br /> to receive our latest news and proposals <div> <inout type="text" value="" /><a href="#"><img alt"" src="images/button_sign_up.gif" /></a> </div> </form> I have changed some of the values as per the following: <form action=signup.php"" class="newsletter"> <strong>Enter your email</strong><br /> to receive our latest news and proposals <div> <inout type="submit" value="" /><a href="#"><img alt"" src="images/button_sign_up.gif" /></a> </div> </form> However, as you know the PHP isn't working and I'm not then that the form will work. Would really appreciate some help and how best to proceed. Quote Link to comment Share on other sites More sharing options...
premiso Posted May 7, 2009 Share Posted May 7, 2009 Well your form is not setup properly as you do not have a <input in it. It seems like you may want to read up on some HTML Form Basics. I would recommend looking at one or a few tutorials on PHP Contact Form to help get you rolling. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.