簡體   English   中英

PHP中的Xampp錯誤(定義變量?)

[英]Xampp error in php (defining variables?)

經過2天的嘗試自己解決問題后,我放棄了:(所以將在這里尋求幫助。我的問題是xampp給了我這個錯誤:

注意:未定義索引:第78行的C:\\ xampp \\ htdocs \\ portfolio \\ index.php中的名稱注意:未定義索引:第79行的C:\\ xampp \\ htdocs \\ portfolio \\ index.php中的電子郵件注意:未定義索引:message在第80行的C:\\ xampp \\ htdocs \\ portfolio \\ index.php中注意:未定義的索引:在第84行的C:\\ xampp \\ htdocs \\ portfolio \\ index.php中是人類注意:未定義的索引:在C:\\ xampp \\中提交htdocs \\ portfolio \\ index.php在第88行

我設法找到了在使用它們之前需要定義變量或檢查它們是否存在的能力,但是我對PHP的了解是非常基礎的。 有人可以幫我嗎?

<section id="contact">
<h3>Contact me :</h3>
<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: Example'; 
    $to = 'example@gmail.com'; 
    $subject = 'Hello';
    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit']) {
    if ($name != '' && $email != '') {
        if ($human == '4') {                 
            if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        } 
    } else if ($_POST['submit'] && $human != '4') {
        echo '<p>You answered the anti-spam question incorrectly!</p>';
    }
    } else {
        echo '<p>You need to fill in all required fields!!</p>';
    }
}
?>

<form method="post" action="index.php">

    <label>Name</label>
    <input name="name" placeholder="Type Here">

    <label>Email</label>
    <input name="email" type="email" placeholder="Type Here">

    <label>Message</label>
    <textarea name="message" placeholder="Type Here"></textarea>
    <label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">

    <input id="submit" name="submit" type="submit" value="Submit">

</form>


</section>

由於沒有POST請求,因此$ _POST ['name']之類的變量不可用,因此PHP給出了該錯誤。 您需要做的是檢查是否發出了$ _POST請求,然后定義變量:

<?php
if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: Example'; 
    $to = 'example@gmail.com'; 
    $subject = 'Hello';
    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit']) {
    if ($name != '' && $email != '') {
        if ($human == '4') {                 
            if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        } 
    } else if ($_POST['submit'] && $human != '4') {
        echo '<p>You answered the anti-spam question incorrectly!</p>';
    }
    } else {
        echo '<p>You need to fill in all required fields!!</p>';
    }
}
}
?>

當前,您的頁面上正在顯示這些消息,因為您正在顯示所有錯誤和通知。

如果在頁面頂部添加以下內容,則不會看到通知消息:

<?php error_reporting (E_ALL ^ E_NOTICE);  ?>

但是請注意,這只會隱藏消息。 它不能解決根本的問題。

暫無
暫無

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

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