php - 为什么我在文本框中得到奇怪的值?

标签 php mysql

这段代码有什么问题,它在主题和消息文本框中显示奇怪的值,但它们应该是空的。服务器给我 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/

相关文章:

javascript - 如何在选择选择框值时将参数作为 POST 数据传递,这将重新加载同一页面?

mysql - ASP.NET MVC 应用程序的数据库和逻辑层

php - PDO SQLite 无法绑定(bind) NOT IN 表达式

php - 如何让用户使用 PHP 和 MySQL 添加多个标签?

php - HttpURLConnection 从 .php 文件返回垃圾

php - Laravel 4 - 渲染递归@include

php - Laravel 查询错误 - 调用未定义的方法 Illuminate\Database\Query\Builder::query()

sql - "select max"和 "select.. where column=max"有什么区别?

C# MySQL 连接池限制,以及清理连接

mysql - 如何编写bat文件来执行.sql文件