我创建了一个简单的联系表单,提交表单时,变量$ result输出未出现在该表单上方。请帮助我。此联系表格在相同的php文件中可以正常工作,但是当我尝试另一个php文件(以我的情况为fetch.php)时,index.php的h4标签中的变量$ result提交表格后未显示任何输出。
index.php
<?php
include 'fetch.php';
?>
<!DOCTYPE html>
<!-- -->
<html>
<head>
<meta charset="UTF-8">
<title>PHPMailer Contact Form</title>
<style>
label{
display: block;
}
</style>
</head>
<body>
<h4> <?php echo $result; ?> </h4> <!-- here is the problem -->
<form method="post" action="fetch.php">
<label>Name</label>
<input type="text" name="name"><br>
<label>E-mail</label>
<input type="email" name="email"><br>
<label>Subject</label>
<input type="text" name="subject"><br>
<label>Message</label>
<textarea rows="5" cols="40" name="message"></textarea><br>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
fetch.php
<?php
/*
* To change this license header, choose License Headers in
Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//These lines must be at the top script
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$result=""; // this is the variable i asked about
if(isset($_POST['submit']))
{
$uname=$_POST['name'];
$uemail=$_POST['email'];
$usub=$_POST['subject'];
$umsg=$_POST['message'];
require 'vendor/autoload.php'; //composer's autoloader
$mail = new PHPMailer; //creat object
//Server settings
$mail->Host = 'smtp.gmail.com'; //SMTP server
$mail->isSMTP(); //set mailer to use smtp
$mail->SMTPAuth = true; //enable authentication
$mail->Username = 'my@gmail.com'; //smtp user name
$mail->Password = 'secrete'; // smtp password
$mail->SMTPSecure = 'tls'; //Enable TLS encryption
$mail->Port = 587; //Tcp port to connect
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'From Form :'.$usub; //subject
$mail->Body= '<h3>'
.'Name :'.$uname
.'<br>E-mail :'.$uemail
.'<br>Message :'.$umsg
. '</h3>';`enter code here`
//Recipients
$mail->setFrom($uemail,$uname);
$mail->addAddress('my@gmail.com', 'to User');
$mail->addReplyTo($uemail,$uname);
if(!$mail->send()) //send mail
{
$result = "Message could not be sent.";
}
else
{
$result ="Thank you ".$uname." for contacting us.";
}
}
答案 0 :(得分:0)
您有两个选择,
1。使用会话
在fetch.php中将此代码放在末尾 p>
$ _SESSION['result'] = $result ;
然后您可以访问
$_SESSION['result'];
在index.php中
为了初始化会话,放置
session_start();
在两个文件的顶部。
2。网址参数
在fetch.php中最后添加
header('Location: index.php?result='.$result);
然后您可以按以下方式在index.php中访问此url参数,
$_GET['result'];
index.php
<?php
if(!session_id()) session_start();
?>
<!DOCTYPE html>
<!-- -->
<html>
<head>
<meta charset="UTF-8">
<title>PHPMailer Contact Form</title>
<style>
label{
display: block;
}
</style>
</head>
<body>
<h4> <?php if(isset($_SESSION['result'])){ echo $_SESSION['result']; $_SESSION['result']=''; } ?> </h4> <!-- here is the problem -->
<form method="post" action="fetch.php">
<label>Name</label>
<input type="text" name="name"><br>
<label>E-mail</label>
<input type="email" name="email"><br>
<label>Subject</label>
<input type="text" name="subject"><br>
<label>Message</label>
<textarea rows="5" cols="40" name="message"></textarea><br>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
fetch.php
<?php
/*
* To change this license header, choose License Headers in
Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//These lines must be at the top script
if(!session_id()) session_start();
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$result=""; // this is the variable i asked about
if(isset($_POST['submit']))
{
$uname=$_POST['name'];
$uemail=$_POST['email'];
$usub=$_POST['subject'];
$umsg=$_POST['message'];
require 'vendor/autoload.php'; //composer's autoloader
$mail = new PHPMailer; //creat object
//Server settings
$mail->Host = 'smtp.gmail.com'; //SMTP server
$mail->isSMTP(); //set mailer to use smtp
$mail->SMTPAuth = true; //enable authentication
$mail->Username = 'my@gmail.com'; //smtp user name
$mail->Password = 'secrete'; // smtp password
$mail->SMTPSecure = 'tls'; //Enable TLS encryption
$mail->Port = 587; //Tcp port to connect
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'From Form :'.$usub; //subject
$mail->Body= '<h3>'
.'Name :'.$uname
.'<br>E-mail :'.$uemail
.'<br>Message :'.$umsg
. '</h3>';`enter code here`
//Recipients
$mail->setFrom($uemail,$uname);
$mail->addAddress('my@gmail.com', 'to User');
$mail->addReplyTo($uemail,$uname);
if(!$mail->send()) //send mail
{
$result = "Message could not be sent.";
}
else
{
$result ="Thank you ".$uname." for contacting us.";
}
$_SESSION['result'] = $result;
}
header('Location: index.php');