PDA

View Full Version : Resolved Problem with a form not sending to email. Please help.



Richard Cousins
10-09-2012, 08:23 AM
Hi all,

Im creating a website for the company I work for and I have made a simple contact for that asks for Name, e-mail and then a text box where they can write there message.

Now Ive managed to style the form and it seems to work that when I hit submit it goes to a page but what it doesn't do is a) send an email to the address I have specified and b) it doesnt take the user back to the same page they were on even though I have specified this.

The section with the form is called Drop Us A Line.

Here is the code, there are 4 files:

gdform.php


<?php
$request_method = $_SERVER["REQUEST_METHOD"];
if($request_method == "GET")
{
$query_vars = $_GET;
}
elseif ($request_method == "POST")
{
$query_vars = $_POST;
}

reset($query_vars);
$t = date("U");
$file = $_SERVER['DOCUMENT_ROOT'] . "\ssfm\gdform_" . $t;
$fp = fopen($file,"w");

while (list ($key, $val) = each ($query_vars))
{
fputs($fp,"<GDFORM_VARIABLE NAME=$key START>\r\n");
fputs($fp,"$val\r\n");
fputs($fp,"<GDFORM_VARIABLE NAME=$key END>\r\n");
if ($key == "redirect")
{
$landing_page = $val;
}
}

fclose($fp);

if ($landing_page != "")
{
header("Location: http://".$_SERVER["HTTP_HOST"]."/$landing_page");
}
else
{
header("Location: http://".$_SERVER["HTTP_HOST"]."/");
}
?>


gdform.php.bak

<?php
$request_method = $_SERVER["REQUEST_METHOD"];
if($request_method == "GET")
{
$query_vars = $_GET;
}
elseif ($request_method == "POST")
{
$query_vars = $_POST;
}

reset($query_vars);
$t = date("U");
$file = $_SERVER['DOCUMENT_ROOT'] . "\ssfm\gdform_" . $t;
$fp = fopen($file,"w");

while (list ($key, $val) = each ($query_vars))
{
fputs($fp,"<GDFORM_VARIABLE NAME=$key START>\r\n");
fputs($fp,"$val\r\n");
fputs($fp,"<GDFORM_VARIABLE NAME=$key END>\r\n");
if ($key == "redirect")
{
$landing_page = $val;
}
}

fclose($fp);

if ($landing_page != "")
{
header("Location: http://".$_SERVER["HTTP_HOST"]."/$landing_page");
}
else
{
header("Location: http://".$_SERVER["HTTP_HOST"]."/");
}
?>


sendresults.php


<?php
//--------------------------Set these paramaters--------------------------

// Subject of email sent to you.
$subject = 'Contact form';

// Your email address. This is where the form information will be sent.
$emailadd = 'richard.cc@stormdfx.com';

// Where to redirect after form is processed.
$url = 'http://www.stormdfx.com/stormdfx';

// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '1';

// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{
echo "Name of form element $key cannot be longer than 20 characters";die;
}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>



HTML


<form action="gdform.php" method="post" name="ContactForm">
<fieldset>
<label form="name">Name:</label>
<input type="text" id="name" name="name" placeholder="Enter your full name" />
<label form="email">Email:</label>
<input type="text" id="email" name="email" placeholder="Enter your email address" />
<label form="message">Message:</label>
<textarea id="comments" name="comments" placeholder="What's on your mind?"></textarea>
<input type="submit" name="Submit" value="Submit" />
</fieldset>
</form>

From the about PHP can you help me figure out what is going worng?

Many Thanks,

Rich

traq
10-09-2012, 03:12 PM
I'll look at this this afternoon for you, but my first impression was that it's very ...odd. Did you find this somewhere, or are you trying to write it yourself?

Richard Cousins
10-09-2012, 03:19 PM
I found as a free example to use online. Works fine on my personal portfolio site but not this one for some reason.

traq
10-09-2012, 07:41 PM
well, the form you showed posts to gdform.php (which writes to a log file), not to sendresults.php (which sends email). All the scripts have an unusual approach to what they do, however, so there may be other issues (for example, email headers need to end with newlines [ \r\n ], and yours just end with an empty string).

Is that first page supposed to include the sendresults page?

Richard Cousins
10-10-2012, 10:00 AM
Hi traq. Thanks for looking into this for. Last night I had a hunt around for another form to see if I could get it to work and modify it to change the look. I managed to find one and make it the same style and the one above, it also allows the contact form to send and e-mail to multiple e-mail addresses :-)

I'm will update the thread so show that the problem has been resolved.

Thanks for your help :-)