Jump to content

Php Form Using Ajax Validation


Recommended Posts

Hiii guys..I create this form validate through php, but its update using ajax to display the condition messages. However, when it gets down to the last condition statement in php, the form submitted without even displaying the error... It suppose to display the condition for "spam field"..or "human".

Thank you I would really appreciate it if someone can help me out.




$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = $_POST['human'];

if ($name=="" || $email=="" || $message==""){
echo "please filled out all required field!";
elseif ($human!=="2"){
echo "Your spam is wrong!";
echo "Congrats!";



this is ajax

function ajax_post(){
   // Create our XMLHttpRequest object
   var hr = new XMLHttpRequest();
   // Create some variables we need to send to our PHP file
   var url = "my_parse_file.php";
   var fn = document.getElementById("name").value;
   var ln = document.getElementById("message").value;
var em = document.getElementById("email").value;
var message = document.getElementById("message").value;
var human = document.getElementById("human").value;
   var vars = "name="+fn+"&message="+ln+"&email="+em+"&message="+message+"&human="+human;
   hr.open("POST", url, true);
   // Set content type header information for sending url encoded variables in the request
   hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   // Access the onreadystatechange event for the XMLHttpRequest object
   hr.onreadystatechange = function() {
    if(hr.readyState == 4 && hr.status == 200) {
	    var return_data = hr.responseText;
		document.getElementById("status").innerHTML = return_data;
   // Send the data to PHP now... and wait for response to update the status div
   hr.send(vars); // Actually execute the request
   document.getElementById("status").innerHTML = "processing...";

Link to comment
Share on other sites

Instead of echoing "success", have you tried debugging and printing out the value of $human instead?


You might also want to check what type of variable $human is. Since you're using "!==", it's strictly comparing two things, so even if both values are "2", if one is a string and one is an int, you'll get a false return if you are checking to see if they are the same.

Link to comment
Share on other sites

Thanks Ben, I figure it out the issues:


1) When I tell php to echo those condition, I didn't give instruction to prevent form submission and contact with server. Basically, I should have use some js method to stop form from submiting before validate was verified...

2) I didn't split each post variable and give its separate validation and condition

3) I am a designer first and developer second...so I sometimes code basic on how I feel not on reason or logic. ( I had logic error).



$name    = $_POST['name'];
$email   = $_POST['email'];
$message = $_POST['message'];
$phone   = $_POST['phone'];
$human   = $_POST['human'];

$email_to       = "hello@somewhere.com";
   $email_subject  = "web customer";

$formcontent = " From: $name \n email: $email \n Phone: $phone \n  Message: $message"; 

if (($name=="") || (strlen($name)< 2)){
echo "Please fill in your name...";

else if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
       echo  "E-mail address invalid!!";

else if (($message=="") || (strlen($message) >250)){
echo "Message( must be less than 250 characters)";

else if ($human !='2'){
echo 'Sorry, your math is wrong, please try again ';
//header("location: ../thanks.html");
echo 'E-mail sent!! I will try my best to reply within 24 hours ';
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $formcontent, $headers);  


If you see any security issue that I need to improve on, please let me know.


Thanks Ben for your contribution to the web community.

Edited by jack123123
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...