Hi frnds ....

I am trying to create a contact form in PHP , MySql .... In my form i have two 3 input fields n a Submit Button .....


1. Name
2. email
3. SecurityCode ( here user have to enter the captcha code )


I am collecting all information frm user and if the captch code entered by the user is correct then the values will be entered into my database n emailed to some adress ....

Everything is working perfectly ....... But my problem is ...

I want to check these things before submitting the form ..

1. Name field is empty
2. User has entered a valid email
3. User has entered the correct Captcha code as shown in image

# If anything wrong i want to display the information n don't want submit or refresh the page....

# If everything is correct , then submit the form and do the actions ....


# I have searched in Google .... for some Realtime Live Ajax Validation ... But nothing is working perfectly n are too mess ....

# Can anyone tell me a simple solution or just an Idea for how to handle this ... please ...

Recommended Answers

All 16 Replies

Hi vijay..,

For your probs, u dont need to go to AJAX.. you can done with only using Javascript.. For this you need to use Hidden form... Here i can narrate this..

First you create a dummy hidden form inside the body tag with the fields that you need to pass..(i.e)

<form name="hiddenform" action="" method="get">  
// if you pass the values to another page, just mention the page name inside action tag.. otherwise leave as it is.. 

<input type="hidden" name="hidden_name">
// if you need to pass the name, create the above hidden field.. and retrieve the value by the name specified in the passing page...

</form>

Next you get the values in javascript.. suppose for user name..

var a = document.myform.name.value;

//  After validation succeed, assign like the following...

document.hiddenform.hidden_name.value = a;  

hiddenform.submit();    // This command which help us to submit the values of the hidden form...

// Do the above inside a "if" conditional loop.. if fails, you can show the error by using alert box..

Hope you can understand and it can helpful for you.. If any probs, feel free to ask me... I am always with you....

Suppose the above explaination is not enough, and if you need more clear.. just tell me.. I can help you... HTH :-)

This is the simplest form validation

<script type="text/javascript">
		function validate()
		{
			if(document.frm_validaion.user.value=="")
			{
				alert("Enter Username!");
				document.frm_validaion.user.focus();
				return false;
			}
			if(document.getElementById("txtemail").value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1)
			{
				alert("Enter valid Email!");
				document.getElementById("txtemail").focus();
				return false;
			}
		}
	</script>

Html code:

<form action="" method="post" name="frm_validaion" onsubmit="return validate();">
    	<table width="200" border="1">
          <tr>
            <td>Username</td>
            <td>:</td>
            <td><input name="user" id="user" type="text" /></td>
          </tr>
          <tr>
            <td>Email</td>
            <td>:</td>
            <td><input name="txtemail" id="txtemail" type="text" /></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td><input type="submit" name="submit" id="submit" /></td>
  		</tr>
</table>

    </form>

Hi Dragonbaki .... thanks for u r reply ... Actually the method u r telling is Javascript .... I want to use the Ajax method ... Can u tell me a most clear n best solution in ajax to handle this siruation ... please ..... m using php as my server side scripting language .....please help me ...

Ok.. Please wait.. i will tell you later.. why i prefer javascript means, this avoids the database hits and validate in the client side.. So your process will be fast and good, and your database should not go under hanging problem if you handle with large amount of datas..

k , thanks ...... but the prblm is .... if the browser is not enabled the javascript ..... it will be a mess ... right ? What's ur opinion ....

This is the simplest form validation

<script type="text/javascript">
		function validate()
		{
			if(document.frm_validaion.user.value=="")
			{
				alert("Enter Username!");
				document.frm_validaion.user.focus();
				return false;
			}
			if(document.getElementById("txtemail").value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1)
			{
				alert("Enter valid Email!");
				document.getElementById("txtemail").focus();
				return false;
			}
		}
	</script>

Html code:

<form action="" method="post" name="frm_validaion" onsubmit="return validate();">
    	<table width="200" border="1">
          <tr>
            <td>Username</td>
            <td>:</td>
            <td><input name="user" id="user" type="text" /></td>
          </tr>
          <tr>
            <td>Email</td>
            <td>:</td>
            <td><input name="txtemail" id="txtemail" type="text" /></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td><input type="submit" name="submit" id="submit" /></td>
  		</tr>
</table>

    </form>

Hi friend ... thanks for ur help ....

the prblem is i have a Captcha in my form .... How to check that also using javascript ......Using php i know ...... if the security code entered by the user is correct then only i have to submit the form ....otherwise no submit , no page refresh .... that's what i want ....

i don't think so.. So far, i didnt face that problem.. try with that, then tell me if any probs.. i will tell you about AJAX

mistake

can u just explain it using a small code fragment ...... using 1 or 2 input fields ....
.... then i can understand it more clearly .....

the prblm is m a beginner in Javascript .... commonly m using Dreamweaver for developing .... please ....that's why m not good with javascript ....

If user browser doesn't support javascript, you can check it (fields) with php.

This must be done with ajax. Here's code:

<script type="text/javascript">
function getCaptchaCode(){
if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest();
else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200)
captchaCode=xmlhttp.responseText;
}
xmlhttp.open("GET","getCaptchaCode.php",true);
xmlhttp.send();
}
</script>

And in this php file (getCaptchaCode.php):

<?php
echo $captchaCode;
?>

But I don't recomend that, because if some user knows programming, and if he only look at the code of the page, then he can get this code (he only has to open the php script getCaptchaCode.php, and he's going to get captcha code)

I have one idea with cookies

(no, it also won't work)

hey any one help me in this case ..... any one have a Stable ... secure ....Ajax Validation .......

I think I know, so, here it is:

<script type="text/javascript">
function checkCaptcha(){
//get the code that user wrote
code=document.getElementById('code').value;
if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest();
else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
if(xmlhttp.responseText!="") alert(xmlhttp.responseText);
else document.form.submit //else submit the form
}}
//send it to php file
xmlhttp.open("GET","checkCaptchaCode.php?code="+code,true);
xmlhttp.send();
}
</script>

php code:

$code=$_GET['code'];
if($captchaCode!=$code) echo "Your code is wrong!";

It's only part about captcha. You must also add the part to check name and email.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.