Solved

PHP Send_contact.php Form

Posted on 2013-01-05
5
398 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 110

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 110

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 110

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Ajax and PHP 9 53
if(empty) validation fires when trying to select 0 from select list 21 60
PHP encrypted string and passing to a ASP Page 12 49
php subtract from date 2 29
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

726 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