Solved

PHP Send_contact.php Form

Posted on 2013-01-05
5
392 Views
Last Modified: 2013-01-05
Hi

I have moved webhost and am trying to create a contact form in PHP, i've never programmed in php before, as you can imagine I am struggling a little.

I have created a form and send_contact.php file with the following code, most of this is working however I would like to post two variables from my contact form in the message body.  I current have a single line of code:

$message=$_POST['enquiry'];

Open in new window

this works fine but I also want to include the following line:

$message=$_POST['contactnumber'];

Open in new window

When I post the second line the first line doesn't show up in the message body.  I have the code as the following:
$message=$_POST['enquiry'];
$message=$_POST['contactnumber'];

Open in new window


Ideally I would like the message body to be:

Enquiry Details: [Enquiry]
Contact Details: [Contactnumber]

The parts in the brackets need to be pulled from the form.

Could someone help me get this code correct.

thanks
0
Comment
Question by:bains1000
  • 3
5 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38746595
Buy this book.  Trust me, it will make your life MUCH easier and more productive!
http://www.sitepoint.com/books/phpmysql5/
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 38746598
Here is a teaching example showing the essential moving parts of a form-to-email script.  Please read it over and post back with any questions.  I'll be glad to help you get it going, ~Ray

<?php // RAY_form_to_email.php
error_reporting(E_ALL);


// SEND MAIL FROM A FORM


// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from  = "NoReply@Your.org";
$subj  = "Contact Form";

// THIS IS AN ARRAY OF RECIPIENTS - CHANGE THESE FOR YOUR WORK
$to[]  = "You@Your.org";
$to[]  = "Her@Your.org";
$to[]  = "Him@Your.org";


// IF THE DATA HAS BEEN POSTED
if (!empty($_POST['email']))
{
    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
    $email      = clean_string($_POST["email"]);
    $name       = clean_string($_POST["name"]);
    $telephone  = clean_string($_POST["telephone"]);

    // CONSTRUCT THE MESSAGE THROUGH STRING CONCATENATION
    $content    = NULL;
    $content   .= "You have a New Query From $name" . PHP_EOL . PHP_EOL;
    $content   .= "Tel No: $telephone" . PHP_EOL;
    $content   .= "Email: $email" . PHP_EOL;

    // SEND MAIL TO EACH RECIPIENT
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo "MAIL FAILED FOR $recipient";
        }
        else
        {
            echo "MAIL WORKED FOR $recipient";
        }
    }

    // PRODUCE THE THANK-YOU PAGE
    echo '<p>THANK YOU</p>' . PHP_EOL;
}


// A FORM TO TAKE CLIENT INPUT FOR THIS SCRIPT
$form = <<<ENDFORM
<form method="post">
Please enter your contact information
<br/>Email: <input name="email" />
<br/>Phone: <input name="telephone" />
<br/>Name:  <input name="name" />
<br/><input type="submit" />
</form>
ENDFORM;

echo $form;


// A FUNCTION TO CLEAN UP THE DATA - AVOID BECOMING AN OPEN-RELAY FOR SPAM
function clean_string($str)
{
    // IF MAGIC QUOTES IS ON, WE NEED TO REMOVE SLASHES
    $str = stripslashes($str);

    // REMOVE EXCESS WHITESPACE
    $rgx
    = '#'                // REGEX DELIMITER
    . '\s'               // MATCH THE WHITESPACE CHARACTER(S)
    . '\s+'              // MORE THAN ONE CONTIGUOUS INSTANCE OF WHITESPACE
    . '#'                // REGEX DELIMITER
    ;
    $str = preg_replace($rgx, ' ', $str);

    // REMOVE UNWANTED CHARACTERS
    $rgx
    = '#'                // REGEX DELIMITER
    . '['                // START OF A CHARACTER CLASS
    . '^'                // NEGATION - MATCH NONE OF THE CHARACTERS IN THIS CLASS
    . 'A-Z0-9'           // KEEP LETTERS AND NUMBERS
    . '"'                // KEEP DOUBLE QUOTES
    . "'"                // KEEP SINGLE QUOTES
    . '@&+:?_.,/\-'      // KEEP SOME SPECIAL CHARACTERS (ESCAPED HYPHEN)
    . ' '                // KEEP BLANKS
    . ']'                // END OF THE CHARACTER CLASS
    . '#'                // REGEX DELIMITER
    . 'i'                // CASE-INSENSITIVE
    ;
    $str = preg_replace($rgx, NULL, $str);

    return trim($str);
}

Open in new window

0
 

Author Closing Comment

by:bains1000
ID: 38746626
I was missing the . in the additional lines

thanks for your quick response.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38746630
Thanks for the points and thanks for using EE, ~Ray
0
 
LVL 18

Expert Comment

by:Andrej Pirman
ID: 38746633
You are probably asking how to combine those two values into $message.

If you do this, then $message will always have the LAST value only:
$message=$_POST['enquiry'];
$message=$_POST['contactnumber'];

Open in new window

But if you want to add 2nd value to the 1st, then use ".=" instead of "=" only:
$message=$_POST['enquiry'];
$message.=$_POST['contactnumber'];

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now