简体   繁体   中英

How do I display message on the same page?

I have a quick question about my form. After submitting the form you will be directed to a page followed by the text that I put in. The problem is that I want this text to be displayed on the same page as where the form is on (named contact.html) I use two files, one is the Mail_handler.php and the other contact.html. I've tried multiple things to fix it, but for some reason, I have no success. I hope that you guys can help me out! Below you can find the HTML and PHP.

 <form method="POST" action="mail_handler.php"> <div class="col-sm-7 slideanim"> <div class="row"> <div class="col-sm-6 form-group"> <input class="form-control" id="name" name="name" placeholder="Naam" type="text" required> </div> <div class="col-sm-6 form-group"> <input class="form-control" id="phone" name="phone" placeholder="Telefoonnummer" type="text" required> </div> <div class="col-sm-12 form-group"> <input class="form-control" id="email" name="email" placeholder="Email" type="email" required> </div> </div> <textarea class="form-control" id="msg" name="msg" placeholder="Bericht" rows="5"></textarea><br> <div class="row"> <div class="col-sm-12 form-group"> <button class="btn btn-default pull-right" id="submit" name="submit" type="submit">Verstuur</button> </div> </div> </div> </div> </div> </div> </form> 

<?php
if(isset($_POST['submit'])){
    $name=$_POST['name'];
    $email=$_POST['email'];
    $phone=$_POST['phone'];
    $msg=$_POST['msg'];

    $to='infosyncdevelopment@gmail.com'; // Receiver Email ID, Replace with your email ID
    $subject='Form Submission';
    $message="Name :".$name."\n"."Phone :".$phone."\n"."Wrote the following :"."\n\n".$msg;
    $headers="From: ".$email;

    if(mail($to, $subject, $message, $headers)){


        echo "<h1>Bedankt voor uw bericht!"." ".$name.", Wij nemen zo snel mogelijk contact met u op.</h1>";
    }
    else{
        echo "Something went wrong!";
    }
}

?>

the best way is to use ajax, but if you may not do that, you can do this little trick:

change contact.html into php script (maybe contact.php)

<?php
    if(isset($_GET['msg'])) echo $_GET['msg'];
?>
<form method="POST" action="mail_handler.php">
      <div class="col-sm-7 slideanim">
      <div class="row">
        <div class="col-sm-6 form-group">
          <input class="form-control" id="name" name="name" placeholder="Naam" type="text" required>
          </div>
          <div class="col-sm-6 form-group">
          <input class="form-control" id="phone" name="phone" placeholder="Telefoonnummer" type="text" required>
        </div> 
          <div class="col-sm-12 form-group">
          <input class="form-control" id="email" name="email" placeholder="Email" type="email" required>
        </div>
      </div>
      <textarea class="form-control" id="msg" name="msg" placeholder="Bericht" rows="5"></textarea><br>
      <div class="row">
        <div class="col-sm-12 form-group">
         <button class="btn btn-default pull-right" id="submit" name="submit" type="submit">Verstuur</button>
        </div>
      </div>
    </div>
  </div>
</div> 
        </div>
    </form> 

in the mail_handler.php, change echo to $msg

<?php
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$msg=$_POST['msg'];

$to='infosyncdevelopment@gmail.com'; // Receiver Email ID, Replace with your email ID
$subject='Form Submission';
$message="Name :".$name."\n"."Phone :".$phone."\n"."Wrote the following :"."\n\n".$msg;
$headers="From: ".$email;

if(mail($to, $subject, $message, $headers)){


    $msg = "<h1>Bedankt voor uw bericht!"." ".$name.", Wij nemen zo snel mogelijk contact met u op.</h1>";
}
else{
    $msg = "Something went wrong!";
}

header("Location: contact.php?$msg");

} ?>

Use AJAX

    $("form").submit(function(e) {
    e.preventDefault();
    var name = $('#name').val(); 
    // do this for all other input tags
        $.post("file.php" , {x:name , .....} , function(data){
              // do stuff with data , here data is the things echoed/printed by 'file.php'
        });
    });

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