Neha
Neha

Reputation: 1

I have 2 forms in a webpage having same code but second form doesnot work

I have a webpage which contains 2 contact form one at the banner and another at the bottom of the webpage at contact details. I have being using php for this. However the mail gets submitted of the first form but not of the second form. But in case if I remove the first form from the website, the second form works absolutely fine. I would like to know the reason for it if any body can help. You can refer to website http://www.infinitesignalsolution.com

<form role="form" id="contactForm" data-toggle="validator" class="shake">
    <div class="row">
        <div class="form-group col-sm-6">
            <label for="name" class="h4">Name</label>
            <input type="text" class="form-control" id="name" placeholder="Enter name" required data-error="NEW ERROR MESSAGE">
            <div class="help-block with-errors"></div>
        </div>
        <div class="form-group col-sm-6">
            <label for="email" class="h4">Email</label>
            <input type="email" class="form-control" id="email" placeholder="Enter email" required>
            <div class="help-block with-errors"></div>
        </div>
    </div>
     <div class="form-group">
        <label for="sub" class="h4">Sub</label>
        <input type="sub" id="sub" class="form-control" placeholder="Enter Subject" required>
        <div class="help-block with-errors"></div>
    </div>
    <div class="form-group">
        <label for="message" class="h4">Message</label>
        <textarea id="message" class="form-control" rows="5" placeholder="Enter your message" required></textarea>
        <div class="help-block with-errors"></div>
    </div>
    <button type="submit" id="form-submit" class="btn btn-success btn-lg pull-right ">Submit</button>
    <div id="msgSubmit" class="h3 text-center hidden"></div>
    <div class="clearfix"></div>
</form>

JavaScript

 $("#contactForm").validator().on("submit", function (event)
 {
     if (event.isDefaultPrevented()) {
         // handle the invalid form...
         formError();
         submitMSG(false, "Did you fill in the form properly?");
     } else {
         // everything looks good!
         event.preventDefault();
         submitForm();
     }
 });

 function submitForm()
 {
     // Initiate Variables With Form Content
     var name = $("#name").val();
     var email = $("#email").val();
     var sub = $("#sub").val();
     var message = $("#message").val();

     $.ajax({
         type: "POST",
         url: "php/form-process.php",
         data: "name=" + name + "&email=" + email + "&sub=" + sub + "&message=" + message,
         success : function(text)
                   {
                       if (text == "success"){
                           formSuccess();
                       } else {
                           formError();
                           submitMSG(false,text);
                       }
                   }
     });
 }

 function formSuccess()
 {
     $("#contactForm")[0].reset();
     submitMSG(true, "Message Submitted!")
 }

 function formError()
 {
     $("#contactForm").removeClass().addClass('shake animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
         $(this).removeClass();
     });
 }

 function submitMSG(valid, msg)
 {
     if(valid){
         var msgClasses = "h3 text-center tada animated text-success";
     } else {
         var msgClasses = "h3 text-center text-danger";
     }

     $("#msgSubmit").removeClass().addClass(msgClasses).text(msg);
 }

PHP

<?php

$errorMSG = "";

// NAME
if (empty($_POST["name"])) {
    $errorMSG = "Name is required ";
} else {
    $name = $_POST["name"];
}

if (empty($_POST["phone"])) {
    $errorMSG = "Phone No is required ";
} else {
    $phone = $_POST["phone"];
}

// EMAIL
if (empty($_POST["email"])) {
    $errorMSG .= "Email is required ";
} else {
    $email = $_POST["email"];
}

// MESSAGE
if (empty($_POST["message"])) {
    $errorMSG .= "Message is required ";
} else {
    $message = $_POST["message"];
}

$EmailFrom = "[email protected]";
$EmailTo = "[email protected]";
$Subject = "Enquiry received from website";

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $phone;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";

// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$EmailFrom);

// redirect to success page
if ($success && $errorMSG == ""){
   echo "success";
}else{
    if($errorMSG == ""){
        echo "Something went wrong :(";
    } else {
        echo $errorMSG;
    }
}

?>

Upvotes: 0

Views: 70

Answers (1)

delboy1978uk
delboy1978uk

Reputation: 12365

Do both forms have the same ID? If so, give them unique ID's, but give them a common css class.

Then refer to $(".contactForm") instead. As for the fields, you can give them a class but target the correct ones using $(this).find('.field-class') inside the submit function.

Upvotes: 1

Related Questions