这段代码有什么问题,它在主题和消息文本框中显示奇怪的值,但它们应该是空的。服务器给我 undefined variable 错误调用堆栈#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
}
?>
最佳答案
<input type="text" name="subject" id="subject" class="input_field" value="<?php echo $subject; ?>" />
在此特定情况下,如果未定义 $_POST['submit']
,则不会声明该值。 PHP 将打印一条警告,而不是您期望的内容(空白字符串)。您需要初始化 if (isset($_POST['submit']))
语句上面的变量。
$_POST 仅在提交表单时定义,因此在第一次查看时,您将看到警告而不是空字符串。
关于php - 为什么我在文本框中得到奇怪的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22541290/