PDA

View Full Version : PHP form to mail problems (moved)


thedon
10-21-2009, 03:06 AM
Hi,
Please see my send file below. I sometimes get a response from my contact form, but 8 out of 10 times it comes back as a blank email.

<?php
//--------------------------Set these paramaters--------------------------
// Subject of email sent to you.
$subject = 'Results from Contact form';
// Your email address. This is where the form information will be sent.
$emailadd = 'admin@thai-discovery.com';
// Where to redirect after form is processed.
$url = 'http://www.thai-discovery.com';
// 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 = '0';
// --------------------------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.'">';
?>

edbr
10-21-2009, 04:19 AM
thats odd. basically either it works or not you tested yoursef using blank fields etc?
try changing $req = '0'; to $req = '1';

thedon
10-21-2009, 04:29 AM
I cant submit blank forms, If I press submit, it says I must put a name and email.
I don't know how I can even get blank results as the contact form does not allow it

edbr
10-21-2009, 06:14 AM
no indeed frankly the code looks fine except for this
f ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
20 characters is very small . delete this section and try again

cdpitt
03-13-2010, 04:28 AM
I have had the same problem. If I fill out the form to test, it works. However, I continue to black results back. I do not know I could get the results completely blank. I thought it was spam, etc.

http://www.barringtontexas.com/contact.htm#form

Any thoughts or insight would help. I understand the basics of dreamweaver, but still learning.

tux
03-13-2010, 05:21 PM
I cant submit blank forms, If I press submit, it says I must put a name and email.
I don't know how I can even get blank results as the contact form does not allow it

Why would you want to submit a blank form??

Edbr is correct, for the form to ensure that the fields are filled in and stop blank emails you have to have $req = '1';, your code currently has it set to $req = '0'; which means it will allow the form to be submitted with the fields empty causing you to get blank emails.

I have tested your code and it works correctly but as Edbr has said, you should maybe increase the maximum fields size as 20 is rather small.

I have had the same problem. If I fill out the form to test, it works. However, I continue to black results back. I do not know I could get the results completely blank. I thought it was spam, etc.

Do you have form validation in your script? Post the code that your form submits to. ( process_form.php )

cdpitt
03-22-2010, 01:42 PM
Sorry for the Delay, here is the code, and the page link.

<h2><a name="form" id="form">Online Form:</a></h2>

Please send any specific information about your request so we can direct

it to the appropriate people:

<form method='post' action='process_form.php'>

<p><input type="hidden" name="contact" value="0" /></p>

<p><strong>What kind of Information can we provide?</strong></p>

<dl>

<dd>

<input type="text" name="INFO__User_Name" value="" />

<input type="radio" name="INFO__MessageType" value="Available Lots" checked="checked" />

Available Lots

<input type="radio" name="INFO__MessageType" value="Builder" />

Builder

<input type="radio" name="INFO__MessageType" value="Community Info" />

Community Info

<input type="radio" name="INFO__MessageType" value="Other" />

Other </dd>

</dl>

<p>&nbsp;</p>

<p><strong>Enter your comments in the space provided below:</strong></p>

<dl>

<dd>

<textarea name="INFO__Comments" rows="5" cols="42"></textarea>

</dd>

</dl>

<p><strong>Tell us how to get in touch with you:</strong></p>

<dl>

<dd>

<table id="table38">

<tr>

<td>Name</td>

<td>

<input type="text" size="35" maxlength="256" name="INFO__Username" /> </td>

</tr>

<tr>

<td>E-mail</td>

<td>

<input type="text" size="35" maxlength="256" name="INFO__UserEmail" /> </td>

</tr>

<tr>

<td>Tel</td>

<td>

<input type="text" size="35" maxlength="256" name="INFO__UserTel" /> </td>

</tr>

<tr>

<td>FAX</td>

<td>

<input type="text" size="35" maxlength="256" name="INFO__UserFax" /> </td>

</tr>

</table>

</dd>

</dl>

<dl>

<dd>

<input type="checkbox" name="ContactRequested" value="ContactRequested" />

Please contact me as soon as possible regarding this matter.</dd>

</dl>

<p><input name="submit" type="submit" value="Submit Comments" /></p>

<p><input name="reset" type="reset" value="Clear Form" /></p>

</form>

http://www.barringtontexas.com/contact.htm

davidj
03-22-2010, 01:59 PM
add code tags please mate

cdpitt
03-23-2010, 04:56 AM
<h2><a name="form" id="form">Online Form:</a></h2>

Please send any specific information about your request so we can direct

it to the appropriate people:

<form method='post' action='process_form.php'>

<p><input type="hidden" name="contact" value="0" /></p>

<p><strong>What kind of Information can we provide?</strong></p>

<dl>

<dd>

<input type="text" name="INFO__User_Name" value="" />

<input type="radio" name="INFO__MessageType" value="Available Lots" checked="checked" />

Available Lots

<input type="radio" name="INFO__MessageType" value="Builder" />

Builder

<input type="radio" name="INFO__MessageType" value="Community Info" />

Community Info

<input type="radio" name="INFO__MessageType" value="Other" />

Other </dd>

</dl>

<p>&nbsp;</p>

<p><strong>Enter your comments in the space provided below:</strong></p>

<dl>

<dd>

<textarea name="INFO__Comments" rows="5" cols="42"></textarea>

</dd>

</dl>

<p><strong>Tell us how to get in touch with you:</strong></p>

<dl>

<dd>

<table id="table38">

<tr>

<td>Name</td>

<td>

<input type="text" size="35" maxlength="256" name="INFO__Username" /> </td>

</tr>

<tr>

<td>E-mail</td>

<td>

<input type="text" size="35" maxlength="256" name="INFO__UserEmail" /> </td>

</tr>

<tr>

<td>Tel</td>

<td>

<input type="text" size="35" maxlength="256" name="INFO__UserTel" /> </td>

</tr>

<tr>

<td>FAX</td>

<td>

<input type="text" size="35" maxlength="256" name="INFO__UserFax" /> </td>

</tr>

</table>

</dd>

</dl>

<dl>

<dd>

<input type="checkbox" name="ContactRequested" value="ContactRequested" />

Please contact me as soon as possible regarding this matter.</dd>

</dl>

<p><input name="submit" type="submit" value="Submit Comments" /></p>

<p><input name="reset" type="reset" value="Clear Form" /></p>

</form>

tux
03-24-2010, 06:54 AM
We will need to see the code from your processing file: process_form.php

cdpitt
03-25-2010, 12:37 AM
Tux: Here is the code, be gentle I am very basic in my knowledge.


<?php

/* INSTRUCTIONS
**************
* 1) Change the 'emailTo' variable (below) from "you@yourdomain.com (Your Name)" to your own email and your own name
* 2) Change the 'emailFrom' variable (below) from "yourdomain.com" to your own domain (no www required)
* 3) a) Change the 'redirect' variable (below) from "www.yourdomain.com" to your own domain.
* b) Make sure you have a "thank_you.htm" in the root folder of your website
* 4) Make sure that each of your for elements on the page that submits to this script are named
* ike the following: <input type=text name="INFO__User_Name">
* NOTE: You cannot have spaces in the name of the for element.
* ie: GOOD element name "INFO__User_Name"
* BAD element name "INFO__User Name"
* 5) Set the "action" of the form to "process_form.php" in the form page that submits to this script
* Example: <form method='post' action='process_form.php'>
* 6) Submit form to make sure all required information gets emailed to you. If not, then go through steps 4 and 5 again.
*/

// CONFIGURATION SETTINGS
$emailTo = "info@barringtonkingwood.com (Your Name)";
$emailFrom = "noreply@barringtontexas.com (Your Domain Form Results)";
$subject = "Web Form Results";
$redirectURL = "http://www.barringtontexas.com/contact_confirmation.htm";


// PLACE ALL FIELDS PASSED IN FROM FORM INTO MESSAGE BODY
foreach ($_POST as $key => $value) {
$keyInfo = explode("__", $key);

if ($keyInfo[0] == "INFO") {
$message .= $keyInfo[1].":\n ".$value."\n\n";
}
}

// SEND EMAIL
mail($emailTo, $subject, $message, "From: ".$emailFrom);

// REDIRECT TO SPECIFIED THANK YOU PAGE
header("Location: $redirectURL");

?>

tux
03-25-2010, 06:40 AM
I tested this, sending it to myself and it worked fine. What problem are you experiencing?

cdpitt
03-25-2010, 01:46 PM
It works fine, however, I keep getting blank forms back. Not when I, or other individuals try to submit, but it seems like spam. I can't even get it to send blank forms, but someone can. It happens about twice a day. I am wondering if I am lacking some sort of security???

davidj
03-25-2010, 02:11 PM
always best practice to have a CAPTCHA on your form

http://www.captcha.net/