可能重复:
PHP:"Notice:Undefined variable"answers"Notice:Undefined index">
我是PHP的新手,正在尝试使用它。我的PHP文件中有以下代码。
$output = "<div style='display:none'>
<div class='contact-top'></div>
<div class='contact-content'>
<h1 class='contact-title' style='text-align:center'>Write a Testimonial:</h1>
<div class='contact-loading' style='display:none'></div>
<div class='contact-message' style='display:none'></div>
<form action='#' style='display:none'>
<label for='contact-name'>*Name:</label>
<input type='text' id='contact-name' class='contact-input' name='name' tabindex='1001' />
<label for='contact-email'>*Email:</label>
<input type='text' id='contact-email' class='contact-input' name='email' tabindex='1002' />";
if ($extra["form_subject"]) {
$output .= "
<label for='contact-subject'>Subject:</label>
<input type='text' id='contact-subject' class='contact-input' name='subject' value='' tabindex='1003' />";
}
$output .= "
<label for='contact-message'>*Message:</label>
<textarea id='contact-message' class='contact-input' name='message' cols='40' rows='4' tabindex='1004'></textarea>
<br/>";
if ($extra["form_cc"]) {
$output .= "
<label> </label>
<input type='checkbox' id='contact-cc' name='cc' value='1' tabindex='1005' /> <span class='contact-cc'>Send me a copy</span>
<br/>";
}
$output .= "
<label> </label>
<button type='submit' class='contact-send contact-button' tabindex='1006'>Send</button>
<button type='submit' class='contact-cancel contact-button simplemodal-close' tabindex='1007'>Cancel</button>
<br/>
<input type='hidden' name='token' value='" . smcf_token($to) . "'/>
</form>
</div>
</div>";
echo $output;
当我试图运行代码时,虽然出现了模型框,但它也显示了php错误。
下面是我收到的错误信息
注意:未定义的索引:F:''wamp''www''bog''wordpress''wp-content''plugins''demo''contact.php中的form_cc,位于第56行
知道出了什么问题吗?
因为数组extra
没有名为form_cc
的索引。对数组extra
执行var_dump,这样您就可以看到问题所在。同时使用isset()
和empty()
方法。
很简单,在数组中似乎没有这个索引的定义,在这种情况下,似乎没有得到输入的值
在这种情况下,只需使用:
if (!empty($extra["form_cc"])) {
...
}
empty((将检查数组中是否存在键/索引,以及是否设置了值。
问题是在$extra
数组中没有一个名为"form_cc"的位置(索引(。因此,您应该使用
if (! empty($extra["form_cc"]))
{
// do stuff
}
if (isset($extra["form_cc"])) {
...
}
将删除警告消息,这是一个检查isset((的好习惯