PHP Form Email Results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
trailerparkboy
Forum Newbie
Posts: 4
Joined: Tue Aug 16, 2011 11:10 am

PHP Form Email Results

Post by trailerparkboy »

Hello, new to php and trying to get a form working with checkbox items and email me the results.

I am using the script below, so instead of just displaying the user's selections, want to get them emailed to me.

Code: Select all

<?php

	if(isset($_POST['formSubmit'])) 
    {
		$aDoor = $_POST['formDoor'];
		
		if(isset($_POST['formWheelchair'])) 
        {
			echo("<p>You DO need wheelchair access.</p>\n");
		} 
        else 
        {
			echo("<p>You do NOT need wheelchair access.</p>\n");
		}
		
		if(empty($aDoor)) 
        {
			echo("<p>You didn't select any buildings.</p>\n");
		} 
        else 
        {
            $N = count($aDoor);

			echo("<p>You selected $N door(s): ");
			for($i=0; $i < $N; $i++)
			{
				echo($aDoor[$i] . " ");
			}
			echo("</p>");
		}
	}
?>

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
	<p>
		Which buildings do you want access to?<br/>
		<input type="checkbox" name="formDoor[]" value="A" />Acorn Building<br />
		<input type="checkbox" name="formDoor[]" value="B" />Brown Hall<br />
		<input type="checkbox" name="formDoor[]" value="C" />Carnegie Complex<br />
		<input type="checkbox" name="formDoor[]" value="D" />Drake Commons<br />
		<input type="checkbox" name="formDoor[]" value="E" />Elliot House
	</p>
	<p>
		Do you need wheelchair access?
		<input type="checkbox" name="formWheelchair" value="Yes" />
	</p>
	<input type="submit" name="formSubmit" value="Submit" />
</form>
I think I need to add some code like follows, but can't seem to get it working (also need to go to thankyou page after submitted). Any suggestions? :oops:
$my_email="(email address where results to be sent)";

mail( "$my_email", "Feedback Form Results",
$aDoor[$i]" );
header( "Location: $thankyou_page" );
}

Thanks!
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: PHP Form Email Results

Post by phphelpme »

Hi this is a simple example of sending an email using php:

Code: Select all

<?php
 $to = "recipient@example.com";
 $subject = "Hi!";
 $body = "Hi,\n\nHow are you?";
 if (mail($to, $subject, $body)) {
   echo("<p>Message successfully sent!</p>");
  } else {
   echo("<p>Message delivery failed...</p>");

Best wishes
trailerparkboy
Forum Newbie
Posts: 4
Joined: Tue Aug 16, 2011 11:10 am

Re: PHP Form Email Results

Post by trailerparkboy »

Hi this is a simple example of sending an email using php:
I can get emails sent with php. Issue I am having is sending the checklist selections within the email. I can't figure out how to display the results within the email that is sent.

Thanks.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP Form Email Results

Post by Celauran »

Code: Select all

<?php

if (isset($_POST['formSubmit']))
{
    $aDoor = $_POST['formDoor'];
    $msgBody = "";
    $email = "you@localhost";

    if (isset($_POST['formWheelchair']))
    {
        $msgBody .= "<p>You DO need wheelchair access.</p>\n";
    }
    else
    {
        $msgBody .= "<p>You do NOT need wheelchair access.</p>\n";
    }

    if (empty($aDoor))
    {
        $msgBody .= "<p>You didn't select any buildings.</p>\n";
    }
    else
    {
        $N = count($aDoor);

        $msgBody .= "<p>You selected $N door(s): ";
        for ($i = 0; $i < $N; $i++)
        {
            $msgBody .= $aDoor[$i] . " ";
        }
        $msgBody .= "</p>";
    }

    if (!empty($msgBody))
    {
        echo $msgBody;
        mail($email, "Form Results", $msgBody);
    }
}
?>
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: PHP Form Email Results

Post by phphelpme »

Sorry, thought you were saying you dont know how to send emails via php from your first post.

Nice job Calauran looks good but dont forget to redirect after email has been sent with:

Code: Select all

echo $msgBody;
        mail($email, "Form Results", $msgBody);

header('Location: http://www.example.com/');

    }
}
?>
Best wishes
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP Form Email Results

Post by Celauran »

I completely forgot about the redirect. Nice catch.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: PHP Form Email Results

Post by phphelpme »

No worries buddy, you have done the same for me also. Easily done by everyone. You did a good job sorting the issue out which was the most important aspect anyway. I just added something tedious to the end... lol

Best wishes
trailerparkboy
Forum Newbie
Posts: 4
Joined: Tue Aug 16, 2011 11:10 am

Re: PHP Form Email Results

Post by trailerparkboy »

You guys are good ! Just have a couple more questions

In the email I receive, says from "Nobody". If I want to display who filled out the form, would I have to capture the user's name as a form field and then add some code to display it in the email?

this doesn't seem to work:

Code: Select all

<input type="text" size="35" name="email_address" value="" maxlength="100" />
.
$customer_name = $_REQUEST['customer_name'] ;
.
then guessing would need something like echo $msgBody;
        mail($email, "Form Results", "From: $customer_name", $msgBody);
Also, is it difficult to add validation for mandatory fields I want to add (ex. "Name:", "Phone: ", etc).

I've come up with this, but again, don't think I'm quite there yet:

Code: Select all

// If the form fields are empty, redirect to the error page.
elseif (empty($customer_name) || empty($phone_number)) {
header( "Location: $error_page" );
}

Thanks for your help! I'll continue to see if I can figure it out on my own, but appreciate your assistance as I'm supposed to have this done already!
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: PHP Form Email Results

Post by phphelpme »

Its rather simple to capture the users name and email but yes you are right, you would have to ask for the details from the user first otherwise you will recieve a message from Mr Nobody. :)

You would have to place into your form the two fields:

Code: Select all

<input type="text" size="35" name="customer_name" value="" maxlength="50" />
<input type="text" size="35" name="customer_email" value="" maxlength="100" />
Then you would have to call that POST:

Code: Select all

$customer_name = $_POST['customer_name'];
$customer_email = $_POST['customer_email'];
Regards your from field you would need to send additional headers:

Code: Select all

$headers = 'From: ' . $customer_email . "\r\n" .
   'Reply-To: '. $email . "\r\n" .
   'X-Mailer: PHP/' . phpversion();
Then when sending the email:

Code: Select all

mail($email, "Form Results", $msgBody, $headers);
Regards your error checking I would not send them to another page but I would redirect them to the same page but set if statements to show error messages near your fields that are not filled in etc:

Code: Select all

$error = $_GET['error'];
$error_page1 = "form_page.php?error=1";
$error_page2 = "form_page.php?error=2";

if ($error =="1") echo "<p style=\"color:red\">Please enter your full name!";
<input type="text" size="35" name="customer_name" value="" maxlength="50" />

if ($error =="2") echo "<p style=\"color:red\">Please enter a valid email address!</p>";
<input type="text" size="35" name="customer_email" value="" maxlength="100" />

Regards your validation code:

Code: Select all

if (empty($customer_name)) {
header( "Location: $error_page1" ); }

elseif (empty($customer_email)) {
header("Location: $error_page2" ); }

else { // Send email here
}
I have rushed this really quickly as I am just running out the door... lol I hope this helps you complete your code.

Dont forget that if they have already filled in some parts of the form you need to call their entries so they dont have to fill in all the form again so you would do that by placing for example:

Code: Select all

if (empty($other_field)) { echo "<input type=\"text\" size=\"35\" name=\"other_field\" value=\"\" maxlength=\"100\" />"; }
else { 
$other_field =$_GET['other_field'];
echo "<input type=\"text\" size=\"35\" name=\"other_field\" value=\"" . $other_field ."\" maxlength=\"100\" />";  }
Then your error message link should be in this case:

Code: Select all

$error_page = "form_page.php?error=1&other_field=" . $other_field;
Then you keep adding your fields to the error link as required. This is probably not the best/fastest way of doing things but again it should give you an idea of what you need to do.

Again I am rushing this so if their are any errors then I appologise. :)

Best wishes
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP Form Email Results

Post by Celauran »

You may also want to consider using JavaScript form validation in addition. That way they can see errors before the form is submitted.
trailerparkboy
Forum Newbie
Posts: 4
Joined: Tue Aug 16, 2011 11:10 am

Re: PHP Form Email Results

Post by trailerparkboy »

You may also want to consider using JavaScript form validation in addition. That way they can see errors before the form is submitted.
Funny you say that. I was just about to reply that I could not figure out the php validation code and where it should be placed. I think would be ideal to use the php validation. I was already looking at javascript solution. Hopefully not many people disable javascript.

UPDATE: Found a different javascript validation that is working. Now, I went to move my files to the root directory (my html page, my associated php mail page, and my .js file script). When I go to my form page now, after I submit the form I get error "The requested URL /send_mailVersion2.php was not found on this server." -- but this is clearly in the root folder with all my html pages. I'm not understanding this, as my html page is not saying my .php file is in a specific folder:

Code: Select all

<form id ="myform" name=form action="send_mailVersion2.php" method="post">
Can someone tell me why I am getting this message now? form works great when I go to my subfolder where I was testing it out first.

Thanks!!
Post Reply