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 )


m 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 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 ...

Dani AI

Generated

A simple, robust approach is to validate on the client for quick feedback and always re-validate on the server before inserting/emailing. For client-side, the jQuery Validation plugin can check name/email and make an async request to verify the CAPTCHA without reloading the page.

<form id="contact">
  <input name="name" />
  <input name="email" />
  <input name="securitycode" />
  <button type="submit">Send</button>
</form>

<script>
$(function () {
  $("#contact").validate({
    rules: {
      name: { required: true },
      email: { required: true, email: true },
      securitycode: {
        required: true,
        remote: { url: "/check-captcha.php", type: "post" }
      }
    },
    messages: { securitycode: { remote: "Code does not match." } },
    submitHandler: function (form) { form.submit(); }
  });
});
</script>

The plugin’s remote rule expects your server to return "true" when the code matches, or a message string when it does not (remote method, validate() options). On the server, verify again before processing:

// check-captcha.php
session_start();
$ok = isset($_POST['securitycode'], $_SESSION['captcha'])
   && hash_equals($_SESSION['captcha'], $_POST['securitycode']);
header('Content-Type: application/json');
echo $ok ? 'true' : json_encode('Code does not match.');

// process.php (final guard)
session_start();
if (empty($_POST['name'])
 || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)
 || !hash_equals($_SESSION['captcha'], $_POST['securitycode'])) {
    http_response_code(400); exit('Invalid input');
}

If you use Google reCAPTCHA instead of a custom code, you must verify the token server-side with siteverify before accepting the submission (Google reCAPTCHA verification). PHP’s filter_var(..., FILTER_VALIDATE_EMAIL) is the recommended built-in for email format checks (PHP manual).

Recommended Answers

All 6 Replies

You can handle these checks on the client-site by Javascript.
Or you make a AJAX call to your server (which is actually a post to your server), your server is checking the input and returns the result. When the result is OK, your form can post the data. While writing I thought: this will become a very weird solution: don't build it.

Why won't you post it and check the input on the server?

Member Avatar for Member #120589

There are loads of client-side validators out there. jQuery has a good few. Another simple one is YAV. Anyway, js hijacks the submit action and on validation either returns error messages or proceeds with the submit.

You MUST have server-side validation as js can be circumvented and forms can be spoofed.

Anyone have any ideas how to solve this issues using jQuery ... Please .....

There are so many jquery form validations available, Check this both if anyone match your needs.

I think that you can make it simpler by using the validation through Java Script.
You can assign a function to the submit button. Don't set the button type as a "submit" button. For example:
HTML:

<form name="Form1">
<input type="text" name="F1">
<input type="button" onclick="Validate();" value="Submit">
</form>

Java Script:

<script language="JavaScript">
if(!Form1.F1.value)
{
     return();
}
else
{
     Form1.submit();
}
</script>
commented: in high security mode internet explorer ignores javescript +0

Hi

you can solve the problem using javascript and ajax

<form name="Form1">
<input type="text" name="name">
<input type="text" name="email">
<input type="text" name="code">
<input type="button" onclick="Validate();" value="Submit">
</form>

function check(){
if(document.forms.form1.name.value.length<=0){
alert('Please enter a name');
return false;
}else if(document.forms.form1.email.value.length<=0){
alert('Please enter your email ID');
return false;

}else if(document.forms.form1.code.value.length<=0){
alert('Please enter the code');
return false;
}

}

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.