0

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

    <?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);
}

?>

问题是当我提交表单时,我收到一封电子邮件,上面写着

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

谁能告诉我哪里出错了

提前致谢

亚当

4

3 回答 3

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">
于 2012-10-30T17:04:34.417 回答
0

快速浏览后,您应该交换 $body 以阅读

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

试试看它是否有效

于 2012-10-30T17:06:11.373 回答
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>
于 2012-10-30T17:19:04.810 回答