Pissed Off At PHP
September 20th, 2005
So, today I wake up all happy and ready for some work. Things are going well, I’ve finished reading my blogs, checked my email, everything is in order for a good day.
Then a friend/client asks for their site to be updated. I make the update only to find that something has killed my upload form on that site. After futzing around with it for well over 4 hours and getting nowhere I make a decision that may bite me in the ass later. An hour after that things are working again.
Fast forward an hour or two when I decide to check up on another form. This time a contact form that was an experiment in functions for me. It took me waaaay to long to figure out how to get it to work the first time only to realize that I had not actually gotten it to work. The form was giving out the proper errors if the required fields were not filled out but it would not send the email if the form was filled out correctly. After another 3 hours of messing around I’m still no better off than I was before.
If you’re interested, here’s the form in PHP:
function checkRequired()
{
// Declare variables for later
$stackKeys = array();
$stackValues = array();
$arrErrors = array();
// Loop through $_POST
// Set up hard variables
foreach ($_POST as $key => $value)
{
// Create an array of the keys and their values
array_push($stackKeys, $key);
array_push($stackValues, $value);
} //end foreach
$stack = array_combine_emulated($stackKeys, $stackValues);
extract($stack, EXTR_PREFIX_SAME, "pre");
// Array of required fields
$arrRequired = explode(',', $required);
// Loop through required fields
// Check them for value
for ($i=0; $i<=count($arrRequired); $i++)
{
if ( $stack[$arrRequired[$i]] == '' )
{
$arrErrors[$i] = $arrRequired[$i];
if (!is_null($arrRequired[$i]))
{
echo "<p class=\"error\">Please note: ".$arrRequired[$i]." is a required field.";
}
}
} // end for arrRequired
}
checkRequired();
$topic = array( "1"=>"An advertising inquiry from Phyneclothing.com",
"2"=>"Someone wants you to know about an event",
"3"=>"Just a message from Phyneclothing.com",
"4"=>"Technical problem found on Phyneclothing.com",
"5"=>"Just a message from Phyneclothing.com",
"6"=>"Email from PhyneClothing.com" );
function is_valid_email($email) {
if(ereg("([[:alnum:]\.\-]+)(\@[[:alnum:]\.\-]+\.+)", $email))
{
return 0;
}
else{
return 1;
}
}
$s_emailvalid = is_valid_email($_POST['email']); // check email
if ($s_emailvalid == 1) {
$errmsg .= " <p class=\"error\">Please enter a valid email address</p>";
$sendmail = false;
}
if ( $sendmail )
{
//Email variables set here
/*--------------------- Send email from form --------------------------*/
foreach ($recipient as $send_to) {
mail ($send_to, $topic[$subject], $text, $headers);
}
}
if( $sendmail ) {
echo "<p>Thank you for contacting us. We will respond to your email as soon as possible.</p>\n";
} else {
echo $errmsg;
}
If you have any thoughts or suggestions, feel free to leave a comment. I’m tired of pulling my hair out for the day and need to take a break.
November 20th, 2005 at 10:01 am
As a small update, this problem has been solved and the answer is posted here:
http://www.screenflicker.com/blog/2005/10/a-better-email-form-with-php/