Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6198771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:04:24+00:00 2026-05-24T04:04:24+00:00

I have a jQuery AJAX form and I’d like for it to do better

  • 0

I have a jQuery AJAX form and I’d like for it to do better validation on input – on the PHP side. Here is the PHP script that works with AJAX:

while (true) {
if (empty($_POST['name'])) {
    $return['error'] = true;
    $return['msg'] = 'You did not enter your name.';
    break;
}

if (empty($_POST['email'])) {
    $return['error'] = true;
    $return['msg'] = 'You did not enter your email.';
    break;
}

if (empty($_POST['message'])) {
    $return['error'] = true;
    $return['msg'] = 'You did not enter your message.';
    break;
}

break;
}

if (!$return['error'])
    $return['msg'] = "Thank you: {$_POST['name']}<br />email address: {$_POST['email']}<br />Your Message: {$_POST['message']}";


echo json_encode($return);

And here is the PHP script I use elsewhere for validation:

<?php

$subject = "Website Contact Form Enquiry";

//If the form is submitted

if(isset($_POST['submit'])) {

//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
    $hasError = true;
} else {
    $name = trim($_POST['contactname']);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '')  {
    $hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
    $hasError = true;
} else {
    $email = trim($_POST['email']);
}

//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
    $hasError = true;
} else {
    if(function_exists('stripslashes')) {
        $comments = stripslashes(trim($_POST['message']));
    } else {
        $comments = trim($_POST['message']);
    }
}

//If there is no error, send the email
if(!isset($hasError)) {
    $emailTo = 'info@bgv.co.za'; //Put your own email address here
    $body = "Name: $name \n\nEmail: $email \n\nComments:\n $comments";
    $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

    mail($emailTo, $subject, $body, $headers);
    $emailSent = true;

}
}
?>

Please can someone who know their PHP help me – I dont have a clue how to put this together.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:04:24+00:00Added an answer on May 24, 2026 at 4:04 am

    try like this. i think this code alone working good for what you want.

    $subject = "Website Contact Form Enquiry";
    
    //If the form is submitted
    
    if(isset($_POST['submit'])) {
    
    //Check to make sure that the name field is not empty
    if(trim($_POST['contactname']) == '') {
        $hasError = true;
        $return['msg'] = 'You did not enter your name.';
    } else {
        $name = trim($_POST['contactname']);
    }
    
    //Check to make sure sure that a valid email address is submitted
    if(trim($_POST['email']) == '')  {
        $hasError = true;
        $return['msg'] = 'You did not enter your email.';
    } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
        $hasError = true;
        $return['msg'] = 'You did not enter valid email.';
    } else {
        $email = trim($_POST['email']);
    }
    
    //Check to make sure comments were entered
    if(trim($_POST['message']) == '') {
        $hasError = true;
       $return['msg'] = 'You did not enter your message.';
    } else {
        if(function_exists('stripslashes')) {
            $comments = stripslashes(trim($_POST['message']));
        } else {
            $comments = trim($_POST['message']);
        }
    }
    
    //If there is no error, send the email
    if(!isset($hasError)) {
        $emailTo = 'info@bgv.co.za'; //Put your own email address here
        $body = "Name: $name \n\nEmail: $email \n\nComments:\n $comments";
        $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
    
        mail($emailTo, $subject, $body, $headers);
        $emailSent = true;
    
    }else{
      echo json_encode($return);
    
     }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a jquery ajax form. i have validation at server side for repeated
I have a form that uses jQuery to submit an ajax post and it
I have some jQuery code. I have called an Ajax function file, file.php, that
I have a jQuery ajax function and would like to submit an entire form
I have a jquery Ajax call function that I use to submit form to
i have this jQuery-AJAX code below and a form: <script type=text/javascript> $(document).ready(function () {
I have a form that uses a jQTools overlay and jQuery .ajax submission routine.
I have a jquery/ajax form that has event.preventDefault() . The problem is that my
I have a jQuery ajax script to process a form on submit. However, I
I have written jQuery code, in files Main.html and ajax.php . The ajax.php file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.