Problem with HTML form in PHP

software development

#1

I have an HTML form that I would like to output to email via PHP. However, the PHP page does not seem to be picking up the values from the HTML form; thus they do not display in the email or on the response web page. Any idea what’s wrong?

The form is below.

First Name Last Name
Email Address

That form should send data to testform.php:

<?php // get posted data into local variables $email_add = $_POST['email_add'] ; $EmailTo = "computerquery@ijpoole.net"; $Subject = "Testform PHP Test"; $fname = $_POST['fname'] ; $lname = $_POST['lname'] ; $headers = "From: $email_add"; $body = ""; $body .= "You have received an order from:"; $body .= "\n\n"; $body .= $fname; $body .= " "; $body .= $lname; $body .= "\n"; $success = mail($EmailTo, $Subject, $body, "From: <$email_add>"); echo $fname . " " .$lname . "
" ; echo "Your email test completed successfully." ; ?>

What has happened most of the time is that I will get a blank email; even the “from” information (“email_add”) is missing. I’m a newbie to PHP so I’m sure I’m doing something ignorant here. Thanks for your help.


#2

Not sure what caused your issue but I’ve “fixed” your code as an example. You should get in the habit of trying to catch error conditions. Also make sure you sanitize your input! For example you don’t want some to exploit your script by inserting additional headers as part of the email address. I’ve included a function that keeps that from happening and more. $fname and $lname should be sanitized if you ever decide to use them in a message header too.

[code]

body { font-family: Verdana, Arial, Helvetica, sans-serif; } td { font-size: x-small; }

td label {
font-weight: bold;
}

td input[type=text] {
display: block;
margin-left: 0.5em;
width: 90%;
}

table {
width: 460px;
}

First Name: Last Name:
Email Address:
[/code]---------------------------[code]<?php

// Initialize some variables

// To header value
$msg_recipient = "computerquery@ijpoole.net";
// Subject header value
$msg_subject = “Testform PHP Test”;
// Envelope sender
$msg_sender = "computerquery@ijpoole.net";

// Functions used

function validEmail($email) {
/*
Validate an email address.
Provide email address (raw input)
Returns true if the email address has the email
address format and the domain exists.
http://www.linuxjournal.com/article/9585
*/

$isValid = true;
$atIndex = strrpos($email, “@”);
if (is_bool($atIndex) && !$atIndex) {
$isValid = false;
}
else {
$domain = substr($email, $atIndex 1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64) {
// local part length exceeded
$isValid = false;
}
else if ($domainLen < 1 || $domainLen > 255) {
// domain part length exceeded
$isValid = false;
}
else if ($local[0] == ‘.’ || $local[$localLen-1] == ‘.’) {
// local part starts or ends with ‘.’
$isValid = false;
}
else if (preg_match(’/\.\./’, $local)) {
// local part has two consecutive dots
$isValid = false;
}
else if (!preg_match(’/^[A-Za-z0-9\-\.] $/’, $domain)) {
// character not valid in domain part
$isValid = false;
}
else if (preg_match(’/\.\./’, $domain)) {
// domain part has two consecutive dots
$isValid = false;
}
else if
(!preg_match(’/^(\\.|[A-Za-z0-9!#%&`_=\/$’* ?^{}|~.-]) $/’,
str_replace("\\","",$local))) {
// character not valid in local part unless
// local part is quoted
if (!preg_match(’/^"(\\"|[^"]) “$/’,
str_replace(”\\","",$local))) {
$isValid = false;
}
}
if ($isValid && !(checkdnsrr($domain,“MX”) ||
checkdnsrr($domain,“A”))) {
// domain not found in DNS
$isValid = false;
}
}
return $isValid;
}

// Process input

$error = ‘’;

if ($_SERVER[‘REQUEST_METHOD’] === ‘POST’) {
// get posted data into local variables
$email_add = $_POST[‘email_add’];
$fname = $_POST[‘fname’];
$lname = $_POST[‘lname’];
// valid input
if ($fname == ‘’ || $lname == ‘’ || $email_add == ‘’) {
$error = ‘Invalid input: blank field’;
}
if (validEmail($email_add) == false) {
$error = ‘Invalid input: invalid address’;
}
if ($error === ‘’) {
$msg_headers = “From: $email_add\n”;
$params = ‘-f’ . $msg_sender;
$msg_body = “”;
$msg_body .= “You have received an order from:\n”;
$msg_body .= “\n”;
$msg_body .= $fname . " " . $lname . “\n”;
$result =
mail(
$msg_recipient,
$msg_subject,
$msg_body,
$msg_headers,
$params
);
if (!$result) {
$error = ‘Could not send message.’;
}
}
}
else {
$error = ‘You did not submit the form correctly.’;
}

// Generate output

if ($error === ‘’) {
echo
"$fname $lname< /br>" .
“Your email test completed successfully.”;
}
else {
echo
"An error occured:< /br>" .
$error;
}
?>[/code]

Customer since 2000 :cool: openvein.org


#3

Nice code :cool:

Maximum Cash Discount on any plan with MAXCASH

How To Install PHP.INI / ionCube on DreamHost


#4

I apologize for not responding sooner. This was tremendously helpful. My problem now is that I did all of my debugging on my personal site, got things working with your help, but when I moved the code over to the director of the domain where I actually want the form to work, it stopped working and I have no idea why. I have three domains and I thought things would be as simple as copying the pages to the correct domain. There’s nothing directory-specific in the code that I can find. I’m now doing research to figure out what I might have done wrong in the move.