gueland Posted February 9, 2010 Share Posted February 9, 2010 I have written this page to capture an email address, ip and date that will post to a database and then email, but it does not post Here is the code I have written for the page. <div id="wrap"> <img src="images/top.jpg" id="top"> <div id="piece3"> <form method=post action="index.php" > <input type=hidden name=redirect value="thankyou.php" /> <input type=hidden name=errorredirect value="error.php" /> <script type="text/javascript"> function verifyRequired() { if (document.icpsignup["fields_email"].value == "") { document.icpsignup["fields_email"].focus(); alert("The Email field is required."); return false; } return true; } </script> <input type=text name="fields_email" id="email" title="Enter your email address"> <input name="Submit" type="submit" id="subscribe" value="" /> </form> </div> <div id="alerts"> <ul class="hoverbox"> <li><a href="#"><img src="images/Stock1.jpg"><img src="images/vnda.png" class="preview"></a></li> </ul> <ul class="hoverbox2"> <li><a href="#"><img src="images/Stock2.jpg"><img src="images/pir.png" class="preview"></a></li> </ul> <ul class="hoverbox3"> <li><a href="#"><img src="images/Stock3.jpg"><img src="images/gvbp.png" class="preview"></a></li> </ul> <ul class="hoverbox4"> <li><a href="#"><img src="images/Stock4.jpg"><img src="images/fre.png" class="preview"></a></li> </ul> </div> <div id="footer"> <h1>Grow your portfolio like you never have, subscribe free above</h1> </div> </div> <?php //include the connection file require_once('connection.php'); //save the data on the DB and send the email if(isset($_POST['action']) && $_POST['action'] == 'submitform') { //recieve the variables $email = $_POST['email']; $ip = gethostbyname($_SERVER['REMOTE_ADDR']); //save the data on the DB mysql_select_db($database, $connection); $insert_query = sprintf("INSERT INTO contacts (email, date, ip) VALUES (%s, NOW(), %s)", sanitize($email, "text"), sanitize($ip, "text")); $result = mysql_query($insert_query, $connection) or die(mysql_error()); if($result) { //send the email $to = "[email protected]"; $subject = "New contact from the website"; //headers and subject $headers = "MIME-Version: 1.0rn"; $headers .= "Content-type: text/html; charset=iso-8859-1rn"; $headers .= "From: ".$name." <".$email.">rn"; $body = "New contact "; $body .= "Email: ".$email." "; $body .= "IP: ".$ip." "; mail($to, $subject, $body, $headers); //ok message echo "Your message has been sent"; } } function sanitize($value, $type) { $value = (!get_magic_quotes_gpc()) ? addslashes($value) : $value; switch ($type) { case "text": $value = ($value != "") ? "'" . $value . "'" : "NULL"; break; case "long": case "int": $value = ($value != "") ? intval($value) : "NULL"; break; case "double": $value = ($value != "") ? "'" . doubleval($value) . "'" : "NULL"; break; case "date": $value = ($value != "") ? "'" . $value . "'" : "NULL"; break; } return $value; } ?> Here is the structure of the database. describe contacts; +---------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(100) | NO | | NULL | | | email | varchar(100) | NO | | NULL | | | url | varchar(200) | YES | | NULL | | | comment | text | YES | | NULL | | | date | datetime | NO | | NULL | | | ip | varchar(255) | YES | | NULL | | +---------+--------------+------+-----+---------+----------------+ Can you help? Link to comment https://forums.phpfreaks.com/topic/191441-form-to-capture-email-ip-date-to-mysql-db-and-then-email/ Share on other sites More sharing options...
gueland Posted February 9, 2010 Author Share Posted February 9, 2010 This is the form I was basing it on. from http://www.thetechlabs.com/tutorials/interfaces/a-simple-php-mail-contact-form-with-mysql It does not work on my server either. Link to comment https://forums.phpfreaks.com/topic/191441-form-to-capture-email-ip-date-to-mysql-db-and-then-email/#findComment-1009557 Share on other sites More sharing options...
gueland Posted February 10, 2010 Author Share Posted February 10, 2010 I have changed the coding but still does not work (see below): <?php $usr = "dbuser"; #not real $pwd = "dbpassword"; #not real $db = "dbcontacts"; $host = "localhost"; // connect to database $cid = mysql_connect($host,$usr,$pwd); if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); } ?> <?php // this is processed when the form is submitted // back on to this page (POST METHOD) if ($REQUEST_METHOD=="POST") { // double-up apostrophes $email = $_POST['email']; $ip = gethostbyname($_SERVER['REMOTE_ADDR']); // setup SQL statement $SQL = " INSERT INTO contacts "; $SQL = $SQL . " (email, date, ip) VALUES "; $SQL = $SQL . " ('$email', 'NOW(), '$ip') "; //execute SQL statement $result = mysql_db_query($db,"$SQL",$cid); // check for error if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } if($result) { //send the email $to = "[email protected]"; #not real $subject = "New contact from the website"; //headers and subject $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: <".$email.">\r\n"; $body = "New contact<br />"; $body .= "Email: ".$email."<br />"; $body .= "IP: ".$ip."<br />"; mail($to, $subject, $body, $headers); //ok message echo "Your message has been sent"; } } ?> <div id="wrap"> <img src="images/top.jpg" id="top"> <div id="piece3"> <form method=post action="index.php" > <input type=hidden name=redirect value="thankyou.php" /> <input type=hidden name=errorredirect value="error.php" /> <input type=text name="email" id="email" title="Enter your email address"> <input name="Submit" type="submit" id="subscribe" value="" /> </form> </div> <div id="alerts"> <ul class="hoverbox"> <li><a href="#"><img src="images/Stock1.jpg"><img src="images/vnda.png" class="preview"></a></li> </ul> <ul class="hoverbox2"> <li><a href="#"><img src="images/Stock2.jpg"><img src="images/pir.png" class="preview"></a></li> </ul> <ul class="hoverbox3"> <li><a href="#"><img src="images/Stock3.jpg"><img src="images/gvbp.png" class="preview"></a></li> </ul> <ul class="hoverbox4"> <li><a href="#"><img src="images/Stock4.jpg"><img src="images/fre.png" class="preview"></a></li> </ul> </div> <div id="footer"> <h1>Grow your portfolio like you never have, subscribe free above</h1> </div> </div> <?php mysql_close($cid); ?> Link to comment https://forums.phpfreaks.com/topic/191441-form-to-capture-email-ip-date-to-mysql-db-and-then-email/#findComment-1010190 Share on other sites More sharing options...
gueland Posted February 10, 2010 Author Share Posted February 10, 2010 I am also getting PHP Notice: Undefined variable: REQUEST_METHOD on line 20. Link to comment https://forums.phpfreaks.com/topic/191441-form-to-capture-email-ip-date-to-mysql-db-and-then-email/#findComment-1010194 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.