基本"表格提交"码

时间:2014-05-27 06:08:56

标签: php forms notice

我浏览了各种论坛,寻找解决我面临的问题的方法。我遇到了其他人面临的一些类似问题,但他们的代码解决方案似乎与我的代码无关。如果有人能指出我正确的方向,我将不胜感激。

这是我的浏览器向我显示的通知:

  

注意:未定义的变量:第69行的C:\ wamp \ www ..... \ sendemail4.php中的主题       Stack #TimeMemoryFunctionLocation 10.0010247784 {main}().. \ sendemail4.php:0">

这里是Head First php mysql第4章中我试图运行的代码 -

 <html>
    <head>
    <title> Make Me Elvis - Send Email </title>
    </head>
    <body>
    <p><strong>Private:</strong> For Elmer's use only <br />
        Write and send an email to mailing list numbers.</p>

    <?php

    //Detecting form submission  using isset() function 
    //Making forms sticky 

    if (!empty($_POST['submit'])) {

        $from = 'prasadwalvekar@yahoo.com';
        $subject = $_POST['subject'];
        $text = $_POST['elvismail'];
        $output_form = false;

        if( empty($subject) && empty($text) ) {
            //Both $subject and $text are blank
            echo 'You forgot the email subject and body text';
            $output_form = true;

        }

        if( empty($subject) && !empty($text) ) {
            //$subject is empty and $text is not empty
            echo 'You forgot email subject.';
            $output_form = true;
        }

        if( !empty($subject) && empty($text) ) {
            //$subject is not empty and $text is empty
            echo 'You forgot email body text.';
            $output_form = true;
        }
    }

    else {

        $output_form = true;    //if form's never been submitted, show it!
    }

    if((!empty($subject)) && (!empty($text))) {
        $dbc = mysqli_connect('localhost', 'root', '', 'elvis_store')
            or die('Error connecting to MySQL server.');

        $query = "SELECT * FROM email_list";
        $result = mysqli_query($dbc, $query)
            or die('Error querying database');

        while ($row = mysqli_fetch_array($result)) {
            $to = $row['email'];
            $first_name = $row['first_name'];
            $last_name = $row['last_name'];
            $msg = "Dear $first_name $last_name, \n $text";
            mail($to, $subject, $msg, 'From:' . $from);
            echo 'Email sent to: ' . $to . '<br />';
        }
        mysqli_close($dbc);
    }

    if ($output_form) {
    ?>

    <form method="post" action="<? php echo $_SERVER['PHP_SELF']; ?>">
        Subject of email: <input type="text" name="subject" value=" <?php echo                $subject; ?> "><br />
        Body of email: <textarea name = "elvismail" rows="8" cols="40"><?php echo    $text; ?></textarea><br /> <br />
        <input type = "submit" name = "submit" />
    </form>

    <?php

    }
    ?>

    </body>
</html>

1 个答案:

答案 0 :(得分:0)

尝试仅在设置时回显表单内的变量。否则,它将显示错误,因为在加载表单时第一次没有定义错误(这些变量将仅在提交表单时定义)。您可以使用isset()

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        Subject of email: <input type="text" name="subject" value="<?php if(isset($subject)) { echo $subject; } ?>"><br />
        Body of email: <textarea name = "elvismail" rows="8" cols="40"><?php if(isset($text)) { echo $text; } ?></textarea><br /> <br />
        <input type="submit" name="submit" />
</form>