2016-07-20 19 views
-2

我看過幾十篇關於這個問題的文章,它基本上歸結爲一個變量沒有被聲明或給定的值。不過,我100%肯定它是相同的,並宣佈。使用textarea在PHP中未定義的索引

我在HTML中有一個基本的聯繫表單,我希望它在有人點擊提交按鈕時發送給我和發送電子郵件。我正在調試代碼以查看問題所在。它可以找到的唯一問題是有一個Undefined Index屬於我的文本區域。

我知道textarea的名稱必須與我在PHP中的$_POST上的名稱相同。請看看這兩段代碼,告訴我你是否可以看到爲什麼它不會從我的textarea中獲取信息。名字是message-area

HTML

<form action="mail.php" method="post" name=contact-me-form > 
    <label name="firstname secondname">Name: *&nbsp;</label><br> 
    <input class="half-box" type="text" name="firstname" required > 
    <input class="half-box" type="text" name="secondname" required ><br> 
    <p class="first-name">First Name</p> 
    <p class="second-name">Last Name</p><br> 
    <label name="email">Email Address: *&nbsp;</label><br> 
    <input class="full-box" type="email" name="email" spellcheck="false" required><br> 
    <label name="subject">Subject:&nbsp;</label><br> 
    <input class="full-box" type="text" name="subject"><br> 
    <label name="message">Message: *&nbsp;</label><br> 
    <textarea name="message-area" form="contact-me-form" type="text" placeholder="Please enter your message"></textarea> 
    <button name="submit" type="submit" value="Submit">Submit</button> 
</form> 

PHP

<?PHP 

    $to = ""; 
    $from = ""; 
    $first_name = ''; 
    $last_name = ''; 
    $subject = ''; 
    $message = null; 

error_reporting(-1); 
ini_set('display_errors', 'On'); 
set_error_handler("var_dump"); 

if(isset($_POST['submit'])){ 
     $to = '[email protected]'; 
     $from = $_POST['email']; 
     $first_name = $_POST['firstname']; 
     $last_name = $_POST['secondname']; 
     $subject = $_POST['subject']; 
     $message = $_POST["message-area"]; 
if($message == null){echo "no message detected";} 

$headers = "From: " . $from; 
$headers = "From:" . $to; 
mail($to,$subject,$message,$headers); 
} 

?> 

正如你所看到的名稱是相同但是當我提交上來顯示以下數據。

INT(8)的字符串(29) 「未定義指數:消息區」 的字符串(58) 「/愛馬仕/ bosnaweb25a/b2294 /後跟一個位的更多信息和獲取顯示我的錯誤:[」 消息」 ] => NULL}無檢測消息。

老實說,我不知道這是爲什麼不被拾起,任何人只要有更多的經驗可以突出顯示我的錯誤?

編輯1

這與此無關破折號/連字符,因爲我編輯了我的代碼,如下所示。 也需要注意的是,如果我將其更改爲原始文本,它仍然不起作用,仍然表現爲沒有來自textarea的數據。

HTML

    <form action="mail.php" method="post" id=contact-me-form > 
        <label name="firstname secondname">Name: *&nbsp;</label><br> 
        <input class="half-box" type="text" name="firstname" required > 
        <input class="half-box" type="text" name="secondname" required ><br> 
        <p class="first-name">First Name</p> 
        <p class="second-name">Last Name</p><br> 
        <label name="email">Email Address: *&nbsp;</label><br> 
        <input class="full-box" type="email" name="email" spellcheck="false" required><br> 
        <label name="subject">Subject:&nbsp;</label><br> 
        <input class="full-box" type="text" name="subject"><br> 
        <label name="message">Message: *&nbsp;</label><br> 
        <textarea name="messagearea" type="text" placeholder="Please enter your message"></textarea> 
        <button name="submit" type="submit" value="Submit">Submit</button> 
       </form> 

PHP

<?PHP 

$to = ""; 
$from = ""; 
$first_name = ''; 
$last_name = ''; 
$subject = ''; 
$message = null; 

error_reporting(-1); 
ini_set('display_errors', 'On'); 
set_error_handler("var_dump"); 

if(isset($_POST['submit'])){ 
    $to = '[email protected]'; 
    $from = $_POST['email']; 
    $first_name = $_POST['firstname']; 
    $last_name = $_POST['secondname']; 
    $subject = $_POST['subject']; 
    $message = $_POST["messagearea"]; 
if($message == null){echo "no message detected";} 

$headers = "From: " . $from; 
$headers = "From:" . $to; 
mail($to,$subject,$message,$headers); 
} 

?> 

EDIT 2

用硬編碼值工作,因此必須是變量名進行了測試。奇怪的是它只發生在textarea上。

+0

提示:如果某些項目*被*提交而其他項目不是,請仔細查看它們之間的區別,並消除該差異。 – deceze

+0

嘗試將'message-area'重命名爲不帶連字符的名稱。 – Marki555

+0

謝謝你們,看看。我已經從我的HTML和PHP中刪除了連字符/短劃線,但是現在仍然是同樣的錯誤,表示「未定義索引:messagearea」羞愧仍然會出現問題。 – CardDeath

回答

0

你看起來如果將其更改爲以下它應該工作有在<textarea></textarea>

隨機form=""屬性。

<textarea name="message-area" type="text" placeholder="Please enter your message"></textarea> 

對於上不($message == null)工作$消息的檢查,你應該使用empty()來檢查它,將捕獲NULL""

if (empty($message)) { 
    echo "no message detected"; 
} 
+0

一般來說'form'屬性不是問題;當屬性引用的表單不​​存在時,這只是一個問題。在這種情況下,這是因爲表單具有'name = contact-me-form',當它需要是一個*'id' *。 - 所以,缺乏解釋,但它*是問題。 – deceze

+1

從文本區域中刪除了表單屬性,我將contact-me-form更改爲了一個id,但仍然沒有結果。 – CardDeath

0

這是您的解決方案,只需將您的文本區域代碼替換爲下面的行。

<textarea name="message-area" placeholder="Please enter your message"></textarea> 

這裏它不需要form="contact-me-form"文本。

+0

刪除了這個和type =「text」,但仍然沒有結果。感謝您的嘗試。 – CardDeath

0

謝謝你的幫助,我發現問題,它似乎工作。我還沒有收到電子郵件(可能是可怕的越南互聯網)。

問題是我的變量定義爲$variable = ""沒有設置爲空,因爲我認爲。我不得不通過並將= null分配給我的所有變量,它似乎繞過了錯誤。