0

我正在构建一个 php 联系表单,用于检查是否填写了某些字段(没有 ajax)。表格不能 100% 工作。我会尽力解释。当我提交表单时,用户导航到http://mydomain.com/send.php 收到我的错误消息(如果有的话)。当我刷新 send.php 页面时,我收到以下错误:注意:未定义索引:/Library/WebServer/Documents/~aledvertising/send.php 中的名称

这是我的代码

html表单

<form method="post" action="send.php">
    <div id="form-top">

    </div>
    <div id="form-left">  
<label>Naam:<span class="star">*</span></label>
    <input name="name" placeholder="Uw naam">


    <label>Email:<span class="star">*</span></label>
    <input name="email" type="email" placeholder="Uw email">

    <label>Hoeveel is 2+2? (Anti-spam)<span class="star">*</span></label>
<input name="human" placeholder="Uw antwoord">

    </div>
    <div id="form-right">
    <label>Uw bericht:<span class="star">*</span></label>
    <textarea name="message" placeholder="Uw bericht"></textarea>
    </div>
        <input id="submit" name="submit" type="submit" value="Verzenden">
</form>

发送.PHP

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: ' . $_POST['email']; 
    $to  = 'myemail@mail.com' . ', '; 
    $to .= $_POST['email'];
    $subject = 'Uw vraag op www.aledvertising.be';
    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\nMessage:\n $message";
 ?>
 <?php
if ($_POST['submit']) {
    if ($name != '' && $email != '' && $message != '') {
        if ($human == '4') {                 
            if (mail ($to, $subject, $body, $from)) { 
            echo '<p class="green">Uw bericht is succesvol verzonden.</p>';
        } else { 
            echo '<p>Er iets misgelopen, probeer opnieuw aub.</p>'; 
        } 
    } else if ($_POST['submit'] && $human != '4') {
        echo '<p>2+2 is niet gelijk aan het getal dat u hebt ingevoerd.</p>';
    }
    } else {
        echo '<p>Alle velden met een * zijn verplicht in te vullen.</p>';
    }
}
    ?>

有人可以帮我获得一份完整的工作表格吗?谢谢

4

2 回答 2

3

你应该看到来自 php.ini 的isset方法。

<?php
if(isset($_POST["name"]))
{
//code here

}
?>

问题是,当您重新加载页面时,服务器端代码会查找索引name,但它没有找到它,因为它还name没有任何值。提交页面后会收到该值。所以这就是错误存在的原因。

更新

错误在于,当您提交表单时,有些字段可能会发送为空,因此一旦发布值转到 send.php,就会有 $_POST 期望值。如果他们中的一些人没有接受价值,它会告诉你:“嘿,我没有价值,所以我是空的”。这就是为什么您必须检查所有字段是否都已填写或在服务器端进行验证的原因。

<?php
if(isset($_POST["name"],$_POST["last_name"],etc,etc))
{
//if everything ok, ill go on.
}
else{
//if there are empty field, go back
}

?>

更新 2

if (isset($_POST['name'],$_POST['email'],$_POST['message'])){

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: ' . $_POST['email']; 
    $to  = 'myemail@mail.com' . ', '; 
    $to .= $_POST['email'];
    $subject = 'Uw vraag op www.aledvertising.be';
    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\nMessage:\n $message";
    if ($name != '' && $email != '' && $message != '') {
        if ($human == '4') {                 
            if (mail ($to, $subject, $body, $from)) { 
            echo '<p class="green">Uw bericht is succesvol verzonden.</p>';
        } else { 
            echo '<p>Er iets misgelopen, probeer opnieuw aub.</p>'; 
        } 
    } else if ($_POST['submit'] && $human != '4') {
        echo '<p>2+2 is niet gelijk aan het getal dat u hebt ingevoerd.</p>';
    }
    } else {
        echo '<p>Alle velden met een * zijn verplicht in te vullen.</p>';
    }
}
于 2013-04-30T16:25:10.723 回答
0
<?php
if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: ' . $_POST['email']; 
    $to  = 'myemail@mail.com' . ', '; 
    $to .= $_POST['email'];
    $subject = 'Uw vraag op www.aledvertising.be';
    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\nMessage:\n $message";
    if ($name != '' && $email != '' && $message != '') {
        if ($human == '4') {                 
            if (mail ($to, $subject, $body, $from)) { 
            echo '<p class="green">Uw bericht is succesvol verzonden.</p>';
        } else { 
            echo '<p>Er iets misgelopen, probeer opnieuw aub.</p>'; 
        } 
    } else if ($_POST['submit'] && $human != '4') {
        echo '<p>2+2 is niet gelijk aan het getal dat u hebt ingevoerd.</p>';
    }
    } else {
        echo '<p>Alle velden met een * zijn verplicht in te vullen.</p>';
    }
}
?>

设置 isset() 函数,然后声明变量对我有用,在我的服务器中对其进行测试,没有给出任何错误并且表单已成功提交...

请检查一次...

于 2013-04-30T16:45:50.027 回答