使用echo $ variablename时,表单变量无法打印

时间:2011-12-30 20:28:40

标签: php html

我正在创建一个读取用户名和电子邮件的表单,然后通过PHP文件向他们发送凭证,如下所示:

<?php

  $deal = "offer or discount details";

  $to = "$email";
  $subject = "Tonights wheree voucher - $deal";
  $message = '
  <html>
    <body>
        Test email sent to <?php echo $email; ?>.
    </body>
  </html>
  ';

  $headers = "From: wheree <no-reply@wheree.co.uk>" . "\r\n" .
             "Content-type: text/html" . "\r\n";

  mail($to, $subject, $message, $headers);

?>

表格:

<form class="cmxform" id="details-form" method="get" action="test.php">
                 <fieldset>
                       <p>
                         <input id="name" name="name" title="Please enter your full name. " size="18" class="tagcheck" minlength="5" maxlength="60" value="Name" onfocus="if(this.value==this.defaultValue){this.value=''};" onblur="if(this.value==''){this.value=this.defaultValue};" />
                       </p>
                       <p>
                         <input id="email" name="email" size="18" title="Please enter a valid email address. " class="required email" value="Email" onfocus="if(this.value==this.defaultValue){this.value=''};" onblur="if(this.value==''){this.value=this.defaultValue};" />
                       </p>
                       <p class="tickbox-wrapper">
                       <input id="accept-tick" type="checkbox" name="vehicle" class="required" title="You must accept our Terms & Conditions. " /> I accept the voucher usage <a href="/voucher-terms-and-conditions" alt="terms" class="terms-link">Terms & Conditions</a>.
                       </p>

                       <div id="RegisterErrors" style="display:none"></div>

                       <p>
                         <input class="submit" type="submit" value="Continue"/>
                       </p>
                 </fieldset>
</form>

这样可以正常工作,但不会在收到的电子邮件中回显变量$email吗?我也尝试使用$_GET["email"],但再次没有成功。

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:5)

您正尝试在单引号字符串中放置echo语句。将其连接在:

  $message = '
  <html>
    <body>
        Test email sent to ' . $email . '.
    </body>
  </html>
  ';

或者更好的是,切换到$message的双引号字符串,其中$email可以插补。

  $message = "
  <html>
    <body>
        Test email sent to $email.
    </body>
  </html>
  ";

有关变量插值的更多信息以及单引号和双引号之间的区别,请参阅PHP manual on strings

如果你有很多变量和多行,那么第三个选项就是HEREDOC syntax,这样可以完全避免引用。

  $message =<<<MESSAGE
  <html>
    <body>
        Test email sent to $email.
    </body>
  </html>
MESSAGE;
// Note that the MESSAGE; above MUST be at the start of its own line, with no indentation

答案 1 :(得分:1)

单引号内有$echo个。 PHP中的单引号中不会扩展变量。试着把它放在双引号中。或者,如同您的建议,如果您想使用单引号,请将其连接起来。

$foo = "HELLO";
echo 'Foo: $foo';       // Prints 'Foo: $foo'
echo 'Foo: ' . $foo;    // Prints 'Foo: HELLO'
echo "Foo: $foo";       // Prints 'Foo: HELLO'

答案 2 :(得分:0)

试试这个:

<?php

  $deal = "offer or discount details";
  $to = $_GET['email'];
  $subject = "Tonights wheree voucher - $deal";
  $message = '
  <html>
    <body>
        Test email sent to '.$to.'.
    </body>
  </html>
  ';

  $headers = "From: wheree <no-reply@wheree.co.uk>" . "\r\n" .
             "Content-type: text/html" . "\r\n";

  mail($to, $subject, $message, $headers);

?>