我有PHP联系表单,我希望消息发送或失败的消息显示在主页面内..我在提交按钮后有标签id =“notification”我希望显示send.php的消息在里面。
我的代码是:
<form method="post" action="send.php">
<label>Name</label>
<input name="name" placeholder="Type Here">
<label>Email</label>
<input name="email" type="email" placeholder="Type Here">
<label>Subject</label>
<input name="subject" placeholder="Type Here">
<label>Message</label>
<textarea name="message" placeholder="Type Here"></textarea>
<button id="submit" name="submit" type="submit">Dergo</button>
<h3 id="notification" align="center" style="font-color:green;"></h3>
</form>
Send.php代码:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$from = 'From: KontaktFormular';
$to = 'albanianboy2@gmail.com';
$headers = "From:" . $email . "\r\n";
$headers .= "Reply-To: ".$email."\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8" . "\r\n";
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if(@mail($to,$subject,$body,$headers))
{
echo "Mesazhi juaj u dërgua me sukses";
}else{
echo "Gabim! Ju lutemi provoni përsëri.";
}
?>
答案 0 :(得分:0)
更改代码的这一部分:
}else{
echo "Gabim! Ju lutemi provoni përsëri.";
}
为:
}else{
header('Location: mainpage.php?msg=1');
}
然后在主页上,您可以:
<?php
if(isset($_GET['msg']) && $_GET['msg'] = 1)
{
?>
<h3 id="notification" align="center" style="font-color:green;">
Gabim! Ju lutemi provoni përsëri.
</h3>
<?php
}
?>
答案 1 :(得分:0)
将您的代码放在PHP文件中。 你的代码:
<?php session_start(); ?>
<form method="post" action="send.php">
<label>Name</label>
<input name="name" placeholder="Type Here">
<label>Email</label>
<input name="email" type="email" placeholder="Type Here">
<label>Subject</label>
<input name="subject" placeholder="Type Here">
<label>Message</label>
<textarea name="message" placeholder="Type Here"></textarea>
<button id="submit" name="submit" type="submit">Dergo</button>
<h3 id="notification" align="center" style="font-color:green;">
<?php
if(!empty($_SESSION["notification"])){
echo $_SESSION["notification"];
unset($_SESSION["notification"];
}
?>
</h3>
</form>
您的send.php文件:
<?php
session_start();
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$from = 'From: KontaktFormular';
$to = 'albanianboy2@gmail.com';
$headers = "From:" . $email . "\r\n";
$headers .= "Reply-To: ".$email."\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8" . "\r\n";
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if(@mail($to,$subject,$body,$headers))
{
$_SESSION["notification"] = "Mesazhi juaj u dërgua me sukses";
}else{
$_SESSION["notification"] = "Gabim! Ju lutemi provoni përsëri.";
}
header("location: YOUR_PHP_FILE_WITH_THE_FORM ");
?>
答案 2 :(得分:0)
消息
$notification = "put your message";
Javascript:
// Appeding notification to Body
$('#notification').html($notification);
执行php检查时执行此操作。
把这个css放在首位:
#notification{
display: none;
}
设置你的div
<div id="notification">