javascript - Submit button not working in Bootstrap form

210

I have a form in Bootstrap 3 that's inside a modal. There's a button called "submit" where when it's clicked the stuff that was entered in the form should be sent to an email address. Although when I click "Submit" nothing happens.

Does anyone know why this happens? Here's the form:

<div id="contactPop" tabindex="-1" role="dialog" aria-labelledby="contactModal" aria-hidden="true">
   <div >
    <div >
      <div >
       <!-- Close button. -->
       <button type="button" data-dismiss="modal" aria-hidden="true">&times;</button>
       <!-- Title. -->
       <h3 >
         Contact Us <small > You matter to us.</small>
       </h3>
      </div>

      <!-- User input fields. -->
      <form action="process.php" method="post">
      <div >
       <div >
         <div >
          <label >Subject</label>
          <input type="text" required="required" placeholder="I aciddentally the website!" name="subject">
         </div>
         <div >
          <label >Username</label>
          <input type="text" required="required" placeholder="Notch" name="username">
         </div>
       </div>
       <br>

       <div >
         <div >
          <label >Message</label>
          <textarea rows="8" required="required" placeholder="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eget fermentum justo, sit amet semper." name="message"></textarea>
         </div>

         <div >
          <label >Email</label>
          <input type="email" required="required" placeholder="[email protected]" name="email">
         </div>

         <div >
          <br>
          <p>Responses are usually received within 1-2 business days.</p>
          <p>Please be as clear and concise as possible, in order to help us process your inquiry faster.</p>
         </div>
       </div>
      </div>

      <!-- Close & submit buttons. -->
      <div >
       <button type="button" data-dismiss="modal"><i ></i> Close</button>
       <button type="button" value=" Send" type="submit" id="submit"><i ></i> Submit</button>
      </div>
    </div>
   </div>
 </div>

There's also the process.php that sends the form, which is here:

<?php
//add the recipient's address here
$myemail = '[email protected]';

//grab named inputs from html then post to #thanks
if (isset($_POST['name'])) {
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$message = strip_tags($_POST['message']);
echo "<span class=\"alert alert-success\" >Your message has been received, and we will get                 back to you as soon as possible. Here is what you submitted:</span><br><br>";
echo "<stong>Name:</strong> ".$name."<br>";   
echo "<stong>Email:</strong> ".$email."<br>"; 
echo "<stong>Message:</strong> ".$message."<br>";

//generate email and send!
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email";
mail($to,$email_subject,$email_body,$headers);
}
?>

And then in the index there's also the javascript that is supposed to submit the form.

 <script type="text/javascript">
     $(document).ready(function () {
$("input#submit").click(function(){
    $.ajax({
        type: "POST",
        url: "process.php", //process to mail
        data: $('form.contact').serialize(),
        success: function(msg){
            $("#thanks").html(msg) //hide button and show thank you
            $("#form-content").modal('hide'); //hide popup  
        },
        error: function(){
            alert("failure");
        }
    });
});
});
  </script>

If someone could help that would be greatly appreciated!

526

Answer

Solution:

Your problem is this

<button type="button" value=" Send" class="btn btn-success" type="submit" id="submit" />

You've set the type twice. Your browser is only accepting the first, which is "button".

<button type="submit" value=" Send" class="btn btn-success" id="submit" />
106

Answer

Solution:

Replace this

 <button type="button" value=" Send" class="btn btn-success" type="submit" id="submit">

with

<button  value=" Send" class="btn btn-success" type="submit" id="submit">
364

Answer

Solution:

  • If you puttype=submit it is a Submit Button
  • if you puttype=button it is just a button, It does not submit your form inputs.

and also you don't want to use both of these

140

Answer

Solution:

The .btn classes are designed for <button> , <a> or <input> elements (though some browsers may apply a slightly different rendering).

If you’re using .btn classes on elements that are used to trigger functionality ex. collapsing content, these links should be given a role="button" to adequately communicate their meaning to assistive technologies such as screen readers. I hope this help.

People are also looking for solutions to the problem: php - I m not getting proper balanced days from below code:

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.