Meads Designer Forum
September 07, 2010, 04:45:39 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Make a Small Donation of 5€ and get Full Access to our Forum.
 
   Home   Help Search Calendar Login Register  
Pages: [1]
  Print  
Author Topic: PHP and Pear - form example  (Read 593 times)
amerloc
Administrator
Full Member
*****
Posts: 140


View Profile
« on: November 26, 2009, 12:59:32 AM »

I have been learning PEAR and how to use some of its functions to send form data via the Mail.php in PEAR. To try this you will need to insure you have PEAR installed in your server and know the exact location. If you have your php.ini in the root of your site then it will be easy.

Example:  include("../usr/bin/pear/Mail.php"); here is the exact address to the pear file if you php.ini file is located in the server root

Example:  include("Mail.php");  here is the exact address to the pear file if you php.ini file is located in the site root

Quote
I am also including a method to save data into a database since this really makes the form data more usable later.

First you create your Form: (save as form.php or include in your webpage)

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Pear mailer example</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="processor.php">
  <label>Name<br />
  <input type="text" name="name" />
  </label>
  <p>
    <label>Email<br />
    <input type="text" name="email" />
    </label>
  </p>
  <p>
    <label>Subject<br />
    <input type="text" name="textfield" />
    </label>
  </p>
  <p>
    <label>Message<br />
    <textarea name="message" cols="30" rows="5"></textarea>
    </label>
  </p>
</form>
</body>
</html>


Second you need to create a config.php file to connect to your DB:

Code:
<?php
 $db_host 
"your host";$db_user "user";$db_pass "pass";$db_name "db name";$db_table "table";
?>

Finally we are going to create the guts:(save this as processor.php)

Code:
<?php

$where_form_is
="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));

include(
"config.php");///DB config file
$link mysql_connect($db_host,$db_user,$db_pass);
if(!
$link) die ('Could not connect to database: '.mysql_error());
mysql_select_db($db_name,$link);
//saving all to database before sending mail
$query "INSERT into `".$db_table."` (name,email,subject,message) VALUES ('" $_POST['name'] . "','" $_POST['email'] . "','" $_POST['subject'] . "','" $_POST['message'] . "')";
mysql_query($query);
mysql_close($link);

include (
"Mail.php");//../usr/bin/pear/ here is the exact address to the pear file

///////////////////pear smtp include data///////////////
$from "From: <info@yoursite.com>";
$to "Your Title here <info@yoursite.com>";
$subject "your subject here";
$body "Form data:
Name: " 
.$_POST['name']."
email: "
.$_POST['email']."
subject: "
.$_POST['subject']."
message: " 
.$_POST['message']."<br>your Company moto";

//this you can actually create a seperate file for and use the require_once statement 
$host "mail.yoursite.com";/// ex: mail.yoursite.com
$username "user id";
$password "password";
/////Sending everything through the Pear functions
$headers = array ('From' => $from,
  
'To' => $to,
  
'Subject' => $subject);
$smtp Mail::factory('smtp',
  array (
'host' => $host,
    
'auth' => true,
    
'username' => $username,
    
'password' => $password));

$mail $smtp->send($to$headers$body);

if (
PEAR::isError($mail)) {
  echo(
"<p>" $mail->getMessage() . "</p>");
 } else {
  echo(
"<p>Message successfully sent!</p>");
 }
include(
"confirm.php");
?>



Now we create the confirm.php for redirection to page if everything went successfully:

Code:
<?php
echo 
"<script type=\"text/javascript\">";
echo "window.location=\"http://yoursite.com/your_page.html\";</script>";

?>


Quote
Notice I used java here since the header redirection function will not work.
« Last Edit: November 26, 2009, 01:08:21 AM by amerloc » Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP

Forum hosted by Meads Company, Saint Barthelemy, French West Indies


Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC
Valid XHTML 1.0! Valid CSS!