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!
Hello all:
I'm new to PHP. I setup a html form say "form.html" It prompts the user to fill in the fields: first name, email, & comments. Method is set to post. Then when they hit submit it goes to the php form "form.php" In the php form it is supposed to check to make sure all fields were filled in. If a field wasn't filled in that field would be displayed with a comment prompting the user to fill it in.
Anyway, after they fill in that field and hit submit I get the email but the information from the "Comment" section is missing. If they wrote a sentence I would get 1 word. If everything was filled out properly on the html page I would get everything. What can I do to resolve this? I know the info gets from the html to the php page b/c I did a simple echo check for the comment right before the rest of the php code and it all comes out there. After it gets validated its gone =(. Here is all the php code from my php form.
Your data isn't lost, you're just not trying to access it correctly - the method you have is true for older versions of PHP but is deprecated in versions 4.1 and up: Passing Variables through forms (POST) and URLS (GET)
Following on from what Bech has said, see the following code with comments:
<?php
// * first you need to remove any leading or trailing spaces
// from the posted data (otherwise a value of space will be
// seen as a good entry)
// * at the same time you can set the values of the variables
// you will need later on by using 'variable variable'
// notation
foreach ($_POST as $key => $value) {
// note that the double dollar sign is intentional
$$key = trim($value);
}
// instead of just testing for an empty string, use the empty()
// function to make it even clearer as to what you are doing
// (IMHO)
if (empty($FName) || empty($Email) || empty($Comments)) {
// the following is called heredoc format and makes it
// easier to write HTML in PHP, the important thing to
// remember is that there should be no spaces after
// <<<END and no spaces before or after END;
// you definitely need double quotes around your HTML
// attributes, you're going to find you have problems
// retrieving any data with spaces otherwise
echo <<<END
<form name="form" method="post">
<p class="bodymd" align="center">Please make sure all required fields are filled out.<br />
Fill in the ones you missed, they are listed below.</p>
<p class="bodymd" align="center">If you do not want to use a Email address type: <b>NA</b></p>
END;
}
if (empty($FName)) {
echo <<<END
<p class="bodymd">First Name<br /><input type="text" name="FName" size="36" /></p>
END;
} else {
echo <<<END
<input type="hidden" name="FName" value="$FName" />
END;
}
if (empty($Email)) {
echo <<<END
<p class="bodymd">Email (username@domain.com)<br /><input type="text" name="Email" size="36" /></p>
END;
} else {
echo <<<END
<input type="hidden" name="Email" value="$Email" />
END;
}
if (empty($Comments)) {
echo <<<END
<p class="bodymd">Comments or Questions<br /><textarea name="Comments" rows="10" cols="50"></textarea></p>
END;
} else {
echo <<<END
<input type="hidden" name="Comments" value="$Comments" />
END;
}
if (empty($FName) || empty($Email) || empty($Comments)) {
echo <<<END
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Clear Form">
</form>
END;
} else {
// you need to include a test here so that you don't try
// and send an e-mail to NA
if ($Email != 'NA') {
$message = "Name: $FName\nEmail: $Email\nComments: $Comments\n";
$extra = "From: $FName\r\nReply-To: $Email\r\n";
mail('someuser@adomain.com', 'Subject something here', $message, $extra);
echo <<<END
<p class="bodymd" align="center">Thanks for your enquiry, $FName.<br />
A response will be sent to $Email as soon as possible.</p>
END;
} else {
echo <<<END
<p class="bodymd" align="center">Thanks for your enquiry, $FName.</p>
END;
}
}
?>
Twigletmac it worked like a charm Thank you very much. Except for the very last part b/c after the "else" the message goes no where. I have an autoresponse setup on my server for emails but maybe I can configure it so if an invalid email like NA comes in it will be ignored. Anyway, I just wanted to thank you for taking the time to help me. I'm slowly starting to understand how to code and hopefully I'll become as good as you.
<?php
A response will be sent to $Email as soon as possible.</p>
END;
} else {
echo <<<END
<p class="bodymd" align="center">Thanks for your enquiry, $FName.</p>
END;
}
}
?>
?>
Oops, I think that was me misreading the code - for some reason my brain decided that you were trying to send an e-mail to the enquirer. I'd delete that if...else statement to make it work as it should do so that this:
if ($Email != 'NA') {
$message = "Name: $FName\nEmail: $Email\nComments: $Comments\n";
$extra = "From: $FName\r\nReply-To: $Email\r\n";
mail('someuser@adomain.com', 'Subject something here', $message, $extra);
echo <<<END
<p class="bodymd" align="center">Thanks for your enquiry, $FName.<br />
A response will be sent to $Email as soon as possible.</p>
END;
} else {
echo <<<END
<p class="bodymd" align="center">Thanks for your enquiry, $FName.</p>
END;
}
$message = "Name: $FName\nEmail: $Email\nComments: $Comments\n";
$extra = "From: $FName\r\nReply-To: $Email\r\n";
mail('someuser@adomain.com', 'Subject something here', $message, $extra);
echo <<<END
<p class="bodymd" align="center">Thanks for your enquiry, $FName.<br />
A response will be sent to $Email as soon as possible.</p>
END;
What I think I'll do to make it easy is use that else statement you have there and just redirect it to another email account so it won't get an autoreply from my server.
<?php
if ($Email != 'NA') {
$message = "Name: $FName\nEmail: $Email\nComments: $Comments\n";
$extra = "From: $FName\r\nReply-To: $Email\r\n";
mail('someuser@adomain.com', 'Subject something here', $message, $extra);
echo <<<END
<p class="bodymd" align="center">Thanks for your enquiry, $FName.<br />
A response will be sent to $Email as soon as possible.</p>
END;
} else {
echo <<<END
<p class="bodymd" align="center">Thanks for your enquiry, $FName.</p>
END;
}
?>
<?php
if ($Email != 'NA') {
$message = "Name: $FName\nEmail: $Email\nComments: $Comments\n";
$extra = "From: $FName\r\nReply-To: $Email\r\n";
mail('someuser@adomain.com', 'Subject something here', $message, $extra);
echo <<<END
<p class="bodymd" align="center">Thanks for your enquiry, $FName.<br />
A response will be sent to $Email as soon as possible.</p>
END;
} else {
$message = "Name: $FName\nEmail: $Email\nComments: $Comments\n";
$extra = "From: $FName\r\nReply-To: $Email\r\n";
mail('DIFFERENT_ADD@adomain.com', 'Subject something here', $message, $extra);
echo <<<END
<p class="bodymd" align="center">Thanks for your enquiry, $FName.</p>
END;
}
?>
mail('DIFFERENT_ADD@adomain.com', ..... should do the trick. This way when the message is sent without an email it is OK b/c \Different_Add@adomain.com won't be setup with an autoresponse message. It didn't come to me until I read what you wrote... I gotta remember Ease of Administration is almost always a good thing Thanks Again!!!