提交电子邮件表单后,我希望保持同一页面和成功的信息给apear

时间:2017-06-09 10:15:59

标签: php html

我希望在提交电子邮件信息后,保持在同一页面上,而不是将我重定向到新的。

这是我的HTML代码:

view.backgroundColor = UIColor.clear
view.isOpaque = false

这是PHP代码:

<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>

2 个答案:

答案 0 :(得分:0)

你可以使用一个框架来使用ajax调用,这使得ajax像jQuery或AngularJS一样简单。在这种情况下,在服务器端,您应该创建REST APi端点以发送电子邮件。

答案 1 :(得分:0)

试试这样==&gt;将php和html代码放在同一个文件中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>