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

  • Home
  • SEARCH
  • 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 7853669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:45:59+00:00 2026-06-02T19:45:59+00:00

So I have a form with both javascript validation to check each field is

  • 0

So I have a form with both javascript validation to check each field is filed and a jquery ajax script to stop the page reloading.

The PHP script is very simple and only emails the fields of the form.
The problem is I need to ONLY show the thank you message if each field is filled out, and at the moment it appears to be showing it when I just enter the name field.
It doesn’t show it if i just enter either the email or message field though and I can’t work out why (I’m bad at PHP).

<?php 



$name = $_POST['name'];
$email = $_POST['email'];
$priority = $_POST['priority'];
$message = $_POST['message'];
$telephone = $_POST['telephone'];
$formcontent="From: $name \n Email: $email \n Telephone Number: $telephone \n Priority: $priority \n Message: $message";
$recipient = "myemail@address.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

if(mail($name,$email,$message,$headers)){
    echo "<p>Thanks for your mail...</p>";
}


?>

Javascript that stops page reloading.

<script type="text/javascript">

        $(document).ready(function() {

            $(".contactus").submit(function() {

                $.post("mail.php", $(".contactus").serialize(),

                    function(data) {
                        $("#formResponse").html(data);
                    }
                );
                return false;
            });
        });
    </script>

Form validation.

<script type="text/javascript" language="JavaScript">


var FormName = "contactus";



var RequiredFields = "name,email,priority,message";



function ValidateRequiredFields()
{
var FieldList = RequiredFields.split(",")
var BadList = new Array();
for(var i = 0; i < FieldList.length; i++) {
    var s = eval('document.' + FormName + '.' + FieldList[i] + '.value');
    s = StripSpacesFromEnds(s);
    if(s.length < 1) { BadList.push(FieldList[i]); }
    }
if(BadList.length < 1) { return true; }
var ess = new String();
if(BadList.length > 1) { ess = 's'; }
var message = new String('\n\nThe following field' + ess + ' are required:\n');
for(var i = 0; i < BadList.length; i++) { message += '\n' + BadList[i]; }
alert(message);
return false;
}

function StripSpacesFromEnds(s)
{
while((s.indexOf(' ',0) == 0) && (s.length> 1)) {
    s = s.substring(1,s.length);
    }
while((s.lastIndexOf(' ') == (s.length - 1)) && (s.length> 1)) {
    s = s.substring(0,(s.length - 1));
    }
if((s.indexOf(' ',0) == 0) && (s.length == 1)) { s = ''; }
return s;
}
// -->
</script>

And the form

<form action="mail.php" id="myform" class="contactus" onsubmit="return ValidateRequiredFields(); this.submit(); this.reset(); return false;"   name="contactus" method="POST">
<p class="floatleft" style="width:200px; background-color:#FF0000; height:50px; line-height:50px; margin:0; padding:0;">Nameeeeee</p> <input class="sizetext" type="text" maxlength="10" name="name">
<div style="clear:both"></div>
<p class="floatleft" style="width:200px;">Email</p> <input class="sizetext" type="text" maxlength="10" name="email">
<div style="clear:both"></div>
<p class="floatleft" style="width:200px;">Telephone</p> <input class="sizetext" type="text" maxlength="10" name="telephone">


<p>Priority</p>
<select name="priority" size="1">
<option value="Low">Low</option>
<option value="Normal">Normal</option>
<option value="High">High</option>
<option value="Emergency">Emergency</option>
</select>

</select>
<p>Message</p><textarea name="message" rows="6" cols="25"></textarea><br />

<br />
<input class="buttonstyle" type="submit" value="Send">
</form>
  • 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-06-02T19:46:00+00:00Added an answer on June 2, 2026 at 7:46 pm
    <?php
    
    $name = $_POST['name'];
    $email = $_POST['email'];
    $priority = $_POST['priority'];
    $message = $_POST['message'];
    $telephone = $_POST['telephone'];
    
    $check = array('name', 'email', 'priority', 'message', 'telephone');
    $send = true;
    
    foreach($check as $key => $value){
        if(empty($_POST[$value])){
            $send = false;
        }
    }
    
    
    $formcontent="From: $name \n Email: $email \n Telephone Number: $telephone \n Priority: $priority \n Message: $message";
    $recipient = "myemail@address.co.uk";
    $subject = "Contact Form";
    $mailheader = "From: $email \r\n";
    
    if($send && mail($recipient, $subject, $formcontent, $mailheader)){
        echo "<p>Thanks for your mail...</p>";
    }
    else{
        die('Error!');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a field in my form labeled Name that will contain both the
I have a form with two input textboxes, and I have included jQuery validation
Form validation with jQuery is as easy as adding a classname to a field.
I have an ASP .NET page with both asp .net validators and some javascript
I have a form which takes both the user details and an image uploaded
Background, Part 1 I have a form that collects both frequency and duration from
I have a form and a sub-form, both with some controls that are bound
I have created a form that is used for both adding and editing a
I am writing an app which needs to have both the traditional form of
I have the following regex: ^[a-zA-Z](.*)[a-zA-Z]$ on both the Javascript and PHP side that

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.