PDA

View Full Version : Sendresults.php


thecheekymonkey
07-23-2011, 10:26 AM
Hi first of all ive done a lot of searching and reading and havent found a solution to this problems, i am far from fluent in web design of php etc etc

my problem is this.

i have a template i have downloaded and edited nicely for my site.
there is a contact form on the end, so i did some googling and found this guide

http://www.dreamweaverclub.com/dreamweaver-php-form.php

i have followed it and got the contact form working, however i receive emails no problems, but they are always blank, that is to say it contains my custom heading and sends it to the right address, but in the email i get

Results from form:

but nothing else, no email address, name or message.

here is my dreamweaver form code (bear with me if its wrong lol.


<article class="col1 pad_left1">
<h2>Contact Form</h2>
<form action="http://www.xxxx.co.uk/sendresults.php" method="post" id="ContactForm">
<div>
<div class="wrapper"><input class="input" type="text" value="Name:" onBlur="if(this.value=='') this.value='Name:'" onFocus="if(this.value =='Name:' ) this.value=''" ></div>
<div class="wrapper"><input class="input" type="text" value="Email:" onBlur="if(this.value=='') this.value='Email:'" onFocus="if(this.value =='Email:' ) this.value=''" ></div>
<div class="wrapper"><input class="input" type="text" value="Tel:" onBlur="if(this.value=='') this.value='Fax:'" onFocus="if(this.value =='Fax:' ) this.value=''"></div>
<div class="textarea_box"><textarea cols="1" rows="1" onBlur="if(this.value=='') this.value='Message:'" onFocus="if(this.value =='Message:' ) this.value=''" >Message:</textarea></div>
<a href="javascript:" class="button" onClick="document.getElementById('ContactForm').reset()">clear</a>
<a href="javascript:" class="button" onClick="document.getElementById('ContactForm').submit()">send</a>
</div>
</form>
</article>


and here is my sendresults.php


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

// Subject of email sent to you.
$subject = 'CONTACT FORM FROM xxxx';

// Your email address. This is where the form information will be sent.
$emailadd = 'Help@xxxx.co.uk';

// Where to redirect after form is processed.
$url = 'http://www.xxxx.co.uk';

// 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.'">';
?>

many thanks for the help, ive edited emails and webbys out

edbr
07-23-2011, 11:29 PM
give your form fields a name/id

thecheekymonkey
07-24-2011, 08:22 AM
hi, i dont suppose you could be more detailed / example? not really my thing php / web building.

many thanks.

cheers

edbr
07-25-2011, 01:05 AM
<input class="input" type="text" name"fax" id="fax" value="Tel:" onBlur="if(this.value=='') this.value='Fax:'" onFocus="if(this.value =='Fax:' ) this.value=''">

then the value will be passed as $_POST['fax']

thecheekymonkey
07-25-2011, 07:43 AM
so ive edited my dreamwever code as this and it still doesnt work.

cheers for the patience and help.

<form action="http://www.xxxx.co.uk/sendresults.php" method="post" id="ContactForm">
<div>
<div class="wrapper"><input class="input" type="text" name"Name:" id="Name:" value="Name:" onBlur="if(this.value=='') this.value='Name:'" onFocus="if(this.value =='Name:' ) this.value=''" ></div>
<div class="wrapper"><input class="input" type="text" name"Email:" id="Email:" value="Email:" onBlur="if(this.value=='') this.value='Email:'" onFocus="if(this.value =='Email:' ) this.value=''" ></div>
<div class="wrapper"><input class="input" type="text" name"Tel:" id="Tel:" value="Tel:" onBlur="if(this.value=='') this.value='Tel:'" onFocus="if(this.value =='Tel:' ) this.value=''"></div>
<div class="textarea_box"><textarea cols="1" rows="1" onBlur="if(this.value=='') this.value='Message:'" onFocus="if(this.value =='Message:' ) this.value=''" >Message:</textarea></div>
<a href="javascript:" class="button" onClick="document.getElementById('ContactForm').reset()">clear</a>
<a href="javascript:" class="button" onClick="document.getElementById('ContactForm').submit()">send</a>
</div>
</form>

edbr
07-25-2011, 08:03 AM
hm it should try deleting the following incase its character numbers

$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;

thecheekymonkey
07-25-2011, 08:36 AM
Sorry still didnt work, have edited code now to this


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

// Subject of email sent to you.
$subject = 'CONTACT FORM FROM XXXX';

// Your email address. This is where the form information will be sent.
$emailadd = 'Help@XXXX.co.uk';

// Where to redirect after form is processed.
$url = 'http://www.XXXX.co.uk';

// 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;}
}
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.'">';
?>



and still no message....cheers for the help

edbr
07-25-2011, 09:01 AM
wait i just read this properly
Results from form:

but nothing else, no email address, name or message.Results from form:

but nothing else, no email address, name or message.

what message you still havent named the textfield and what email do you meanif you get form fields?

estebandraws
09-23-2011, 07:05 PM
I was having the same problem. No results in the email that was generated.

When I changed the encoding type to multipart/form-data it sent the form fields in the email.

edbr
09-24-2011, 02:11 AM
that would change anything in this case

thecheekymonkey
01-30-2013, 12:05 AM
wait i just read this properly


what message you still havent named the textfield and what email do you meanif you get form fields?

ok sorry to ressurect this post, but for the last x amount of months i have been unable to do anything.


ok, so i am still where i left off, i.e.

i have done everything you have said, but i still can get the email from the sendresults.

but


i only get

Results from form:



the rest is blank/

please can you help?


cheers

edbr
01-30-2013, 05:39 AM
ok please post complete form and php script and ill test it here and let you have it if it needs changing

thecheekymonkey
01-30-2013, 01:33 PM
<article class="col1 pad_left1">
<h2>Contact Form</h2>
<form action="http://www.xxxx.co.uk/sendresults.php" method="post" id="ContactForm">
<div>
<div class="wrapper"><input class="input" type="text" value="Name:" onBlur="if(this.value=='') this.value='Name:'" onFocus="if(this.value =='Name:' ) this.value=''" ></div>
<div class="wrapper"><input class="input" type="text" value="Email:" onBlur="if(this.value=='') this.value='Email:'" onFocus="if(this.value =='Email:' ) this.value=''" ></div>
<div class="wrapper"><input class="input" type="text" value="Tel:" onBlur="if(this.value=='') this.value='Fax:'" onFocus="if(this.value =='Fax:' ) this.value=''"></div>
<div class="textarea_box"><textarea cols="1" rows="1" onBlur="if(this.value=='') this.value='Message:'" onFocus="if(this.value =='Message:' ) this.value=''" >Message:</textarea></div>
<a href="javascript:" class="button" onClick="document.getElementById('ContactForm').reset()">clear</a>
<a href="javascript:" class="button" onClick="document.getElementById('ContactForm').submit()">send</a>
</div>
</form>
</article>


and here is my sendresults.php

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

// Subject of email sent to you.
$subject = 'CONTACT FORM FROM XXXX';

// Your email address. This is where the form information will be sent.
$emailadd = 'Help@XXXX.co.uk';

// Where to redirect after form is processed.
$url = 'http://www.XXXX.co.uk';

// 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;}
}
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.'">';
?>





many thanks hopefully i can get this sorted, appreciate the help

edbr
01-31-2013, 04:13 AM
its what i said there is no name or id value

change your form to this
<form action="sendresults.php" method="post" id="ContactForm">
<div>
<div class="wrapper"><input class="input" name="name" type="text" value="Name:" onBlur="if(this.value=='') this.value='Name:'" onFocus="if(this.value =='Name:' ) this.value=''" ></div>
<div class="wrapper"><input class="input" name="Email" type="text" value="Email:" onBlur="if(this.value=='') this.value='Email:'" onFocus="if(this.value =='Email:' ) this.value=''" ></div>
<div class="wrapper"><input class="input" name="Fax" type="text" value="Tel:" onBlur="if(this.value=='') this.value='Fax:'" onFocus="if(this.value =='Fax:' ) this.value=''"></div>
<div class="textarea_box"><textarea name="Message" cols="30" rows="1" onBlur="if(this.value=='') this.value='Message:'" onFocus="if(this.value =='Message:' ) this.value=''" >Message:</textarea></div>
<a href="javascript:" class="button" onClick="document.getElementById('ContactForm').reset()">clear</a>
<a href="javascript:" class="button" onClick="document.getElementById('ContactForm').submit()">send</a>
</div>
</form>

jmichae3
02-01-2013, 05:31 AM
Sorry still didnt work, have edited code now to this


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

// Subject of email sent to you.
$subject = 'CONTACT FORM FROM XXXX';

// Your email address. This is where the form information will be sent.
$emailadd = 'Help@XXXX.co.uk';

// Where to redirect after form is processed.
$url = 'http://www.XXXX.co.uk';

// 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.
//you could use true/false here instead.
$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;}
}
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.'">';
?>
and still no message....cheers for the help


what you have there is, if $req is not '1' (and it isn't), form will always do nothing.
examine your brace nesting/indent levels. get that right first. if you can keep that correct,it will solve a lot of problems in your code,because you can catch logic errors, and oops-i-forgot-an-else and other stuff.
this is missing an else on $req. actually,the $req detection logic is in the wrong place. it should only be in a place where you can turn on and off the sending of mail. it's OK to build the mail message and not send it (and maybe give a message instead). :-)

here is what your code properly indented looks like:

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

// Subject of email sent to you.
$subject = 'CONTACT FORM FROM XXXX';

// Your email address. This is where the form information will be sent.
$emailadd = 'Help@XXXX.co.uk';

// Where to redirect after form is processed.
$url = 'http://www.XXXX.co.uk';

// 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.
//you could use true/false here instead.
$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;
}
}
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.'">';
?>

try this:

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

// Subject of email sent to you.
$subject = 'CONTACT FORM FROM XXXX';

// Your email address. This is where the form information will be sent.
$toemail = 'Help@XXXX.co.uk';
$fromname = "www.XXXX.co.uk";
$toname = "www.XXXX.co.uk";
$fromemail = "help@XXXX.co.uk"; //required email From: email address. must be real email account on the web server

// Where to redirect after form is processed.
$url = 'http://www.XXXX.co.uk';

// 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.
//you could use true/false here instead.
$req = '0';

// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";
$space = ' ';
$line = "\r\n";
$reqfailure = false; //detection of any empty field
$reqfailurekey = ""; //empty key name
foreach ($_POST as $key => $value)
{
//echo "$key is empty";die;
$reqfailure |= (''==$value); //OR $reqfailure with the conditional
if ($reqfailure) {
$reqfailurekey = $key; //save for later use
break;
}

$value = str_replace('\n', "$line", $value);
//just where is $j set?
$conc = $key . ":" . $str_repeat($space,$j) . $value . $line;
$text .= $conc;
}
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\n";
$headers .= "X-Priority: 1\n";
$headers .= 'X-Mailer: PHP/' . phpversion()."\n"; //required to get through, may still be caught by spam filters
$headers .= "From: \"".$fromname."\" <".$fromemail.">\n"; //won't send without this
$headers .= "Reply-To: \"".$toname."\" <".$toemail.">\n"; //won't send without this
$headers .= "Return-Path: <".$toemail.">\n"; //person can't reply without this
if ('1' == $req) //compiler will complain if you use = assignment accidentally this way
{
if ($reqfailure) {
//display error message of choice or do action
echo $reqfailurekey . " is empty";
die;
} else {

mail($toemail, $subject, $text, $headers);
echo '<meta http-equiv="refresh" content="0; URL='.$url.'">';
//or
header("Location: " . $url); /* Redirect browser - this PHP must be BEFORE ANY TEXT */
}
} else {
mail($toemail, $subject, $text, $headers);
echo '<meta http-equiv="refresh" content="0; URL='.$url.'">';
//or
header("Location: " . $url); /* Redirect browser - this PHP must be BEFORE ANY TEXT */
}
?>

edbr
02-01-2013, 06:28 AM
but tested and it works, although a poor validation that fields are filled in, even the poster had blank mails sent so the problem was as i said the name+ being missing in the form.

jmichae3
02-01-2013, 08:33 AM
I don't know where $j is supposed to be set. but it looks like it is used without being declared.
minor issue,the original for loop or in my code,anything that references it will simply fail and the code will continue to run.
unsure if the original user wants the variable-indenting of the value or not. seems odd. I should think you would only want one space if that.

but it's easy to make a code indenter. if you want to indent by 4 spaces each, you simply multiply $j by 4 when you use str_repeat() so then really $j would be better named $indentLevel.

good names make things easier when you come back 6 months later and wonder what in the world you were thinking when you wrote the code. when you are debugging though, it's probably OK to use simple names like $j, but it's not readable to someone else. code readability is a good principle to code by.

my instructor 20 years ago at the beginning of the class stressed things like proper code indentation and the like. there were 2 brace styles, K&R and the other kind used today mostly (I stuck with K&R for reasons of my own, one of which it uses 1 fewer line of code, and I need more density).


//K&R method
function a() {
}
//other curly brace method
function a()
{
}


oops. I am going on about style. sorry. I try to teach principles as much as I can so someone can do things on their own. "give a man a fish and he will eat for a meal. but teach a man to fish and you will feed him for a lifetime."

anyway, str_repeat() should be much faster for generating strings of indenting spaces (cool function of the day!), since it is a built-in PHP function and probably originally coded in C or C++,compiled, and optimized (for speed or memory, either one).

hope this helps. I am going to yahoo answers to help some folks if I can. the headers I provided should get farther beyond the spam filters and should actually send a usable email. enjoy.

edbr
02-02-2013, 02:39 AM
perhaps so not disputing but it worked and has been used many times. please feel free to write a complete new script with better validation and im sure we will be happy indeed to add this for download in place of this script. many posters simply want easy solutions not lessons in coding

domedia
02-06-2013, 05:56 PM
jmichae3,

I would encourage you to write it if you think it's substantially better :)

It's also a great promotion of yourself.

More here:
http://www.dreamweaverclub.com/about-us.php

.