Solved

PHP Send_contact.php Form

Posted on 2013-01-05
5
387 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

747 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

12 Experts available now in Live!

Get 1:1 Help Now