提交表单不显示变量

时间:2012-10-30 17:02:14

标签: html php

当我提交表单时,会向我的电子邮件地址发送一封电子邮件,并在表单中输入详细信息,但当我收到电子邮件时,数据为空白,这是我的代码

    <?php
$form = '<form action="test2.php" method="POST">
<table width="300" style="border: 1px solid black;">
<tr>
<td>Name         &nbsp; <td><input type="text" id="name">
<tr>
<td>Phone Number &nbsp; <td><input type="text" id="telephone">
<tr>
<td colspan="2">
<input type="submit" name="submit" value="submit"/></div>
</tr>
</table>';

echo $form;
$to = 'redacted@redacted.redacted';
$name = $_POST['name'];
$telephone = $_POST['telephone'];

$body = "<div>Name: $name <br>Telephone Number: $telephone<br></div>";


// subject
$subject = 'Call Back Requested';


// message
$message = $body;


// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";


// Additional headers
$headers .= 'To:' . "\r\n";
$headers .= 'From: Call Back Request <contact-us-form@yellowgrid.co.uk>' . "\r\n";

if(isset($_POST['submit'])){

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

?>

问题是当我提交表单时,我收到一封说

的电子邮件

姓名(无) 电话号码(无)

谁能告诉我哪里出错了

提前致谢

亚当

3 个答案:

答案 0 :(得分:3)

您必须为输入指定name属性:

<input type="text" id="name" name="name">
<input type="text" id="telephone" name="telephone">

而不是

<input type="text" id="name">
<input type="text" id="telephone">

答案 1 :(得分:0)

快速查看后,您应该将$ body换成

$body = '<div>Name: ' . $name . '<br>Telephone Number: ' . $telephone . '<br></div>';

尝试一下,看看它是否有效

答案 2 :(得分:0)

你在关闭标签时有很多错误..以及使用echo显示整个html表单的不良做法..看看这段代码:

<?php

    extract($_POST);

    if ( isset($submit) )
    {
        $to = 'adam.albison@yellowgrid.co.uk';

        $body = "<div>Name: $name <br>Telephone Number: $telephone<br></div>";
        $subject = 'Call Back Requested';
        $message = $body;

        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= 'To:' . "\r\n";
        $headers .= 'From: Call Back Request <contact-us-form@yellowgrid.co.uk>' . "\r\n";

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

?>

<html>
    <form action="test2.php" method="POST">
        <table width="300" style="border: 1px solid black;">
            <tr> 
                <td>Name &nbsp; </td>
                <td> <input type="text" id="name" name="name" /> </td>
            </tr>
            <tr>
                <td>Phone Number &nbsp; </td>
                <td> <input type="text" id="telephone" name="telephone" /> </td>
            </tr>
            <tr>
                <td colspan="2"> <input type="submit" name="submit" value="submit"/> </td>
            </tr>
    </form>
</html>