来自文本框的PHP变量未传递给电子邮件消息

时间:2016-02-26 05:33:33

标签: php html variables

我正在尝试发送包含用户用户名和密码的电子邮件,但用户名不是,而密码

PHP:

<?php
if(!isset($_POST['submit']))

$user = $_POST['username'];
$pass = $_POST['password'];
$email = $_POST['email'];

$email_from = 'Me@email.com';
$email_subject = "Username and Password for you";
$email_body = "Username: $user \n". 
"Password: $pass \n".

$to = $email;
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_from \r\n";

mail($to,$email_subject,$email_body,$headers);

?>  

HTML:

<!DOCTYPE html>
<html>
<head>
<title>Login Form in PHP with Session</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<form action="db.php" method="post">
  First name:<br>
  <input type="text" name="username" readonly value='<?php echo $random_first .'_'. $random_second; ?>'><br>
  Last name:<br>
  <input type="text" name="password" readonly value='<?php echo $pw; ?>'><BR>
  Enter your email to get a copy of these!<br>
  <input type="text" name="email"><BR>
  <input type ="submit" value = "submit" name="submit">
</form>
</body>

生成的电子邮件:

Username:
Password: hG31cZbTL

我现在不知所措。用户名将按照应有的格式生成。我可能只是错过了一些小东西,需要其他眼睛才能看到它。

我有一个脚本在页面加载时生成随机用户名和密码,因此用户名和密码值中的变量。它们很长,并且认为它们不适用于这个特定的问题,因此将它们排除在外。

4 个答案:

答案 0 :(得分:0)

将您的PHP代码更改为:

<?php
if(isset($_POST['submit'])) // did changes here
{
   $user = $_POST['username'];
   $pass = $_POST['password'];
   $email = $_POST['email'];

   $email_from = 'Me@email.com';
   $email_subject = "Username and Password for you";
   $email_body = "Username: $user \n". 
   "Password: $pass \n".

   $to = $email;
   $headers = "From: $email_from \r\n";
   $headers .= "Reply-To: $visitor_from \r\n";

   mail($to,$email_subject,$email_body,$headers);
}
?>  

答案 1 :(得分:0)

应该是includecode,用于在html页面中生成随机变量

<强> HTML

<?php 
 require_once 'your file';
 // or code 
?>
<!DOCTYPE html>
<html>
<head>
<title>Login Form in PHP with Session</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<强> PHP

if(isset($_POST['username'],$_POST['password'],$_POST['email']))
{
 $user = $_POST['username'];
 $pass = $_POST['password'];
 $email = $_POST['email'];
}

答案 2 :(得分:0)

只需在代码中替换下面的below行。

$email_body = "Username: $user <br/> Password: $pass ";

$email_body = "Username: ".$user." <br/> Password: ".$pass;

答案 3 :(得分:-1)

我尝试了上面的建议,但他们根本就停止了发送的电子邮件。

我删除了if(!isset($_POST['submit'])),它完美无缺!去图。

感谢您的所有投入,我感谢您的所有时间。

编辑:圣牛!好吧,出于某种原因,我没有看到你们中的任何人从if(!isset($_POST['submit']))移除了爆炸(!)。因为大括号一直被指出我完全错过了爆炸,我猜!

我已经删除了爆炸,现在它可以正常工作,并且可以安全地从想要通过空白电子邮件淹没我的服务器的天网机器人。

再次感谢大家。