Copy link to clipboard
Copied
Hi Guys/gals
Recently bought a template so i could start learning and create my first site, but i have come stuck on the Feeback form.
I know how to get introduce the mailto: code to send email, but what i really want is a php code to do it for me. here is what i have so far.
Form Code:
<form id="ContactForm" action="form.php" method="post">
<div>
<label><span class="input"><input type="text" value="Name:" onBlur="if(this.value=='') this.value='Name:'" onFocus="if(this.value =='Name:' ) this.value=''" /></span></label>
<label><span class="input"><input type="text" value="E-mail:" onBlur="if(this.value=='') this.value='E-mail:'" onFocus="if(this.value =='E-mail:' ) this.value=''" /></span></label>
<label><span class="input"><input type="text" value="Subject:" onBlur="if(this.value=='') this.value='Subject:'" onFocus="if(this.value =='Your Country:' ) this.value=''" /></span></label>
<span class="textarea"><textarea onBlur="if(this.value=='') this.value='Your Message:'" onFocus="if(this.value =='Your Message:' ) this.value=''" >Your Message:</textarea></span>
</div>
<div class="alignright">
<a href="#" class="link" onClick="document.getElementById('ContactForm').reset()"><span><span>clear</span></span></a>
<a href="#" class="link" onClick="document.getElementById('ContactForm').submit()"><span><span>send</span></span></a>
</div>
</form>
And here is the PHP:
<?php
function spamcheck($field)
{
//filter_var() sanitizes the e-mail
//address using FILTER_SANITIZE_EMAIL
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
if (isset($_REQUEST['email']))
{//if "email" is filled out, proceed
//check if the email address is invalid
$mailcheck = spamcheck($_REQUEST['email']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{//send email
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("email@person.com", "Subject: $subject",
$message, "From: $email" );
echo "Thank you for using our mail form";
}
}
else
{//if "email" is not filled out, display the form
echo "<form method='post' action='mailform.php'>
Email: <input name='email' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>";
}
?>
Can someone plase let me know where i am going wrong.
Thanx in advance.
Pedro
Copy link to clipboard
Copied
First, I'd recommend removing the $_REQUEST superglobal in favor of $_POST since your form is sending via POST method. Otherwise, someone could fake a cookie or GET variable into your script and you wouldn't know the difference. Second, what exactly is wrong? Are you getting an error? If so, what? Or is it not sending in a test environment? If so, what environment (Windows/Mac) and which package are you using (eg: MAMP, WAMP, XAMPP, etc.)?
Copy link to clipboard
Copied
Hi
I am using windows and its not working in a test environment, i havent set up the xamp yet as i didnt think i needed to, as you can see i am new to all things web design/development related.
The Web hosting i have does have the facility of a server, do i dump the XAMP in there?
Copy link to clipboard
Copied
jpfcastella wrote:
Hi
I am using windows and its not working in a test environment
That's the key and that's exactly what I thought it might be but I didn't want to say. Windows does not have a built-in way to handle sending outgoing mail from an Apache environment. You must open up the PHP.ini file and then change the SMTP name to that of the SMTP for your ISP and then set the sendmail_form address to be your local email address. You should be able to leave the smtp_port field where it's at. If your SMTP server requires authentication I would recommend following the link below as they provide some alternatives.
http://stackoverflow.com/questions/4652566/php-mail-setup-in-xampp