Solved

Tweak the code to work right. PHP sending HTML form data

Posted on 2014-02-10
6
373 Views
Last Modified: 2014-02-11
I create these forms maybe once every 7 years. I can never figure out what I'm doing wrong.

The form is here:

http://www.lvcotd.org/speaker.htm 

The php is here:
http://www.lvcotd.org/send_form_email.php

You will be helping a good cause! Thanks so much for looking!
phpform.zip
0
Comment
Question by:Dreamboat
[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
  • 2
6 Comments
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39849384
There's so much wrong you would be better of starting again. Firstly the HTML. Your code is out of date - seriously. you are using deprecated tags, you have no valid doctype, your labels are mostly pointing to the telephone field and none of your fields have ID (used for the for attribute of labels)

Now for the PHP! When you submit a form to a script with a POST method, all the values from the form are passed to the script in the $_POST array, using the <input> name as the key. So for example, your First Name field has a name of first_name, which will make it available in the script as $_POST['first_name']. You're not accessing the POST array at all, so no info is available.

You are attempting to set the to andfrom addresses with this:

$_REQUEST['xxx.xxx@gmail.com'];

That is looking for a form field called  xxx.xxx@gmail.com.

Arrays in PHP are set using square brackets, not curly:

$fields['someKey'] = 'some value';

There's too much more to explain in details what's wrong, so give me a minute and I'll show you how to do it properly
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 400 total points
ID: 39849448
Here you go. Have a good look through both files to make sure you understand what's happening. The HTML is valid (important if you want things to work properly), so use it as a base. If you're unsure about anything, ask away :)

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<meta name="author" content="Chris Stanyon">
	<title>Experts Exchange // Q_28361521</title>
	<style type="text/css" media="screen">
		body { font-family: Calibri; font-size: 14px; }
		td { vertical-align: top; }
	</style>
</head>
<body>
	<h1>Speaker Request</h1>
	<p>We have several speaking engagements every month and are always honored to be asked.</p>
	<p>Please complete the information below. All fields are required.</p>

	<form name="contactform" method="post" action="send_email.php">
	<table>
		<tr>
			<td><label for="first_name">First Name</label></td>
			<td><input type="text" name="first_name" id="first_name"></td>
		</tr>	
		<tr>
			<td><label for="last_name">Last Name</label></td>
			<td><input type="text" name="last_name" id="last_name"></td>
		</tr>	
		<tr>
			<td><label for="organisation">Organisation</label></td>
			<td><input type="text" name="organisation" id="organisation"></td>
		</tr>	
		<tr>
			<td><label for="email">Email</label></td>
			<td><input type="text" name="email" id="email"></td>
		</tr>	
		<tr>
			<td><label for="telephone">Telephone</label></td>
			<td><input type="text" name="telephone" id="telephone"></td>
		</tr>	
		<tr>
			<td><label for="date">Date of Event</label></td>
			<td><input type="text" name="date" id="date"></td>
		</tr>	
		<tr>
			<td><label for="time">Time of Event</label></td>
			<td><input type="text" name="time" id="time"></td>
		</tr>	
		<tr>
			<td><label for="description">Comments</label></td>
			<td><textarea name="description" id="description"></textarea></td>
		</tr>	
		<tr>
			<td></td>
			<td><input type="submit" value="Send"></td>
		</tr>
	</table>
	</form>
</body>
</html>

Open in new window

<?php
//do some error checking:
if ( empty($_POST['email']) ):
	die("You have not entered an email, please go back and try again");
elseif ( empty($_POST['first_name']) ):
	die("You have not entered a name, please go back and try again"); 
endif;


//Send the speaker request:
$to = "xxxx.xxxx@gmail.com";
$from = $_POST['email'];
$subject = "Speaker Request";
$headers = "From: $from\n";

$message = <<<EOT
A speaker request has been made for:

First Name: {$_POST['first_name']} 
Last Name: {$_POST['last_name']}
Organisation: {$_POST['organisation']}
Email: {$_POST['email']}
Telephone: {$_POST['telephone']}
Date of Event: {$_POST['date']}
Time of Event: {$_POST['time']}
Comments: {$_POST['description']}
EOT;

$sendMail = mail($to, $subject, $message, $headers);


//Send the confirmation email:
$to = $_POST['email'];
$from = "xxxx.xxxx@gmail.com";
$subject = "Thank you for contacting us";
$headers = "From: $from\n";

$message = <<<EOT
Thank you for contacting us.

Somebody will get back to you as soon as possible, usually within 48 hours.
EOT;

$sendConfirm = mail($to, $subject, $message, $headers);


if( $sendMail ): 
	header( "Location: thankyou.html" ); 
else:
	die("We encountered an error sending your mail, please notify webmaster@LVCOTD.com");
endif;
?>

Open in new window

0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 39849939
If you're new to web technologies, this article can help you get started learning the PHP side of things.  It's not something you can learn overnight and you might want to consider hiring a developer if you have more than a form-to-email task ahead of you.  (As the great fire fighter Red Adair said, "If you think it's expensive to hire a professional, just wait till you hire an amateur!")
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

Here is my teaching example of form-to-email showing how, inter alia, to avoid becoming an open-relay for spam.  You might want to "pretty it up" with a valid doctype and some CSS, but it shows all of the required moving parts.  It's my personal preference with simple things like this to package the PHP action script in the same file with the HTML document.  Helps me keep my work organized!  Please read it over and post back if there is anything you don't understand.

<?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';
$to[] = 'Ray.Paseur@Gmail.com';


// IF THE DATA HAS BEEN POSTED (AT LEAST SOMETHING IN THE EMAIL INPUT CONTROL)
if (!empty($_POST['e']))
{
    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
    $safe_mail = clean_string($_POST['e']);
    $safe_name = clean_string($_POST['n']);
    $safe_fone = clean_string($_POST['t']);
    $save_idea = clean_string($_POST['i']);

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

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

    // PRODUCE THE THANK-YOU PAGE
    echo '<p>THANK YOU</p>' . PHP_EOL;
    die('TASK COMPLETE');
}


// A FORM TO TAKE CLIENT INPUT FOR THIS SCRIPT
$form = <<<ENDFORM
<form method="post">
Please enter your contact information
<br/>Email: <input name="e" />
<br/>Phone: <input name="t" />
<br/>Name:  <input name="n" />
<br/>Ideas? <textarea name="i"></textarea>
<br/><input type="submit" />
</form>
ENDFORM;

echo $form;

Open in new window

HTH, ~Ray
0
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!

 
LVL 22

Author Comment

by:Dreamboat
ID: 39850062
Thanks, guys! I'm at work now, but will be working on this tonight. This is the most difficult thing I'll do on the website, so I'm not hot to understand it, just get it done. You guys are just great.
0
 
LVL 22

Author Closing Comment

by:Dreamboat
ID: 39852021
Chris, thanks. I tried your code, but I'm getting the same error I got with mine:

You have not entered an email, please go back and try again

I *did* enter an email, of course. And I put my own email in place of the xxxx.xxxx in the php.

At this point, I'll just pay someone to do it. I'm going to have a heap of snow to shovel in the next day or two.
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39852027
It'll be something simple you've overlooked. Double check you have an <input> with a name of email:

<input type="text" name="email">

You should have one, and only one of these.

Post your HTML and I'll have a look over it
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Node.js 11 81
Find RGB colors from a screen. 2 41
How to remove  "0":  in a json string 4 46
Load string Array from file 23 42
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

734 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