此代码中出现了什么错误,它在主题和消息文本框中显示出奇怪的值,但它们应该为空。服务器给我未定义的变量错误调用堆栈#TimeMemoryFunctionLocation的东西。请看一下,我是一个php初学者。请帮我摆脱文本框中的那些值。他们应该是空的。
<?php
if (isset($_POST['submit'])) {
$from = 'drstoic@xyz.com';
$subject = $_POST['subject'];
$text = $_POST['newsletter_content'];
$output_form = false;
if (empty($subject) && empty($text)) {
// We know both $subject AND $text are blank
echo 'You forgot the email subject and body text.<br />';
$output_form = true;
}
if (empty($subject) && (!empty($text))) {
echo 'You forgot the email subject.<br />';
$output_form = true;
}
if ((!empty($subject)) && empty($text)) {
echo 'You forgot the email body text.<br />';
$output_form = true;
}
}
else {
$output_form = true;
}
if ((!empty($subject)) && (!empty($text))) {
$dbc = mysqli_connect('localhost', 'root', 'a12345', 'pocr')
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']; ?>"
<label for="subject">Subject:</label>
<input type="text" name="subject" id="subject" class="input_field"
value="<?php echo $subject; ?>" />
<div class="cleaner h10"></div>
<label for="newsletter_content">Message:</label>
<textarea id="newsletter_content" name="newsletter_content" rows="0" cols="0"><?php echo $text; ?></textarea>
<div class="cleaner h10"></div>
<input type="submit" value="Send" id="submit" name="submit" class="submit_btn float_l" />
<input type="reset" value="Reset" id="reset" name="reset" class="submit_btn float_r" />
</form>
<?php
}
?>
答案 0 :(得分:0)
<input type="text" name="subject" id="subject" class="input_field" value="<?php echo $subject; ?>" />
在此特定情况下,如果未定义$_POST['submit']
,则不会声明该值。 PHP将打印警告而不是您期望的内容(空白字符串)。您需要在if (isset($_POST['submit']))
语句上方初始化类似这些变量。
$ _ POST仅在表单提交时定义,因此在第一个视图中,您将看到警告而不是空字符串。