WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : This script is a contact form between users of a website (kinda like the PM function on the forums)
Categories : PHP, Databases, MySQL, Regexps
bastien koert
Date : Feb 23rd 2005
Grade : 3 of 5 (graded 5 times)
Viewed : 6428
File : No file for this code example.
Images : No Images for this code example.
Search : More code by bastien koert
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

<?php

//select the email address' from your db
//NOTE: This will expose your users email addresses to address harvesters potentially leading to spam
//      A better method is simply to post the user name and then do a look up on the email address
//      but that is a another story


if (isset($_POST['submit']))
{
//submit button pushed call the send_email function
 
send_email();
 
}else{
//nothing has been pushed so show the form
 
show_form();

}
//end if

/**************************************************************************
                  send_mail function
**************************************************************************/
function send_email()
{
   
//default values for elements
   
$subject     = '';
   
$email       = '';
   
$message     = '';
   
$your_name   = '';
   
$your_email  = '';
   
$err_msg     = '';
   
$headers     = '';
   
   
//get the values from the form handle any errors
   
if(isset($_POST['subject']))
    {
     
$subject = $_POST['subject'];
    }
    if(isset(
$_POST['email']))
    {
     
$email = $_POST['email'];
    }
    if(isset(
$_POST['message']))
    {
     
$message = $_POST['message'];
    }
    if(isset(
$_POST['your_name']))
    {
     
$your_name = $_POST['your_name'];
    }
    if(isset(
$_POST['your_email']))
    {
     
$your_email = $_POST['your_email'];
    }
   
   
//validate the emails for correctness
   
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
     
$err_msg .= "Email is not valid. Please re-enter it<br />";
     
$email = '';         
    }
   
//sender's email
   
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $your_email)) {
     
$err_msg .= "Your email is not valid. Please re-enter it<br />";
     
$your_email = '';         
    }
   
   
//check to see if the other elements have values
   
if(empty($message))
    {
     
$err_msg .= "No message set. Please enter a message.<br />";
    }
    if(empty(
$subject))
    {
     
$err_msg .= "No subject set. Please enter a subject.<br />";
    }
    if(empty(
$your_name))
    {
     
$err_msg .= "No sender name set. Please enter a your name.<br />";
    }
   
   
//check the err_msg to see if there are any messages
   
if ($err_msg != ''){
     
//there is an error so build the data into a pipe delimited string and pass it back to the form
     
$data = "$email|$subject|$message|$your_email|$your_name";

     
show_form($data,$err_msg);
      die();
   
    }
//end if
   
    //send the email
    //build the headers
   
$headers .= "MIME-Version: 1.0\n";
   
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
   
$headers .= "X-Priority: 1\n";
   
$headers .= "X-MSMail-Priority: High\n";
   
$headers .= "X-Mailer: PHP\n";
   
$headers .= "From: \"".$your_name."\" <".$your_email.">\n";
   
    if (!
mail($email, $subject, $message, $headers))
    {
    echo
"Email failed!";
    }else{
    echo
"<script language='javascript'>alert('Mail sent');</script>";
    }
//end if
 
}//end function

/**************************************************************************
                  show_form function
**************************************************************************/
function show_form($data='',$msg='')
{
//show the form for the email

//the $data='' and $msg='' constructs allow for no information to be passed to the function

    //set defaults for function
   
$subject     = '';
   
$email       = '';
   
$message     = '';
   
$your_name   = '';
   
$your_email  = '';
   
   
//explode the string passed back from the send_mail function if there is an error
   
if (($data !="" )&&($msg != ""))
    {
       
$elements = explode("|",$data);
       
$email       = $elements[0];
       
$subject     = $elements[1];
       
$message     = $elements[2];
       
$your_email  = $elements[3];
       
$your_name   = $elements[4];
    }
//end if
   
   
    /*
      optional where clauses could include:
        1. only active users
        2. check to see if the user wants to allow contact from others on the site
        3. only new users (signed up within a certain date)
    */
   
$sql = "select email_address from tablename [optional where clause]";
   
   
//your db connection stuff goes here
   
   
$result = mysql_query($sql);
   
    echo
"<html><head><title>Email A Friend</title></head><body>";
    echo
"<table width='100%' cellpadding='0' cellspacing='0'>";
    echo
"<form name='email' action='".$_SERVER['PHP_SELF']."' method='post'>";
    echo
"<tr><td colspan='2' align='center'><h2>Email A Friend</h2></td></tr>";
    echo
"<tr><td colspan='2' align='center'> </td></tr>";
    echo
"<tr><td colspan='2' align='center' style='color:red; font-weight:bold;'>$msg</td></tr>";
   
   
//produce the email drop down
   
if (($result)&&(mysql_num_rows($result)>0)){
     
//produce the drop down list
     
echo "<tr><td width='50%' align='right'>Email Address: </td><td><select name='emails'>"//optionally add MULTIPLE to allow sending to multiple addresses
     
while ($rows = mysql_fetch_array($result)){
        echo
"<option>".$rows['email_address']."</option>";
      }
//end while
     
echo "</select></td></tr>";
    }else{
   
//if there is a problem, have the user manually enter the email address
     
echo "<tr><td colspan='2' align='center' style='color:red; font-weight:bold;'><br />Currently unable to locate email addresses. There maybe a problem with the database.</td></tr>";
      echo
"<tr><td colspan='2'> </td></tr>";
      echo
"<tr><td colspan='2' align='center' style='color:red; font-weight:bold;'>Please enter the email address manually</td></tr>";
      echo
"<tr><td width='40%' align='right'>Email Address: </td><td><input type='text' name='email' size='50' value='$email'></td></tr>";
    }
//end if
     
   
echo "<tr><td align='right'>Subject: </td>                <td><input type='text' name='subject' size='50' value='$subject'></td></tr>";
    echo
"<tr><td align='right' valign='top'>Message: </td>   <td><textarea name='message' cols='25' rows='10'>$message</textarea></td></tr>";
    echo
"<tr><td align='right'>Your name: </td>              <td><input type='text' name='your_name' cols='25' rows='10'  value='$your_name' ></td></tr>";
    echo
"<tr><td align='right'>Your Email: </td>             <td><input type='text' name='your_email' cols='25' rows='10' value='$your_email'></td></tr>";
    echo
"<tr><td colspan='2' align='center'><input type='submit' value='Send Email' name='submit'><input type='reset' value='Reset'></td></tr>";
    echo
"</form></table></body>";

}
//end function

?>



This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server.
Categories : Databases, MySQL, Complete Programs, PHP, Databases
YellowPages Content Grabber (PHP5 +)
Categories : PHP, PHP Classes, Regexps, Databases, MySQL
bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
Mimic ASP's GetString function with PHP
Categories : PHP, Databases, MySQL, Strings
Simple Mini Poll class library (SimPoll)
Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs
Add, Edit /Update & Delete all in one Contact Management Form
Categories : PHP, MySQL, Databases, Beginner Guides
Implementing a Members ONLY area
Categories : PHP, MySQL, Databases, Authentication
This functions makes it easy to use session-variables known from ASP. With one Cookie the array "session" will save and restore from a db-record. In this version MySQL is used but it's should very easy to change
Categories : PHP, Arrays, Cookies, MySQL, Databases
Making a simple Hit-Log using PHP and MySql
Categories : PHP, Log Files, Beginner Guides, Databases, MySQL
complete, simple, working example of a login screen/system using php functions, cookies, and a mysql database for begginers.
Categories : Authentication, Complete Programs, PHP, MySQL, Databases
PHP and MySQL scripting for Muyltiple CheckBoxes
Categories : HTML and PHP, MySQL, Databases, PHP
The simple counter with use MySql and gd.
Categories : MySQL, HTTP, Graphics, PHP, Databases
Storing / Retrieving pictures from database This can be used for banner / ads exchange. very useful for people developing big portal with ads in the site........
Categories : PHP, MySQL, Databases, HTML and PHP
This simple function will take a few arguments and easily set a associative array for each column in a result from a MySQL query
Categories : Databases, PHP, MySQL, Arrays
Simple usersOnline class - keep track of how many users are online on your site
Categories : PHP, PHP Classes, Databases, MySQL