从另一个PHP文件获取值
我在WordPress中使用PHPMailer发送邮件。为此,我有3个文件: contacts.php-带有html表单的文件:从另一个PHP文件获取值,php,Php,我在WordPress中使用PHPMailer发送邮件。为此,我有3个文件: contacts.php-带有html表单的文件: <?php /* * Template Name: Contacts */ get_header(); ?> <div class="container page"> <h2>Напиши нам</h2> <div class="row"> <div cla
<?php
/*
* Template Name: Contacts
*/
get_header(); ?>
<div class="container page">
<h2>Напиши нам</h2>
<div class="row">
<div class="col-lg-5">
<form action="<?php bloginfo('template_url');?>/mailer.php" method="post">
<label for="fname">Имя*</label>
<input type="text" class="form-control" id="fname" name="first-name" required>
<label for="tel">Телефон*</label>
<input type="tel" class="form-control" id="tel" required name="phone">
<label for="tel">E-mail</label>
<input type="tel" class="form-control" id="email" name="email">
<label for="">Сообщение*</label>
<textarea required class="form-control" rows="8" name="msg"></textarea>
<button type="submit" class="btn btn-success" id="btn-contacts">Отправить</button>
</form>
</div>
<div class="col-lg-7">
</div>
</div>
</div>
<?php get_footer(); ?>
Напиши нам
test.php-字母的html标记:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<h5>First Name of Person:</h5>
<?php
echo $fname; //how can I get this variable value???
?>
</body>
</html>
你好
姓名:
我面临着一个问题。
我怎么能得到这个?(请参阅第三个代码段和其中的注释)。提前谢谢你 您可以在您的
POST
请求中将其存储在会话中,然后显示它。此外,默认情况下,在WP会话中不启用。您需要像这样启用它们
if (!session_id()) {
session_start();
}
$\u SESSION['firstName']=$\u POST['first-name'];//存储
$body = <<<EOD
....
<h5>First Name of Person:</h5>
$fname
....
EOD;
$body=您可以使用herdoc语法定义html文本和变量。file_get_内容只需读取文件的文本源并存储在变量中。您必须在需要的地方包含此文件,并使用heredoc语法:
$body = <<<HTML
<h5>First Name of Person:</h5>
{$fname}
HTML;
对所有要替换的值执行此操作。谢谢!但它不起作用。我在mailer.php中添加了这一行:$\u SESSION['firstName']=$\u POST['first-name'];//存储
之后,在test.php中,我尝试使用以下命令:Hello人名:
我收到一条空行,其中echo是您使用session_start()启用会话的id代码>?最好的方法!非常感谢。
$body = <<<HTML
<h5>First Name of Person:</h5>
{$fname}
HTML;
<h5>First Name of Person:</h5>
{first_name}
$body = file_get_contents("test.php");
$body = str_replace("{first_name}",$fname,$body);