我在表单中给出了2个选项(上传图标/选择图标),但是当我用php检查它的投掷错误时......
HTML
<label class="tagslabel">Upload Icon</label>
<input name="uploadicon" type="file" />
<span>or</span>
<ul class="icons clearfix">
<li>
<input type="radio" value="123456" name="selecticon"/>
<label for="selecticon">
<img src="pics/123456.jpg" alt="123456" width="34" height="34" />
</label>
</li>
<li>
<input type="radio" value="654321" name="selecticon" />
<label for="selecticon">
<img src="pics/654321.jpg" alt="654321" width="34" height="34" />
</label>
</li>
</ul>
PHP
if (!empty($_FILES['uploadicon']['name'])) {
//do image processing and validation
}
else {
if($_POST['selecticon'] == '') $errors['selecticon'] = 'Please Select An Icon or upload Icon!';
}
但如果我上传图片而不是选择图片,则会抛出以下错误。
Notice: Undefined index: selecticon
答案 0 :(得分:4)
尝试更改
if($_POST['selecticon'] == '')
到
if (!isset($_POST['selecticon']))
答案 1 :(得分:0)
用户可能既不选择您的选项,也不会自己上传文件。在这种情况下,您应该检查$ _POST超全局中是否存在'selecticon'。像这样:
if(!isset($_POST['selecticon'] || $_POST['selecticon'] == '') $errors['selecticon'] = 'Please Select An Icon or upload Icon!'