Anasayfa » Ajax - Jquery » ajax iletişim formu

ajax iletişim formu

ajax iletişim formu Google Buzz Ekle

Ajax ile görsellik açısından zengin birçok form türevi gerçekleştirilebilir.Bu makalemizde ajax ile iletişim formunun nasıl yapılabileceğini göreceğiz.boş kalan kısımlar için uyarı verme gibi özellikleride bulunuyor.

406 ajax iletişim formu

Form omurgası için kullanacağımız html kodları şu şekildedir;

<div id="main-container">	<!-- The main container element -->
<div id="form-container">	<!-- The form container -->
<h1>Fancy Contact Form</h1>	<!-- Headings -->
<h2>Drop us a line and we will get back to you</h2>
<form id="contact-form" name="contact-form" method="post" action="submit.php">	<!-- The form, sent to submit.php -->
<table width="100%" border="0" cellspacing="0" cellpadding="5">
<tr>
<td width="18%"><label for="name">Name</label></td>	<!-- Text label for the input field -->
<td width="45%"><input type="text" class="validate[required,custom[onlyLetter]]" name="name" id="name" value="<?=$_SESSION['post']['name']?>" /></td>
<!-- We are using session to prevent losing data between page redirects -->
<td width="37%" id="errOffset">&nbsp;</td>
</tr>
<tr>
<td><label for="email">Email</label></td>
<td><input type="text" class="validate[required,custom[email]]" name="email" id="email" value="<?=$_SESSION['post']['email']?>" /></td>
<td>&nbsp;</td>
</tr>
<tr>
<td><label for="subject">Subject</label></td>
<!-- This select is being replaced entirely by the jqtransorm plugin -->
<td><select name="subject" id="subject">
<option value="" selected="selected"> - Choose -</option>
<option value="Question">Question</option>
<option value="Business proposal">Business proposal</option>
<option value="Advertisement">Advertising</option>
<option value="Complaint">Complaint</option>
</select>          </td>
<td>&nbsp;</td>
</tr>
<tr>
<td valign="top"><label for="message">Message</label></td>
<td><textarea name="message" id="message" class="validate[required]" cols="35" rows="5"><?=$_SESSION['post']['message']?></textarea></td>
<td valign="top">&nbsp;</td>
</tr>
<tr>
<td><label for="captcha"><?=$_SESSION['n1']?> + <?=$_SESSION['n2']?> =</label></td>
<!-- A simple captcha math problem -->
<td><input type="text" class="validate[required,custom[onlyNumber]]" name="captcha" id="captcha" /></td>
<td valign="top">&nbsp;</td>
</tr>
<tr>
<td valign="top">&nbsp;</td>
<!-- These input buttons are being replaced with button elements -->
<td colspan="2"><input type="submit" name="button" id="button" value="Submit" />
<input type="reset" name="button2" id="button2" value="Reset" />
<?=$str?>
<!-- $str contains the error string if the form is used with JS disabled -->
<img id="loading" src="img/ajax-load.gif" width="16" height="16" alt="loading" />
<!-- the rotating gif animation, hidden by default -->
</td></tr>
</table>
</form>
<?=$success?>
<!-- The $success variable contains the message that is shown if JS is disabled and the form is submitted successfully -->
</div>
</div>	<!-- closing the containers -->

i13 ajax iletişim formu

formun eklendiği sayfanın yani demo.php sayfasının

<head></head>

tagları arasına aşağıdakileri ekliyoruz.

<link rel="stylesheet" type="text/css" href="jqtransformplugin/jqtransform.css" />
<link rel="stylesheet" type="text/css" href="formValidator/validationEngine.jquery.css" />
<link rel="stylesheet" type="text/css" href="demo.css" />
<?=$css?> <!-- Special CSS rules, created by PHP -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="jqtransformplugin/jquery.jqtransform.js"></script>
<script type="text/javascript" src="formValidator/jquery.validationEngine.js"></script>
<script type="text/javascript" src="script.js"></script>

üst kısımda eklediğimiz script.js dosyasının kodları şu şekildedir

$(document).ready(function(){
	/* after the page has finished loading */
	$('#contact-form').jqTransform();
	/* transform the form using the jqtransform plugin */
	$(&quot;button&quot;).click(function(){
		$(&quot;.formError&quot;).hide();
		/* hide all the error tooltips */
	});
	var use_ajax=true;
	$.validationEngine.settings={};
	/* initialize the settings object for the formValidation plugin */
	$(&quot;#contact-form&quot;).validationEngine({	/* create the form validation */
		inlineValidation: false,
		promptPosition: &quot;centerRight&quot;,
		success :  function(){use_ajax=true},	/* if everything is OK enable AJAX */
		failure : function(){use_ajax=false}	/* in case of validation failure disable AJAX */
	 })
	$(&quot;#contact-form&quot;).submit(function(e){
			if(!$('#subject').val().length)
			{
				$.validationEngine.buildPrompt(&quot;.jqTransformSelectWrapper&quot;,&quot;* This field is required&quot;,&quot;error&quot;)
				/* a custom validation tooltip, using the buildPrompt method */
				return false;
			}
			if(use_ajax)
			{
				$('#loading').css('visibility','visible');
				/* show the rotating gif */
				$.post('submit.php',$(this).serialize()+'&amp;ajax=1',
				/* using <a href="/tag/jquery"target="_self"rel="nofollow"title="Jquery" >jQuery</a>'s post method to send data */
				function(data){
					if(parseInt(data)==-1)
						$.validationEngine.buildPrompt(&quot;#captcha&quot;,&quot;* Wrong verification number!&quot;,&quot;error&quot;);
						/* if there is an error, build a custom error tooltip for the captcha */
					else
					{
						$(&quot;#contact-form&quot;).hide('slow').after('&lt;h1&gt;Thank you!&lt;/h1&gt;');
						/* show the confirmation message */
					}
					$('#loading').css('visibility','hidden');
					/* hide the rotating gif */
				});
			}
e.preventDefault();	/* stop the default form submit */
})
});

chart11 ajax iletişim formu

Css dosyamıza aşağıdaki kodları ekliyoruz

body,h1,h2,h3,p,quote,small,form,input,ul,li,ol,label{
	/* reset some of the page elements */
	margin:0px;
	padding:0px;
}
body{
	color:#555555;
	font-size:13px;
	background: url(img/dark_wood_texture.jpg) #282828;
	font-family:Arial, Helvetica, sans-serif;
}
.clear{
	clear:both;
}
#main-container{
	width:400px;
	margin:30px auto;
}
#form-container{
	background-color:#f5f5f5;
	padding:15px;
	/* rounded corners */
	-moz-border-radius:12px;
	-khtml-border-radius: 12px;
	-webkit-border-radius: 12px;
	border-radius:12px;
}
td{
	/* prevent multiline text */
	white-space:nowrap;
}
a, a:visited {
	color:#00BBFF;
	text-decoration:none;
	outline:none;
}
a:hover{
	text-decoration:underline;
}
h1{
	color:#777777;
	font-size:22px;
	font-weight:normal;
	text-transform:uppercase;
	margin-bottom:5px;
}
h2{
	font-weight:normal;
	font-size:10px;
	text-transform:uppercase;
	color:#aaaaaa;
	margin-bottom:15px;
	border-bottom:1px solid #eeeeee;
	margin-bottom:15px;
	padding-bottom:10px;
}
label{
	text-transform:uppercase;
	font-size:10px;
	font-family:Tahoma,Arial,Sans-serif;
}
textarea{
	color:#404040;
	font-family:Arial,Helvetica,sans-serif;
	font-size:12px;
}
td > button{
	/* A special CSS selector that targets non-IE6 browsers */
	text-indent:8px;
}
.error{
	/* this class is used if JS is disabled */
	background-color:#AB0000;
	color:white;
	font-size:10px;
	font-weight:bold;
	margin-top:10px;
	padding:10px;
	text-transform:uppercase;
	width:300px;
}
#loading{
	/* the loading gif is hidden on page load */
	position:relative;
	bottom:9px;
	visibility:hidden;
}
.tutorial-info{
	color:white;
	text-align:center;
	padding:10px;
	margin-top:10px;
}

Demo.php dosyamız

session_name("fancyform");
session_start();
$_SESSION['n1'] = rand(1,20);	/* generate the first number */
$_SESSION['n2'] = rand(1,20);	/* then the second */
$_SESSION['expect'] = $_SESSION['n1']+$_SESSION['n2'];	/* the expected result */
/* the code below is used if JS has been disabled by the user */
$str='';
if($_SESSION['errStr'])	/* if submit.php returns an error string in the session array */
{
	$str='<div class="error">'.$_SESSION['errStr'].'</div>';
	unset($_SESSION['errStr']);	/* will be shown only once */
}
$success='';
if($_SESSION['sent'])
{
	$success='<h1>Thank you!</h1>';	/* the success message */
	$css='<style type="text/css">#contact-form{display:none;}</style>';
	/* a special CSS rule that hides our form */
	unset($_SESSION['sent']);
}

Submit.php dosyamız

require "phpmailer/class.phpmailer.php";
session_name("fancyform");	/* starting the session */
session_start();
foreach($_POST as $k=>$v)
{
	/* if magic_quotes is enabled, strip the post array */
	if(ini_get('magic_quotes_gpc'))
	$_POST[$k]=stripslashes($_POST[$k]);
	$_POST[$k]=htmlspecialchars(strip_tags($_POST[$k]));
	/* escape the special chars */
}
$err = array();
/* some error checks */
if(!checkLen('name'))
	$err[]='The name field is too short or empty!';
if(!checkLen('email'))
	$err[]='The email field is too short or empty!';
else if(!checkEmail($_POST['email']))
	$err[]='Your email is not valid!';
if(!checkLen('subject'))
	$err[]='You have not selected a subject!';
if(!checkLen('message'))
	$err[]='The message field is too short or empty!';
/* compare the received captcha code to the one in the session array */
if((int)$_POST['captcha'] != $_SESSION['expect'])
	$err[]='The captcha code is wrong!';
/* if there are errors */
if(count($err))
{
	/* if the form was submitted via AJAX */
	if($_POST['ajax'])
	{
		echo '-1';
	}
	/* else fill the SESSION array and redirect back to the form */
	else if($_SERVER['HTTP_REFERER'])
	{
		$_SESSION['errStr'] = implode('<br />',$err);
		$_SESSION['post']=$_POST;
		header('Location: '.$_SERVER['HTTP_REFERER']);
	}
	exit;
}
/* the email body */
$msg=
'Name:	'.$_POST['name'].'<br />
Email:	'.$_POST['email'].'<br />
IP:	'.$_SERVER['REMOTE_ADDR'].'<br /><br />
Message:<br /><br />
'.nl2br($_POST['message']).'
';
$mail = new PHPMailer();	/* using PHPMailer */
$mail->IsMail();
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->AddAddress($emailAddress);
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->Subject = "A new ".mb_strtolower($_POST['subject'])." from ".$_POST['name']." | contact form feedback";
$mail->MsgHTML($msg);
$mail->Send();
unset($_SESSION['post']);	/* unsetting */
/* the form was successfully sent */
if($_POST['ajax'])
{
	echo '1';
}
else
{
	$_SESSION['sent']=1;
	if($_SERVER['HTTP_REFERER'])
		header('Location: '.$_SERVER['HTTP_REFERER']);
	exit;
}
/* some helpful functions */
function checkLen($str,$len=2)
{
	return isset($_POST[$str]) && mb_strlen(strip_tags($_POST[$str]),"utf-8") > $len;
}
function checkEmail($str)
{
	return preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $str);
}
download
demo
Tarih : 23 Kasım 2009
Kategori : Ajax - Jquery
Etiketler : , ,
rss aboneliği

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

ajax iletişim formu için 2 Yorum

  1. Glupaja_Devo4ka diyor ki:

    I want to quote your post in my blog. It can?
    And you et an account on Twitter?

    CevaplaCevapla
  2. admin diyor ki:

    Yes you are.Our Twitter account is http://twitter.com/faydaliweb

    CevaplaCevapla

Yorum yapın

Copyright FaydalıWeb | Internetin Faydalı Yüzü | Powered By WordPress | Yazılar (RSS) | Yorumlar (RSS) Design By Htworks | İletişim / Contact