php - PHP联系表格没有从另一个php文件中获取所有信息
问题描述
我创建了一个简单的联系表单,其中在提交表单时变量 $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.";
}
}
解决方案
你有两个选择,
1.使用会话
在 fetch.php 中把这段代码放在最后
$ _SESSION['result'] = $result ;
然后你就可以访问
$_SESSION['result'];
在 index.php 中
为了初始化会话放
session_start();
在两个文件的顶部。
2.url参数
在 fetch.php 最后添加这个
header('Location: index.php?result='.$result);
然后就可以在 index.php 中访问这个 url 参数,如下所示,
$_GET['result'];
索引.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>
获取.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');
推荐阅读
- ecmascript-6 - 从存储多个函数的导出常量中显式导入一个函数
- javascript - 在 confirm() 方法中单击“取消”与单击“确定”具有相同的结果
- image - Flutter 使用 InteractiveViewer 裁剪图像
- java - 如何在 android 中实现 Paypal Native Checkout SDK?
- java - 即使在编码后写入标准输入时,Python 'A bytes like object is required'
- powershell - 如何提高迭代循环性能
- java - 自动化多个链接并在 Selenium 中验证正确重定向和损坏页面的最佳方法,无需上传 Excel
- apache - .htaccess 重写规则行为错误
- react-native - React Native 中的 Appium setTimeout 端到端测试
- mysql - 在mysql中使用窗口函数创建重复排名