<?php

//include the connection file
//require_once('connection.php');

$hostname = "mysql.controldomain.com";
$database = "F1077A_PACKEMIN";
$username = "F1077A_craig";
$password = "ck2009";
$table = "cms_members";

// Establish connection with database
$connect = mysql_connect($hostname, $username, $password);
if (!$connect)
  {
  die('Could not connect: ' . mysql_error());
  }

// Select database
$select = mysql_select_db($database, $connect);
if (!$select)
  {
  die('Could not find database: ' . mysql_error());
  }

// Receive the variables
$fname = $HTTP_POST_VARS['fname'];
$lname = $HTTP_POST_VARS['lname'];
$address = $HTTP_POST_VARS['address'];
$suburb = $HTTP_POST_VARS['suburb'];
$pcode = $HTTP_POST_VARS['pcode'];
$email = $HTTP_POST_VARS['email'];
$phone = $HTTP_POST_VARS['phone'];
$source = $HTTP_POST_VARS['source'];
$age = $HTTP_POST_VARS['age'];

// Declare variables
$date = date("l, d/m/y g:i a");
$date2 = date("d/m/y");

// Insert data to database
$sql = "INSERT INTO cms_members (createdDate, updatedDate, fname, lname, address, suburb, pcode, email, phone, source, age) VALUES ($date, $date, $fname, $lname, $address, $suburb, $pcode, $email, $phone, $source, $age)";

mysql_query($sql);

mysql_close($connect);


// Send mail to admin and new member
$admin = "web@packemin.com.au";
$subject1 = "New Packemin Subscriber";

$messagePack = "You have a new mailing list subscriber:

$date

$fname
$lname
$address
$suburb
$pcode
$email
$phone
$source
$age";

$messageCust = "We have received your request to join the Packemin Productions mailing list.

We look forward to keeping you up to date with our latest show information.

Thank you,

Web Admin @ Packemin Productions
www.packemin.com.au


Note: Packemin Productions values your privacy. If you have received this in error, simply respond to this email, typing 'REMOVE' in the subject line.";


/* PHP form validation: the script checks that the Email field contains a valid email address and the Subject field isn't empty. preg_match performs a regular expression match. It's a very powerful PHP function to validate form fields and other strings - see PHP manual for details. */
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
  echo "<h4>Invalid email address</h4>";
  echo "<p><a href='javascript:history.back(1);'>Back</a></p>";
} elseif ($fname == "") {
  echo "<h4>First Name required</h4>";
  echo "<p><a href='javascript:history.back(1);'>Back</a></p>";
} elseif ($lname == "") {
  echo "<h4>Surname required</h4>";
  echo "<p><a href='javascript:history.back(1);'>Back</a></p>";
} elseif ($suburb == "") {
  echo "<h4>Suburb required</h4>";
  echo "<p><a href='javascript:history.back(1);'>Back</a></p>";
} elseif ($pcode == "") {
  echo "<h4>Postcode required</h4>";
  echo "<p><a href='javascript:history.back(1);'>Back</a></p>";
} elseif ($source == "select") {
  echo "<h4>Please select a source option</h4>";
  echo "<p><a href='javascript:history.back(1);'>Back</a></p>";
} elseif ($age == "select") {
  echo "<h4>Please specify your age bracket</h4>";
  echo "<p><a href='javascript:history.back(1);'>Back</a></p>";
}

/* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
elseif (mail($email,$subject1,$messageCust,"FROM:$admin")) {
  mail($admin,$subject1,$messagePack, "FROM:$email");
  echo "<br><h4>Thank you for using our online form.</h4><p>You will soon receive an email confirming your subscription.</p>";
} else {
  echo "<h4>Sorry, we are unable to send email to $email</h4>";
}

?>