I have this form:
<form action="../index_success.php" method="post" id="sendEmail" class="email">
<h3 class="register2">Newsletter Signup:</h3>
<ul class="forms email">
<li class="name">
<label for="yourName">Name: </label>
<input type="text" name="yourName" class="info" id="yourName" value="<?php echo $_POST['yourName']; ?>" /><br />
</li>
<li class="city"><label for="yourCity">City: </label>
<input type="text" name="yourCity" class="info" id="yourCity" value="<?php echo $_POST['yourCity']; ?>" /><br />
</li>
//This is where I need help with the check box code
<li class="classes"><label for="classInterest">Interested in classes?: </label>
<input type="checkbox" name="classInterest" class="info" id="classInterest" value="Yes" /><br />
</li>
<li class="email">
<label for="emailFrom">Email: </label>
<input type="text" name="emailFrom" class="info" id="emailFrom" value="<?php echo $_POST['emailFrom']; ?>" />
<?php if(isset($emailFromError)) echo '<span class="error">'.$emailFromError.'</span>';
?>
</li>
<li class="buttons email">
<button type="submit" id="submit">Send</button>
<input type="hidden" name="submitted" id="submitted" value="true" />
</li>
</ul>
</form>
This is emailed to a user. I don't know how to add the check box above so the user sees a "yes" if the box is checked:
<?php
$mailTo = 'xxx#xxx.com'; // This is the hardcoded Recipient Address
$mailSubject = 'Subject'; // This is the hardcoded Subject
$mailFrom = $_POST['emailFrom'];
$yourName = $_POST['yourName'];
$yourCity = $_POST['yourCity'];
$classInterest = $_POST['classInterest']; //This is the code for the checkbox
$mailHeader = "From: {$mailFrom}";
$mailBody = "Name = {$yourName} City = {$yourCity} Class interest = {$classInterest}";
mail( $mailTo , $mailSubject , $mailBody , $mailHeader );
Basically, what I need is (psudocode):
if ($classInterest == "yes") {
$classInterest = "Interested in Classes in ".$yourCity;
}
else {
...
}
Try this:
$classInterest = (isset($_POST['classInterest']) && $_POST['classInterest'] == 'Yes') ? "Interested in Classes in " . $yourCity : '';
EDIT
http://blog.gerv.net/2006/10/firefox_reload_behaviour/ read the article and first comment by Jason
Related
Can anyone see what's wrong with these two files? When I click submit, it just redirects me to a blank page and I get no feedback from the form what so ever let alone an email.
HTML code
<form action="../script/form14.php" method="POST" >
<h1>KittyBoo</h1>
<p>Beställ kattmat online - alltid laktos- och glutenfri</p><fieldset class="formular">
<legend>1. Välj produkt</legend>
<ul>
<li>
<input type="checkbox" name="Kitty Extra Small" value="Kitty Extra Small"> <li> Kitty Extra Small: 90 kr
<br></li>
<li>
<input type="checkbox" name="Kitty Small" value="Kitty Small"> <li> Kitty Small: 140 kr
<br></li>
<li>
<input type="checkbox" name="Kitty Medium" value="Kitty Medium"> <li> Kitty Medium: 180 kr
<br></li>
<li>
<input type="checkbox" name="Kitty Large" value="Kitty Large"> <li> Kitty Large: 210 kr
<br></li>
<li>
<input type="checkbox" name="Kitty Extra Large" value="Kitty Extra Large"> <li> Kitty Extra Large: 240 kr<br></li>
</ul>
</fieldset>
<fieldset class="formular">
<legend>2. Dina uppgifter</legend>
<label for="namn"> För- och efternamn:</label><br>
<input type="text" id="namn" name="from" required><br>
<label for="email">Epost:</label><br>
<input type="text" id="email" name="email" required><br>
<label for="emailconfirm">Konfirmera Epost:</label>
<input type="text" id="emailconfirm" name="emailconfirm" required>
<label for="adress">Adress:</label>
<input type="text" id="adress" name="adress" required>
<label for="postnummer">Postnummer:</label>
<input type="text" id="postnummer" name="postnummer" required>
<label for="stad">Stad:</label>
<input type="text" id="stad" name="stad" required>
<label for="land">Land:</label>
<select id="land" name="Land">
<option> Sverige</option>
<option> Finland</option>
<option> Norge</option>
<option> Danmark</option>
<option> Island</option>
</select>
</fieldset>
<fieldset class="formular">
<legend>3. Leverans</legend>
<label for="date">Välj datum för leverans:</label>
<input type="date" id="date" name="date" required/>
<p>Leveranssätt:</p>
<ul>
<li>
<input type="radio" name="radAnswer" required value="Posten"> <a> Posten</a>
<input type="radio" name="radAnswer" required value="DHL"> <a> DHL</a>
<input type="radio" name="radAnswer" required value="FedEx"> <a> FedEx</a>
</li>
</ul>
<p>Välj färg på din KittyBoo:</p>
<input type="radio" name="radAnswer" value="bla"> <a> Blå</a>
<input type="radio" name="radAnswer" value="rosa"> <a> Rosa</a>
<p>Övriga önskemål kring din leverans:</p>
<textarea name="subject" id="ovrig" rows="3" cols="30" maxlength="300" required> Max 300 ord</textarea>
</fieldset>
<input type="submit" value="skicka">
</form>
PHP Script
<?php
function spamcheck($field)
{
// Sanitize e-mail address
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
// Validate e-mail address
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
?>
<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"]))
{
?>
<?php
}
else
// the user has submitted the form
{
// Check if the "from" input field is filled out
if (isset($_POST["email"]))
{
// Check if "from" email address is valid
$mailcheck = spamcheck($_POST["email"]);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{
$from = $_POST["from"]; // sender
$subject = $_POST["subject"];
$email = $_POST["email"];
function build_message($request_input){if(!isset($message_output))
{$message_output ="";}if(!is_array($request_input))
{$message_output = $request_input;}
else{foreach($request_input as $key => $value)
{if(!empty($value)){if(!is_numeric($key))
{$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;}
else{$message_output .= build_message($value).", ";}}}}return rtrim($message_output,", ");}
$message = build_message($_REQUEST);
// message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
// send mail
mail("helenaevaschroder#gmail.com",$subject,$message, "From: $from\n");
echo "Tillbakaack för ditt meddelande";
echo "<p><a href=http://www.sh.se>Tillbaka till sidan</a></p>";
}
}
}
?>
The server is set up for PHP.
If it would be of any help, you can check the form out here:http://student.mtstudent.se/~sh14hf2407/pages/formular.html
Your PHP condition if (!isset($_POST["submit"])) { ... } is always FALSE because $_POST['submit'] doesn't exist in your html form. You have created an element <input type="submit" value="skicka"> but $_POST['submit'] means "an element which name attribute is submit". So replace :
<input type="submit" value="skicka">
by
<input type="submit" name="submit" value="skicka">
and this issue will be fixed.
change
<input type="submit" value="skicka">
into
<input type="submit" name="submit" value="skicka">
I am having trouble figuring our exactly why my php code won't pick up the first name from my form, I hope you guys can help.
Everything else is emailed through fine, just the first name field which is element_1_1.
Here is the PHP:
$<?php
$errors = '';
$myemail = 'example#example.co.uk';
if( empty($_POST['element_1_1']) ||
empty($_POST['element_1_2']) ||
empty($_POST['element_3']) ||
empty($_POST['element_2']) ||
empty($_POST['element_5']) ||
empty($_POST['element_4']))
$name = $_POST['element_1_1'];
$lastname = $_POST['element_1_2'];
$email_address = $_POST['element_3'];
$number = $_POST['element_2'];
$area = $_POST['element_5'];
$discount = $_POST['element_4'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address \n";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "";
$email_body = "".
"\n First Name: $name \n \n Second Name: $lastname \n \n Email: $email_address \n \n Phone Number: $number \n \n Area(s): $area \n \n Discount: $discount \n".
$headers = "From: example#example.co.uk\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: http://www.example.co.uk');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Contact form handler</title>
</head>
<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
echo "";
echo " \n <a href='http://www.example.com'>Go Back</a> \n";
?>
</body>
</html>
And here is the form:
<div id="bmvform-form_container">
<form id="form_507993" class="appnitro" method="post" action="formsend.php">
<div class="form_description">
<h2>Want To Receive Our Properties?</h2>
<p><em>At Pure Acquisitions, we respect your privacy. Any details provided will NOT be sent to any third party without your consent.</em></p>
</div>
<ul >
<li id="li_1" >
<label class="description" for="element_1">Name </label>
<span>
<input id="element_1_1" name="element_1_1" class="element text" maxlength="255" size="14" value=""/>
<label>First</label>
</span>
<span>
<input id="element_1_2" name="element_1_2" class="element text" maxlength="255" size="14" value=""/>
<label>Last</label>
</span><p class="guidelines" id="guide_1"><small>We don't want to be rude!</small></p>
</li> <li id="li_3" >
<label class="description" for="element_3">Email Address </label>
<div>
<input id="element_3" name="element_3" class="element text medium" type="text" maxlength="255" value=""/>
</div><p class="guidelines" id="guide_3"><small>To send you important stuff!</small></p>
</li> <li id="li_2" >
<label class="description" for="element_2">Phone Number </label>
<div>
<input id="element_2" name="element_2" class="element text medium" type="text" maxlength="255" value=""/>
</div><p class="guidelines" id="guide_2"><small>We need to let you know about our properties!</small></p>
</li> <li id="li_5" >
<label class="description" for="element_5">Locations You Are Interested In </label>
<span>
<input id="element_5_1" name="element_5" class="element radio" type="radio" value="All" />
<label class="choice" for="element_5_1">All</label>
<input id="element_5_2" name="element_5" class="element radio" type="radio" value="London" />
<label class="choice" for="element_5_2">London</label>
<input id="element_5_3" name="element_5" class="element radio" type="radio" value="South of England" />
<label class="choice" for="element_5_3">South of England</label>
<input id="element_5_4" name="element_5" class="element radio" type="radio" value="West Midlands" />
<label class="choice" for="element_5_4">West Midlands</label>
<input id="element_5_5" name="element_5" class="element radio" type="radio" value="East Midlands" />
<label class="choice" for="element_5_5">East Midlands</label>
<input id="element_5_6" name="element_5" class="element radio" type="radio" value="North West" />
<label class="choice" for="element_5_6">North West</label>
<input id="element_5_7" name="element_5" class="element radio" type="radio" value="North East" />
<label class="choice" for="element_5_7">North East</label>
<input id="element_5_8" name="element_5" class="element radio" type="radio" value="Yorkshire & Humber" />
<label class="choice" for="element_5_8">Yorkshire & Humber</label>
<input id="element_5_9" name="element_5" class="element radio" type="radio" value="Wales" />
<label class="choice" for="element_5_9">Wales</label>
<input id="element_5_10" name="element_5" class="element radio" type="radio" value="Scotland" />
<label class="choice" for="element_5_10">Scotland</label>
</span><p class="guidelines" id="guide_5"><small>Let us know where in the country you are looking for property.</small></p>
</li> <li id="li_4" >
<label class="description" for="element_4">Ideal Discount From Market Value </label>
<div>
<input id="element_4" name="element_4" class="element text medium" type="text" maxlength="255" value=""/>
</div><p class="guidelines" id="guide_4"><small>How much discount (in percent) are you looking for?</small></p>
</li>
<li class="buttons">
<input type="hidden" name="form_id" value="507993" />
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
</li>
</ul>
</form>
</div>
if( empty($_POST['element_1_1']) ||
empty($_POST['element_1_2']) ||
empty($_POST['element_3']) ||
empty($_POST['element_2']) ||
empty($_POST['element_5']) ||
empty($_POST['element_4']))
$name = $_POST['element_1_1'];
$lastname = $_POST['element_1_2'];
$email_address = $_POST['element_3'];
$number = $_POST['element_2'];
$area = $_POST['element_5'];
$discount = $_POST['element_4'];
You're missing the curley brackets here? I would assume this or something similar with curley brackets, depending on what you'd like to do.
if( empty($_POST['element_1_1']) ||
empty($_POST['element_1_2']) ||
empty($_POST['element_3']) ||
empty($_POST['element_2']) ||
empty($_POST['element_5']) ||
empty($_POST['element_4']))
{
$name = $_POST['element_1_1'];
$lastname = $_POST['element_1_2'];
$email_address = $_POST['element_3'];
$number = $_POST['element_2'];
$area = $_POST['element_5'];
$discount = $_POST['element_4'];
}
What are you trying to do with the if-statement anyway? You're checking if one of them is empty and then set them all? Or you should check if one of them is empty and then send a message back to user that one of the fields needs to be filled? Then you should change it a bit to something like this:
if( empty($_POST['element_1_1']) ||
empty($_POST['element_1_2']) ||
empty($_POST['element_3']) ||
empty($_POST['element_2']) ||
empty($_POST['element_5']) ||
empty($_POST['element_4']))
{
do something; }
else {
$name = $_POST['element_1_1'];
$lastname = $_POST['element_1_2'];
$email_address = $_POST['element_3'];
$number = $_POST['element_2'];
$area = $_POST['element_5'];
$discount = $_POST['element_4'];
}
what is your if statement doing?
if( empty($_POST['element_1_1']) ||
empty($_POST['element_1_2']) ||
empty($_POST['element_3']) ||
empty($_POST['element_2']) ||
empty($_POST['element_5']) ||
empty($_POST['element_4']))
This seems to be unrelated to anything, but if none of them are empty, then $name is not set (It is the first line after the if statement, and will only be called if the if statement is true)
I would assume you were meant to execute all the lines, which requires curly braces around them.
For some reason, the options are not showing up in my email. I can get the email to send just fine. I can see the body and all its comments, but none of the entries that the user made. I know I am doing something wrong, but I cannot determine what it is.
Also, feel free to mock me if it looks horrible. :)
$ToEmail = 'dmandrade1978#gmail.com';
$EmailSubject = 'Message from web page!!';
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$urphone = $_POST['urphone'];
$event = $_POST['event'];
$date = $_POST['date'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$comment = $_POST['comment'];
$hearaboutus = $_POST['hearaboutus'];
$body = <<<EMAIL
Email: $email <br />
Name: $name <br />
Comment: $comment <br />
Phone: $urphone <br />
Date: $urdate <br />
Comment: $comment <br />
How did you hear?: $hearaboutus <br />
Mail optiom: $mail <br />
EMAIL;
$header = "Content-type: text/html\r\n";
mail("$ToEmail", "$EmailSubject", "$body", "$header");
echo ("Message Sent!");
?>
<td class = "form">
<form action="?" method="get" enctype="text/plain">
<p class = "form">Name:<br />
<input type="text" name="name" id="name" /></p>
<p class = "form">E-mail:<br />
<input type="text" name="email" id="email" /></p>
<p class = "form">Phone #:<br />
<input type="text" name="urphone" id="urphone" /></p>
<p class = "form">Event type:<br />
<input type="text" name="event" id="event" /></p>
<p class = "form">Date of event:<br />
<input type="text" name="date" id="date" /></p>
<p class = "form" >Prefered method of contact:<br />
<span class = "contact">
<input type="radio" name="phone" id="phone" /> Phone<br />
<input type="radio" name="mail" id="mail" /> E-mail<br />
</span></p>
<p class = "form">How did you hear about us?:<br />
<select name="hearaboutus" id="hearaboutus" />
<option value="internet">Internet</option>
<option value="word of mouth">Friend/Family</option>
<option value="magazine">Magazine</option>
<option value="other">Other</option>
</select></p>
<p class = "form">Message, questions, or availability:<br />
<textarea rows="10" cols="30" name="comment" id="comment">
</textarea></p>
<input type="submit" value="Send email to us" id="submit">
<input type="reset" value="Reset and start over">
</form>
Change method="get"
to
method="post"
and remove enctype . It's not required in this case.
Also, why is there a ? in your action? You can keep action blank as it is posting to the same page.
To make things even simpler for you, copy paste the below code and run the file again
if(isset($_POST['submit']))
{
$ToEmail = 'dmandrade1978#gmail.com';
$EmailSubject = 'Message from web page!!';
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$urphone = $_POST['urphone'];
$event = $_POST['event'];
$date = $_POST['date'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$comment = $_POST['comment'];
$hearaboutus = $_POST['hearaboutus'];
$body = <<<EMAIL
Email: $email <br />
Name: $name <br />
Comment: $comment <br />
Phone: $urphone <br />
Date: $urdate <br />
Comment: $comment <br />
How did you hear?: $hearaboutus <br />
Mail optiom: $mail <br />
EMAIL;
$header = "Content-type: text/html\r\n";
mail("$ToEmail", "$EmailSubject", "$body", "$header");
echo ("Message Sent!");
}
?>
<td class = "form">
<form action="" method="post">
<p class = "form">Name:<br />
<input type="text" name="name" id="name" /></p>
<p class = "form">E-mail:<br />
<input type="text" name="email" id="email" /></p>
<p class = "form">Phone #:<br />
<input type="text" name="urphone" id="urphone" /></p>
<p class = "form">Event type:<br />
<input type="text" name="event" id="event" /></p>
<p class = "form">Date of event:<br />
<input type="text" name="date" id="date" /></p>
<p class = "form" >Prefered method of contact:<br />
<span class = "contact">
<input type="radio" name="phone" id="phone" /> Phone<br />
<input type="radio" name="mail" id="mail" /> E-mail<br />
</span></p>
<p class = "form">How did you hear about us?:<br />
<select name="hearaboutus" id="hearaboutus" />
<option value="internet">Internet</option>
<option value="word of mouth">Friend/Family</option>
<option value="magazine">Magazine</option>
<option value="other">Other</option>
</select></p>
<p class = "form">Message, questions, or availability:<br />
<textarea rows="10" cols="30" name="comment" id="comment">
</textarea></p>
<input type="submit" name="submit" value="Send email to us" id="submit">
<input type="reset" value="Reset and start over">
</form>
I have a form that allows the user to add more form fields if needed. I need to post the values through php in an email, but it keeps coming back as "array" instead of the values. I am a novice and just can't figure out the problem.
Here is the javascript:
var counter = 1;
var limit = 50;
function addInput(divName){
if (counter == limit) {
alert("You have renter code hereeached the limit of adding " + counter + " inputs");
}
else {
var newdiv = document.createElement('div');
newdiv.innerHTML = "Entry " + (counter + 1) + " <br><input type='text' name='myInputs[]'>";
document.getElementById(divName).appendChild(newdiv);
counter++;
}
}
Here is the html form (I deleted most of it to save time):
<form id="teamreg" class="appnitro" method="post" action="teamreg.php" >
<input name="recipient" type="hidden" id="recipient" value="kim#ka-kingdesigns.com" />
<ul>
<li>
<h3>Contact Information</h3>
</li>
<li id="li_1" >
<label class="description" for="company">Company/Organization </label>
<div>
<input id="company" name="company" class="element text large" type="text" maxlength="255" value=""/>
</div>
</li>
<li id="li_3" >
<label class="description" for="contactname">Team Cooridinator</label>
<span>
<input id="element_2_1" name= "contactname_1" class="element text" maxlength="255" size="8" value=""/>
<label>First</label>
</span>
<span>
<input id="element_2_2" name= "contactname_2" class="element text" maxlength="255" size="14" value=""/>
<label>Last</label>
</span>
</li>
<li class="section_break">
<h3>Team Members</h3>
<p>Please list everyone planning on volunteering (including spouses, children, etc.)<br/>
</p>
</li>
<li>
<div id="dynamicInput">
Entry 1<br><input type="text" name="myInputs[]">
</div>
<input type="button" value="Add another text input" onClick="addInput('dynamicInput');">
</li>
<li class="buttons">
<input type="hidden" name="form_id" value="421421" />
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
</li>
</ul>
</form>
Here is the php:
header("location: registrationthankyou.html");
$company = $_POST['company'];
$coordinator = "{$_POST['contactname_1']} {$_POST['contactname_2']}";
$myInputs = $_POST['myInputs'];
foreach ($myInputs as $eachInput) {
echo $eachInput . "<br>";
}
$to = $_POST['recipient']; //"user#example.com";
$subject = "Day of Caring Team Registration".$_POST['subject'];
$comments = "
<strong>Company:</strong> {$company}<br>
<strong>Team Coordinator:</strong> {$coordinator} <br>
<strong>Team Members</strong> {$myInputs}<br><br>
mail($to,$subject,$comments,"From: $from\r\n"
."Reply-To: $from\r\n"
."Content-Type: text/html; charset=iso-8859-1\n"
."X-Mailer: PHP/" . phpversion());
Change:
<strong>Team Members</strong> {$myInputs}<br><br>
To:
<strong>Team Members</strong> ".implode(",", $_POST['myInputs'])."<br /><br />
The code you have included in the following is not doing anything for your email:
foreach ($myInputs as $eachInput) {
echo $eachInput . "<br>";
}
You can do:
foreach ($myInputs as $eachInput) {
$message .= $eachInput . "<br>";
}
And then update the email message to reflect {$myInputs} to {$message}
I'm building a contact us page that also uses a reCaptcha, but im having a few issues with it. I fill in all of the fields in the contact form and the correct reCaptcha words, but the form does not submit. I'm assuming this is something to do with the validation, but wondered if someone might be able to spot where i'm going wrong?
The PHP code at the top of my page looks like this;
<?php include('includes/session.php');
$err = '';
$success = '';
if(isset($_POST["docontact"]) && $_POST["docontact"] == "yes") {
//get form details
$form = new stdClass();
$form->name = sanitizeOne($_POST["name"], "str");
$form->email = sanitizeOne($_POST["email"], "str");
$form->phone = sanitizeOne($_POST["phone"], "str");
$form->mysevenprog = sanitizeOne($_POST["mysevenprog"], "str");
$form->enquiry = sanitizeOne($_POST["enquiry"], "str");
$form->howfindsite = sanitizeOne($_POST["howfindsite"], "str");
//Check for errors (required: name, email, enquiry)
if($form->name == "") {
$err .= '<p class="warning">Please enter your name!</p>';
}
if($form->email == "") {
$err .= '<p class="warning">Please enter your email address!</p>';
}
if($form->enquiry == "") {
$err .= '<p class="warning">Please supply an enquiry message!</p>';
}
//Send Email
if($err == "") {
$mailer = new BlueMailer();
$mailer->AddAddress(Configuration::getVar("developer_email"), Configuration::getVar("admin_email_name"));
include('templates/email/contact-us-admin.php');
if(!$mailer->Send()) {
$err .= "<p>There was an error sending submitting your request!, Please try again later.";
} else {
$success = 'thanks';
}
}
} else {
//Initialise empty variables
$form = new stdClass();
$form->name = "";
$form->email = "";
$form->phone = "";
$form->mysevenprog = "";
$form->enquiry = "";
$form->howfindsite = "";
}
?>
And then in the body of my page I have the form as follows;
<?php if($err != "") : ?>
<div class="error">
<?php echo $err; ?>
</div>
<?php endif; ?>
<?php if($success == 'thanks') : ?>
<h3>Thank you for your enquiry</h3>
<p>Your enquiry has been successfully sent. Someone will contact you shortly.</p>
<?php else: ?>
<h3>If you are looking to advertise with us, have some feedback about some of our programming or want to say 'Hi' please use the fields below</h3>
<form name="contactus" id="contactus" method="post" action="<?php echo $_SERVER['SCRIPT_NAME'] ?>">
<ul>
<li><label for="name">Your name: *</label> <input name="name" id="name" class="textbox" style="width: 75%;" type="text" value="<?php echo $form->name ?>" /></li>
<li><label for="email">Email address: *</label> <input name="email" id="email" class="textbox" style="width: 75%;" type="text" value="<?php echo $form->email ?>" /></li>
<li><label for="phone">Telephone:</label> <input name="phone" id="phone" class="textbox" style="width: 75%;" type="text" value="<?php echo $form->phone ?>" /></li>
<li><label for="mysevenprog">My Seven programme</label> <input name="mysevenprog" class="textbox" style="width: 75%;" type="text" value="<?php echo $form->mysevenprog ?>" /></li>
<li><label for="enquiry">Enquiry/Message: *</label> <textarea name="enquiry" class="textarea" rows="5" cols="30" style="width: 75%;" id="enquiry"><?php echo $form->enquiry ?></textarea></li>
<li><label for="howfindsite">How did you find out about our site?</label> <input name="howfindsite" id="howfindsite" class="textbox" style="width: 75%;" type="text" value="<?php echo $form->howfindsite ?>" /></li>
<li>
<?php
require_once('recaptchalib.php');
// Get a key from http://recaptcha.net/api/getkey
$publickey = "";
$privatekey = "";
# the response from reCAPTCHA
$resp = null;
# the error code from reCAPTCHA, if any
$error = null;
# was there a reCAPTCHA response?
if ($_POST["recaptcha_response_field"]) {
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if ($resp->is_valid) {
echo "You got it!";
} else {
# set the error code so that we can display it
$error = $resp->error;
}
}
echo recaptcha_get_html($publickey, $error);
?>
</li>
<li><input type="submit" value="Submit Form" class="button" /></li>
</ul>
<input type="hidden" name="docontact" value="yes" />
</form>
<?php endif; ?>
The form gets rendered like this in the browser;
<form name="contactus" id="contactus" method="post" action="/contact-us2.php">
<ul>
<li><label for="name">Your name: *</label> <input name="name" id="name" class="textbox" style="width: 75%;" type="text" value="" /></li>
<li><label for="email">Email address: *</label> <input name="email" id="email" class="textbox" style="width: 75%;" type="text" value="" /></li>
<li><label for="phone">Telephone:</label> <input name="phone" id="phone" class="textbox" style="width: 75%;" type="text" value="" /></li>
<li><label for="mysevenprog">My Seven programme</label> <input name="mysevenprog" class="textbox" style="width: 75%;" type="text" value="" /></li>
<li><label for="enquiry">Enquiry/Message: *</label> <textarea name="enquiry" class="textarea" rows="5" cols="30" style="width: 75%;" id="enquiry"></textarea></li>
<li><label for="howfindsite">How did you find out about our site?</label> <input name="howfindsite" id="howfindsite" class="textbox" style="width: 75%;" type="text" value="" /></li>
<li>
<script type="text/javascript" src="http://api.recaptcha.net/challenge?k=XXXXXXXXXXXXXXXXXXX"></script>
<noscript>
<iframe src="http://api.recaptcha.net/noscript?k=XXXXXXXXXXXXXXXXXXXX" height="300" width="500" frameborder="0"></iframe><br/>
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
<input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>
</noscript>
</li>
<li><input type="submit" value="Submit Form" class="button" /></li>
</ul>
<input type="hidden" name="docontact" value="yes" />
</form>
Just use Zend_Service_ReCaptcha. You'll integrate this service just with few lines:
//Creating instance
$recaptcha = new Zend_Service_ReCaptcha($pubKey, $privKey);
//Display output
echo $recaptcha->getHTML();
//Handling input
$result = $recaptcha->verify(
$_POST['recaptcha_challenge_field'],
$_POST['recaptcha_response_field']
);
//And finally validate captcha
if ($result->isValid()) {
//Cool!
}
Take a look at Using reCAPTCHA to stop spam in PHP.