简体   繁体   中英

After submitting email form, I want to stay on the same page and message like success to apear

I want after submitting the email information, to stay on the same page, instead of redirecting me to new one.

THis is my html code:

<form method="post" action="index.php" id="formaa" accept-charset="UTF-8">

    <input name="name" placeholder="Name" id="aa">
    <input name="email" type="email" placeholder="E-mail" id="aa">      
    <textarea name="message" placeholder="Message"></textarea>      
    <input name="human" placeholder="2+2=" id="ab">
    <input id="submit" name="submit" type="submit" value="Submit">

</form>

This is the PHP code:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$headers = 'From: PappuLighting'; 
$to = 'v.karageorgiev@pappu-lighting.com'; 
$subject = 'Hello';
$human = $_POST['human'];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

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

if ($_POST['submit'] && $human == '4') {                 
if (mail ($to, $subject, $body, $headers)) { 
echo '<p>Your message has been sent!</p>';
} else { 
echo '<p>Something went wrong, go back and try again!</p>'; 
} 
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>

you can use ajax calls by using one frameworks that makes ajax simple like jQuery or AngularJS. in that case in the server side you should create REST APi endpoint to send email.

Try like this ==> put the php and html code in same file and action=""

<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$headers = 'From: PappuLighting'; 
$to = 'v.karageorgiev@pappu-lighting.com'; 
$subject = 'Hello';
$human = $_POST['human'];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

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

if ($_POST['submit'] && $human == '4') {                 
if (mail ($to, $subject, $body, $headers)) { 
$messageSuccess =  '<p>Your message has been sent!</p>';
} else { 
$messageerror =  '<p>Something went wrong, go back and try again!</p>'; 
} 
} else if ($_POST['submit'] && $human != '4') {
$messageerror = '<p>You answered the anti-spam question incorrectly!</p>';
}
}
?>

    <?php  
    if(isset($messageSuccess))
    echo $messageSuccess;

   else if(isset($messageSuccess))
    echo $messageerror;
    ?>

<form method="post" action="" id="formaa" accept-charset="UTF-8">

    <input name="name" placeholder="Name" id="aa">
    <input name="email" type="email" placeholder="E-mail" id="aa">      
    <textarea name="message" placeholder="Message"></textarea>      
    <input name="human" placeholder="2+2=" id="ab">
    <input id="submit" name="submit" type="submit" value="Submit">

</form>

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