Technical Discussion
  >> Web Design / HTML / Web hosting Forum


Register (or login) on our website and you will not see this ad.


These posts have been archived and can no longer be replied to or modified.
  Print Thread
Standard User deleted
(deleted) Thu 29-Apr-10 16:28:00
Print Post

Form PHP Help Please, Anyone?


[link to this post]
 
Hi, I've used the following code for a contact form on my website, I've two q's that maybe someone can help with. (I'm no expert at php btw, I found this a couple of years ago.)

Is there a way without captcha to reduce spam coming back?

Also, after the php, the html shows confirmation of the data submitted in the form. The item listed under workshop is a check box from the web form page. There are 9 check boxes, but this only shows the last checked box (if you know what I mean ie, there could be all 9 checked but only the last one comes back as a result. How do I get round this problem?

Am I better to shelve this code for something else do you think?

The html on the web form page:

<form action="workshopenquiryformproc.php" method="post" id="enquiryform">

<fieldset>

<dl>
<dt>
<label for ="name">Name:</label>
</dt>

<dd>
<input type = "text" name="name" id="name" tabindex="1" />
</dd>

<dt>
<label for ="email">Email Address:</label>
</dt>

<dd>
<input type = "text" name="email" id="email" tabindex="2" />
</dd>

<dt>
<label for ="telno">Tel. No.:</label>
</dt>

<dd>
<input type = "text" name="telno" id="telno" tabindex="3" />
</dd>

<dt>
<label for ="yourlocation">Your Location:</label>
</dt>

<dd>
<input type = "text" name="yourlocation" id="yourlocation" tabindex="4" />
</dd>

<br />
<p>
Check any and all that are of interest to you:
</p>

<input type="checkbox"name="workshop" value="shop1" />Shop 1<br />
<input type="checkbox"name="workshop" value="shop2" />Shop2<br />
<input type="checkbox"name="workshop" value="shop3" />Shop 3<br />
<input type="checkbox"name="workshop" value="shop4" />Shop 4<br />
<input type="checkbox"name="workshop" value="shop5" />Shop 5<br />
<input type="checkbox"name="workshop" value="shop6" />Shop 6<br />
<input type="checkbox"name="workshop" value="shop7" />Shop 7<br />
<input type="checkbox"name="workshop" value="shop8" />Shop 8<br />
<input type="checkbox"name="workshop" value="shop9" />Shop 9<br />
<input type="checkbox"name="workshop" value="" /><br />
<br />

<dt>
<label for="additionalinfo">Please use this box to supply any additional information:</label>
</dt>
<dd>
<textarea name="additionalinfo" id ="additionalinfo" rows="8" cols="25" tabindex="4"></textarea>
</dd>
</dl>
</fieldset>
<input type="submit" value="Send Form" tabindex="5" />
</form>

And the file called by the form to process:

<?php

$my_email = "[email protected]";

//Continue to the index page once user has submitted form.
$continue = "";

// Initialise variables

$errors = array();

if($_SERVER['REQUEST_METHOD'] == "POST"){$form_input = $_POST;}elseif($_SERVER['REQUEST_METHOD'] == "GET"){$form_input = $_GET;}else{exit;}

// Remove leading whitespace from all values.

function recursive_array_check(&$element_value)
{

if(!is_array($element_value)){$element_value = ltrim($element_value);}
else
{

foreach($element_value as $key => $value){$element_value[$key] = recursive_array_check($value);}

}

return $element_value;

}

recursive_array_check($form_input);

// Check referrer is from same site.

if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){$errors[] = "You must enable referrer logging to use the form";}

// Check for a blank form.

function recursive_array_check_blank($element_value)
{

global $set;

if(!is_array($element_value)){if(!empty($element_value)){$set = 1;}}
else
{

foreach($element_value as $value){if($set){break;} recursive_array_check_blank($value);}

}

}

recursive_array_check_blank($form_input);

if(!$set){$errors[] = "You cannot send a blank form";}

// Strip HTML tags from all fields.

function recursive_array_check2(&$element_value)
{

if(!is_array($element_value)){$element_value = strip_tags($element_value);}
else
{

foreach($element_value as $key => $value){$element_value[$key] = recursive_array_check2($value);}

}

return $element_value;

}

recursive_array_check2($form_input);

// Validate name field.

if(isset($form_input['name']) && !empty($form_input['name']))
{

if(preg_match("`[\r\n]`",$form_input['name'])){$errors[] = "You have submitted an invalid new line character";}

if(preg_match("/[^a-z' -]/i",stripslashes($form_input['name']))){$errors[] = "You have submitted an invalid character in the name field";}

}

// Validate email field.

if(isset($form_input['email']) && !empty($form_input['email']))
{

if(preg_match("`[\r\n]`",$form_input['email'])){$errors[] = "You have submitted an invalid new line character";}

if(!preg_match('/^([a-z][a-z0-9_.-\/\%]*@[^\s\"\)\?<>]+\.[a-z]{2,6})$/i',$form_input['email'])){$errors[] = "Email address is invalid";}

}

// Display any errors and exit if errors exist.

if(count($errors)){foreach($errors as $value){print "$value<br>";} exit;}

// Build message.

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(!is_numeric($key)){$message_output .= "\n\n".$key.": ".build_message($value);}else{$message_output .= "\n\n".build_message($value);}}}return $message_output;}

$message = build_message($form_input);

$message = $message . "\n\n-- \nSubmitted from Website Enquiry Form";

$message = stripslashes($message);

$subject = "Website Enquiry";

$headers = "From: " . $form_input['email'] . "\n" . "Return-Path: " . $form_input['email'] . "\n" . "Reply-To: " . $form_input['email'] . "\n";

mail($my_email,$subject,$message,$headers);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>

<title>title here</title>

<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />

<meta name="description" content="" />

<meta name="keywords" content="" />

<meta name="author" content="" />

<meta name="verify-v1" content="" />

<link rel="stylesheet" href="style/masterstyle.css" type="text/css" media="all" />

</head>

<body class="contact">

<div id ="wrapper">

<?php include("menu.txt"); ?>

<div id="mainframe">

<div id="content">

<p class="intro">
Enquiry Confirmation
</p>

<br /><br />

<p>
Thank you for your enquiry <?php print stripslashes($form_input['name']); ?>.
</p>

<p>
Please find below confirmation of the information you have submitted.
</p>

<p>
Contact Name: <?php print stripslashes($form_input['name']); ?> <br />
Email Address: <?php print stripslashes($form_input['email']); ?> <br />
Tel. No: <?php print stripslashes($form_input['telno']); ?> <br />
Your Location: <?php print stripslashes($form_input['location']); ?> <br />
Workshop Interests: <?php print stripslashes($form_input['workshop']); ?> <br />
Addtional Info: <?php print stripslashes($form_input['additionalinfo']);?>
</p>
</p>
</div>
</div>
<div id="clearingdiv">
</div>
<div id ="footer">
</div>

</div>

</body>

</html>
Standard User deleted
(deleted) Thu 29-Apr-10 22:16:33
Print Post

Re: Form PHP Help Please, Anyone?


[re: deleted] [link to this post]
 
For your first question, you could put a hidden text box (with css) into the form. Then with PHP check if that form has anything entered (most spambots fill out all boxes it can see in the source). If anything is entered in that box, then reject the form.
Standard User ukwiz
(fountain of knowledge) Fri 30-Apr-10 11:29:27
Print Post

Re: Form PHP Help Please, Anyone?


[re: deleted] [link to this post]
 
You could also look at Tectite FormMail to do all the work you need, and much more.

David

O2 Pro - Unlimited, cheap, good support


Register (or login) on our website and you will not see this ad.

Standard User deleted
(deleted) Tue 04-May-10 18:08:34
Print Post

Re: Form PHP Help Please, Anyone?


[re: deleted] [link to this post]
 
In reply to a post by smiffy12:
There are 9 check boxes, but this only shows the last checked box (if you know what I mean ie, there could be all 9 checked but only the last one comes back as a result. How do I get round this problem?
There are ten checkboxes, and the last one is missing a value attrib.
  Print Thread

Jump to