0

PHP script for sample contact page for website

phpimg

Contact page for website

<?php
include(‘config.inc.php’);

class ContactUs
{
var $arrErrors = array();
var $strSuccess = ”;
public $strSelect=null;
public $objResult=null;
var $objConfig = null;
var $objDbConn=null;
var $intRandSiteLimit = 35;
var $arrRandSites = array();
var $strName = ”;
var $strPhone = null;
var $strEmail = ”;
var $strContent = ”;
var $strReferrer = null;

function __construct()
{
$this->objConfig = new Config();
}

function processReq()
{
$blnMagicQuotesOn = get_magic_quotes_gpc();
if (isset($_REQUEST[‘contact_name’])) {
$this->strName = trim($_REQUEST[‘contact_name’]);
if ($blnMagicQuotesOn) {
$this->strName = stripslashes($this->strName);
}
}
if (isset($_REQUEST[‘contact_email’])) {
$this->strEmail = trim($_REQUEST[‘contact_email’]);
if ($blnMagicQuotesOn) {
$this->strEmail = stripslashes($this->strEmail);
}
}
if (isset($_REQUEST[‘contact_phn’])) {
$this->strPhone = trim($_REQUEST[‘contact_phn’]);
if ($blnMagicQuotesOn) {
$this->strPhone = stripslashes($this->strPhone);
}
}
if (isset($_REQUEST[‘contact_message’])) {
$this->strContent = trim($_REQUEST[‘contact_message’]);
if ($blnMagicQuotesOn) {
$this->strContent = stripslashes($this->strContent);
}
}
}

function validateReq()
{
$blnSuccess = true;

if ($this->strName == ”) {
$this->arrErrors[‘name’] = ‘Name can\’t be empty’;
$blnSuccess = false;
}
if ($this->strEmail == ”) {
$this->arrErrors[’email’] = ‘Email address can\’t be empty’;
$blnSuccess = false;
}else if (!preg_match(‘/^[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$/i’, $this->strEmail)) {
$this->arrErrors[’email’] = ‘Email address is invalid’;
$blnSuccess = false;
}
if ($this->strContent == ”) {
$this->arrErrors[‘message’] = ‘Message can\’t be empty’;
$blnSuccess = false;
}
return $blnSuccess;
}

function clearFields()
{
$this->strName = ”;
$this->strEmail = ”;
$this->strContent = ”;
$this->strPhone = null;
}

function __destruct()
{
unset($this->objConfig);
}

function closeDbConnection()
{
mysql_close($this->objDbConn);
}

function run()
{
$this->objConfig = new Config();
$this->objDbConn = mysql_connect($this->objConfig->dbHostname, $this->objConfig->dbUsername, $this->objConfig->dbPassword);
mysql_select_db($this->objConfig->dbName);

$strCurrentTime = date(“y-m-d h:i:s”,time());
$strIP = $_SERVER[‘REMOTE_ADDR’];

if(isset($_REQUEST[‘submitted’])) {

$this->processReq();
if ($this->validateReq())
{
$strSelect = “INSERT into `contact_db` (`name`,`phone`,`email`,`message`,`date`,`ip`) VALUES (‘$this->strName’,’$this->strPhone’,’$this->strEmail’,’$this->strContent’,’$strCurrentTime’,’$strIP’)”;
$objResult = mysql_query($strSelect,$this->objDbConn);

if(!mysql_affected_rows()<= 0 )
{
$this->strSuccess=’We have received your message.. Thank you!<br />’;
}
else {
$this->arrErrors[‘failed’]=’Some errors occured. Please try again!’;
}
$this->clearFields();
}
}
else {
}
$this->closeDbConnection();
}
}
$ins = new ContactUs();
$ins->run();
?>
<h3>Contact / Feedback Page</h3>
<?php
if(is_array($ins->arrErrors) && !empty($ins->arrErrors)) {
foreach($ins->arrErrors as $key=>$val) {
echo (“<div style=’color: #FF0000;’>”. $val . “</div>”);
}
} elseif ( $ins->strSuccess != “” ) {
echo (“<div style=’color: #0000FF;’>”. $ins->strSuccess . “</div>”);
}
?>
<form action=”contact.php” method=”post”>
<input type=”hidden” name=”action” value=”post” />
<p><br />
<label for=”contact_name”>Name * <span class=”required”></span></label>
<input type=”text” id=”contact_name” name=”contact_name” value=”<?php echo $ins->strName; ?>” size=”20″ tabindex=”1″/>
</p>

<p>
<label for=”contact_email”>Email * <span class=”required”></span></label>
<input type=”text” id=”contact_email” name=”contact_email” value=”<?php echo $ins->strEmail; ?>” size=”20″ tabindex=”1″/>
</p>
<p>
<label for=”contact_phn”>Phone Number – Optional <span></label>
<input type=”text” id=”contact_phn” name=”contact_phn” value=”<?php echo $ins->strPhone; ?>” size=”20″ tabindex=”1″/>
</p>

Your Message *
<p>
<label for=”contact_message”><span class=”required”></span></label>
<textarea name=”contact_message” id=”contact_message” cols=65 rows=10 tabindex=”2″><?php echo $ins->strContent; ?></textarea>
</p>
<p class=”buttons”>
<input name=”submitted” id=”submitted” value=”true” type=”hidden”>
<button name=”submit” type=”submit” id=”submit” class=”button” tabindex=”5″>Send Email</button>
</p>
</form>
</p>
</div>
<?php
$ins->closeDbConnection();
?>
</body>
</html>

 

Filed in: PHP

Recent Posts

Bookmark and Promote!

Leave a Reply

Submit Comment
© 2017 Expert Shout. All rights reserved. XHTML / CSS Valid.