Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Email form captcha not functioning
Old 03-12-2010, 07:37 PM Email form captcha not functioning
Rayo's Avatar
Experienced Talker

Posts: 35
Trades: 0
Hi everyone....can some kind soul help me fix this PHP script i'm struggling with, its doin me ed in!
Its a PHP email contact form with captcha.
The problem is with the captcha part of the script...the form will run and show errors for invalid fields exept for the captcha.....the capcha image displays and refreshes but the form is not processing the captcha function.
The form can be filled in and the emails sent but the captcha part of the script is dead....I hope this is a clear enough description.
Here's the code.

<?php

define("MAIL_TARGET","my email.com");
define("errorName","Invalid name! It must be at least 2 characters long");
define("errorEmail","Invalid email address!");
define("errorMsg","Invalid message! It must be at least 10 characters long");
define("errorCode","Security code is incorrect! please try again");

function createForm($name="",$email="",$message="",$error1= "",$error2="",$error3="",$error4=""){
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table>
<tr><td>Name: </td><td class="error"><?php echo $error1; ?></td></tr>
<tr><td colspan="2"><input class="text" type="text" name="name" value="<?php echo $name; ?>"></td></tr>
<tr><td>Email:</td><td class="error"><?php echo $error2; ?></td></tr>
<tr><td colspan="2"><input class="text" type="text" name="email" value="<?php echo $email; ?>"></td></tr>
<tr><td>Message:</td><td class="error"><?php echo $error3; ?></td></tr>
<tr><td colspan="2"><textarea class="message" cols="42" rows="8" name="message"><?php echo $message; ?></textarea></td></tr>
<td ><p><img src='captcha.php?<?PHP echo time(); ?>' border='1'><br/>
<tr><td colspan="2"><input class="text" type="text" name="usercode" value="<?php echo $usercode; ?>"></td></tr>
<tr><td>Security Code:</td><td class="error"><?php echo $error4; ?></td></tr>
<tr><td colspan="2"><br/><input class="text" type="submit" name="submitBtn" value="Send email"></td></tr>
</table>
</form>
<?php
}

function isValidEmail($email){
$pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";

if (eregi($pattern, $email)){
return true;
}
else {
return false;
}
}

function sendMail($name,$email,$message){

$subject = "Message from website";
$from = "From: $name <$email>\r\nReply-To: $email\r\n";
$header = "MIME-Version: 1.0\r\n"."Content-type: text/html; charset=iso-8859-1\r\n";
$content = htmlspecialchars($message);

$content = wordwrap($content,70);
mail(MAIL_TARGET,$subject,$content,$from.$header);

}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Email form</title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
</head>
<body background="/assets/images/GryPatBackD.gif">
<div id="main">
<div id="caption">Email</div>
<div id="icon">&nbsp;</div>
<?php if (!isset($_POST['submitBtn'])) {
createForm();
} else {
$name = isset($_POST['name']) ? $_POST['name'] : "";
$email = isset($_POST['email']) ? $_POST['email'] : "";
$message = isset($_POST['message']) ? $_POST['message'] : "";
$usercode = isset($_POST['usercode']) ? $_POST['usercode'] : "";

$error = false;

if (strlen($name)<2) {
$error = true;
$error1 = errorName;
}
if (!isValidEmail($email)) {
$error = true;
$error2 = errorEmail;
}
if (strlen($message)<10) {
$error = true;
$error3 = errorMsg;
}
@session_start();
if ($_SESSION['AntiSpamImage'] != $_REQUEST['usercode']) {
$_SESSION['AntiSpamImage'] = rand(1,9999999);
$error = true;
$error4 = errorCode;
}

if ($error){
createForm($name,$email,$message,$error1,$error2,$ error3);
}
else {
sendMail($name,$email,$message);
?>

<div id="result">
<table width="100%">
<tr><td>
Your message has been sent!
</td></tr>
</table>
</div>
<?php
}
}
?>
<div>
</body>

Last edited by Rayo; 03-13-2010 at 08:51 AM..
Rayo is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-13-2010, 01:02 AM Re: Email form captcha not functioning
Isabella_Smith's Avatar
Ultra Talker

Posts: 285
Trades: 0
Try with putting this :

session_start();

just before this:

if ($_SESSION['AntiSpamImage'] != $_REQUEST['usercode'])

Besides this, put session_start(); in your captcha.php
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE



Please login or register to view this content. Registration is FREE
buy all indian salwar Kameez, Sarees and clothes

Last edited by Isabella_Smith; 03-13-2010 at 01:10 AM..
Isabella_Smith is offline
Reply With Quote
View Public Profile
 
Old 03-13-2010, 04:27 AM Re: Email form captcha not functioning
Rayo's Avatar
Experienced Talker

Posts: 35
Trades: 0
Hi Isabella........thank you for your input....I had put session_start(); at this point....I know it has to be there but I must have deleted it during an act of desperation, sorry for the error, I've ammended the above script.
I also have this at the top of the captcha.php along with the correct id's, but still no go.
As a point of note.....this captcha script has been running fine within a sign up script I managed to muddle together a while ago....so I feel this is ok.
If only my brain was as good as my enthusiasm.

Hold it I just had a result....the captcha validation is now working but the error message for wrong captcha input ie: $error4 won't display..... its totaly baffling because everything looks right.
I have updated the code above accordingly.
Any idea's very welcome.

Last edited by Rayo; 03-13-2010 at 08:59 AM.. Reason: New Result
Rayo is offline
Reply With Quote
View Public Profile
 
Old 03-14-2010, 05:08 AM Re: Email form captcha not functioning
Rayo's Avatar
Experienced Talker

Posts: 35
Trades: 0
Hold it for anyone helping with this problem..........I've finally woke up....I see I haven't completed the create form $errors.
Thanks to everyone showing interest in this thread....apologies for my poor obrservation.
Rayo is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Email form captcha not functioning
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.27877 seconds with 12 queries