Jump to content

Form to capture email, ip, date to mysql db and then email


gueland

Recommended Posts

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?

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);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.