简体   繁体   中英

JQuery (should not to) hides form after Submitting it

I have the tiny homepage, which works in the same window by hiding/showing necessary blocks via display block/none properties using JQuery script which is:

$(document).ready(function(){
  $(".show").click(function (){
    $(".web, .about, .tel, .email").hide();
    $("." + $(this).attr("show-class")).fadeIn("fast");
  });
});

HTML is:

<li><a href="#" class="show" show-class="email">E-mail</a></li>

<div class="email">

  <form method="post" action="">                    
  <input name="name" placeholder="Name (required)">                     
  <input name="email" type="email" placeholder="E-mail (required)">                     
  <textarea name="message" placeholder="Message"></textarea>                        
  <input name="submit" type="submit" value="Submit">                    
  </form>

  <?php

  $name = $_POST['name'];
  $email = $_POST['email'];
  $message = $_POST['message'];
  $to = 'example@example.com'; 
  $subject = 'Hi! From Example.com';

  $body = "From: $name\n E-Mail: $email\n Message:\n $message";

  if ($_POST['submit']) {
  if ($name != '' && $email != '') {
  if (mail ($to, $subject, $body)) { 
  echo '<p>Your message has been sent.</p>';
  } else { 
  echo '<p>Something went wrong, go back and try again.</p>'; 
  }
  } else {
  echo '<p>Please fill in required information.</p>';
  }
  }
  ?>

</div>

And the problem is after successfully or not, submitting the form (pressing the submit button) the block with form hides and only after pressing "E-mail" link again one of the messages (like "Your message has been sent.") appears below/along with the form.

So how could I stop the form from disappearing after submitting it?

Thank you!

Remove $(".web, .about, .tel, .email").hide(); That's what's hiding your inputs.

Adjustment to your code

$(document).ready(function() {
  $(".show").click(function() {
    $("." + $(this).attr("show-class")).fadeIn("fast");
  });
});`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM