I need help inserting to a database then emailing that information. I am running into a PHP Notice: Undefined variable: REQUEST_METHOD on line 20 error also. It will not post to the database. It is on CentOS 5.4, with PHP 5.2.11, MySQL 5.0.86 and Apache 2.2.14.
Here is the script so far:
<?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="http://p2p.wrox.com/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="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="http://p2p.wrox.com/images/Stock1.jpg"><img src="http://p2p.wrox.com/images/vnda.png" class="preview"></a></li>
</ul>
<ul class="hoverbox2">
<li><a href="#"><img src="http://p2p.wrox.com/images/Stock2.jpg"><img src="http://p2p.wrox.com/images/pir.png" class="preview"></a></li>
</ul>
<ul class="hoverbox3">
<li><a href="#"><img src="http://p2p.wrox.com/images/Stock3.jpg"><img src="http://p2p.wrox.com/images/gvbp.png" class="preview"></a></li>
</ul>
<ul class="hoverbox4">
<li><a href="#"><img src="http://p2p.wrox.com/images/Stock4.jpg"><img src="http://p2p.wrox.com/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);
?>
Can anybody help?