Translate

Wednesday 17 September 2014

Generate Captcha using PHP in the Form

Contact us:


Explaination: In this coding i used a simple way for to generate captcha coding using php we no need to use any js and jquery and all.By using this way we can easily understand and it is to very convient for the PHP Developers.For beginners also it has to been very useful and easy to understand.


<div class="container mar-top">
<div class="topic-heading">Contact Us</div>
<form name="form" method="post" action="contact_us.php">
<div class="block-con">
<label class="lbl-con-text">Name<span style="color:red;">*</span></label>
<input type="text"  class="input_text" name="name"  required />

</div>  
<div class="block-con">
<label class="lbl-con-text">Email Id<span style="color:red;">*</span></label>
<input type="email"  class="input_text" name="email" required />
</div>
<div class="block-con">
<label class="lbl-con-text">Mobile Number<span style="color:red;">*</span></label>
<input type="text"  class="input_text" name="zipcode" required />
</div>
<div class="block-con">
<label class="lbl-con-text">City<span style="color:red;">*</span></label>
<input type="text"  class="input_text" name="city" required />
</div>
<div class="block-con">
<label class="lbl-con-text">Message<span style="color:red;">*</span></label>
<textarea class="textarea_text"  name="message" required ></textarea>
</div>
<div class="block-con" style="margin-top:36px">

<label class="lbl-con-text" >Captcha:</label>
<input type="hidden" name="cap1" value="<?php echo $Captcha; ?>" >
<input type="text"  readonly disabled style="width: 9%;background:url(images/pattern-icon.png) repeat-x" value="<?php echo $Captcha; ?>"/>
<input type="text" name="cap2"  style="width:12%;" required />
<span style="color: red; text-align: center; font:bold 14px Arial, Helvetica, sans-serif; margin: 0px 0px 0px 5px;"><?php if($error_message) { echo $error_message; } ?></span>
<p style="color: #474747;font-family: arial;font-size: 11px;font-style: italic;">Please enter the characters displayed in the image into the adjacent input field to complete the process.</p>
</p></div>

<div class="block-con">
<input type="submit" value="Send" name="send" class="con-button" style="margin-top:0px"/>
</div>
</form>
</div>

Captcha coding:

<label class="lbl-con-text" >Captcha:</label>
<input type="hidden" name="cap1" value="<?php echo $Captcha; ?>" >
<input type="text"  readonly disabled style="width: 9%;background:url(images/pattern-icon.png) repeat-x" value="<?php echo $Captcha; ?>"/>
<input type="text" name="cap2"  style="width:12%;" required />
<span style="color: red; text-align: center; font:bold 14px Arial, Helvetica, sans-serif; margin: 0px 0px 0px 5px;"><?php if($error_message) { echo $error_message; } ?></span>
<p style="color: #474747;font-family: arial;font-size: 11px;font-style: italic;">Please enter the characters displayed in the image into the adjacent input field to complete the process.</p>
</p>

PHP Coding:

<?php 
include('connect.php'); 
$Captcha = rand("1111","9999");
if(isset($_POST['send']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$city=$_POST['city'];
$zipcode=$_POST['zipcode'];
$message=$_POST['message'];
if($_POST['cap1'] != $_POST['cap2'])
{
$error_message = "Invalid Captcha";
} else if(!$name || !$email || !$city || !$zipcode || !$message) {
$error_message = "Please enter all details";
}
else {
$insert=mysql_query("insert into contact_us(name,email,city,zipcode,message) values('$name','$email','$city','$zipcode','$message')");
$params1['name'] = $name;
$params1['email'] = $email;
$params1['city'] = $city;
$params1['zipcode'] = $zipcode;
$params1['message'] = $message;

}
}
?>

No comments:

Post a Comment