Solved

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

Posted on 2014-02-10
6
358 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
  • 3
  • 2
6 Comments
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
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 42

Accepted Solution

by:
Chris Stanyon earned 400 total points
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 22

Author Comment

by:Dreamboat
Comment Utility
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
Comment Utility
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 42

Expert Comment

by:Chris Stanyon
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

744 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

18 Experts available now in Live!

Get 1:1 Help Now