簡體   English   中英

提交表單不顯示變量

[英]Submitting Form Not Displaying Variables

當我提交表單時,會將一封電子郵件發送至我的電子郵件地址,並在表單中輸入詳細信息,但是當我收到該電子郵件時,數據為空白,這是我的代碼

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

?>

問題是當我提交表格時,我收到一封電子郵件,內容為

姓名(無)電話號碼(無)

誰能告訴我我要去哪里錯了

提前致謝

亞當

您必須為輸入指定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">

快速瀏覽后,您應該交換您的$ body以閱讀

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

試試看,看看是否可行

您在關閉標簽..時有很多錯誤,並且不好的做法是使用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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM