提问人:sklrboy 提问时间:6/22/2016 最后编辑:Hardik Solankisklrboy 更新时间:6/22/2016 访问量:597
$_POST[“visible”] 标记为未定义索引 [重复]
$_POST["visible"] marked as undefined index [duplicate]
问:
我正在处理一个PHP项目,我需要提交以下表格:
<h2>Create Subject</h2>
<form action="create_subject.php" method="post">
<p>Subject name:
<input type="text" name="menu_name" value="" />
</p>
<p>Position:
<select name="position">
<?php
$subject_set = find_all_subjects();
$subject_count = mysqli_num_rows($subject_set);
for ($count=1; $count <= ($subject_count + 1); $count++) {
echo "<option value=\"{$count}\">{$count}</option>";
}
?>
</select>
</p>
<p>Visible:
<input type="radio" name="visible" value="0" /> No
<input type="radio" name="visible" value="1" /> Yes
</p>
<input type="submit" name="submit" value="Create Subject" />
</form>`
在create_subject.php(表单操作发生的地方)中,我进行了一些验证,如下所示:
if(isset($_POST['submit'])) {
// Process the form
$menu_name = mysql_prep($_POST["menu_name"]);
$position = (int) $_POST["position"];
$visible = (int) $_POST["visible"];
//validations
$required_fields = array("menu_name", "position", "visible");
validate_presences($required_fields);
$fields_with_max_lengths = array("menu_name" => 30);
validate_max_lengths($fields_with_max_lengths);
if(!empty($errors)) {
$_SESSION["errors"] = $errors;
redirect_to("new_subject.php");
}
其中,验证状态应用作检查字段是否为空,如下所示:
function validate_presences($required_fields) {
global $errors;
foreach ($required_fields as $field) {
$value = trim($_POST[$field]);
if (!has_presence($value)) {
$errors[$field] = fieldname_as_text($field)." can't be blank";
}
}
}
但是,当我提交缺少数据的表单时,我没有重定向回上一页并列出会话中存储的所有错误,而是收到以下错误消息:
注意:未定义索引:在第 10 行的 /Users/eak/Sites/widget_corp/public/create_subject.php 中可见
注意:未定义索引:可见于第 22 行的
/Users/eak/Sites/widget_corp/includes/validation_functions.php- 警告:无法修改标头信息 - 第 4 行 /Users/eak/Sites/widget_corp/includes/functions 中的 /Users/eak/Sites/widget_corp/public
/create_subject.php:10 中
已发送的标头.php
因此,输出从检测到 $_POST[“visible”] 未定义的位置开始。这里的解决方案是什么?
答:
0赞
Sujeet Sinha
6/22/2016
#1
解决方案非常简单。正如您所说,是未定义的,只需使用以下代码块即可处理相同的问题。$_POST["visible"]
if (isset($_POST["visible"])) {
//Handle the null condition
}
编辑: 根据您的评论,您有一个函数来处理上述问题,但是在您使用该值后调用它。查看以下评论:
$menu_name = mysql_prep($_POST["menu_name"]);
$position = (int) $_POST["position"];
$visible = (int) $_POST["visible"]; // YOU USED THIS VALUE - ERROR ALREADY OCCURRED HERE
//validations
$required_fields = array("menu_name", "position", "visible"); //AND THEN YOU PERFORMED VALIDATION-- CALL THIS BEFORE HAND
validate_presences($required_fields);
评论
0赞
sklrboy
6/22/2016
有一个状态验证器函数,该函数用于处理 null 条件,但重定向到上一页以列出错误会被错误消息中断。
0赞
Sujeet Sinha
6/22/2016
在使用值之前调用验证器。我将编辑答案以解释相同的问题。
0赞
Dhaval Bhavsar
6/22/2016
#2
嗨,你能用这些替换代码吗?请先进行验证
if(isset($_POST['submit'])) {
//validations
$required_fields = array("menu_name", "position", "visible");
validate_presences($required_fields);
if(!empty($errors)) {
$_SESSION["errors"] = $errors;
redirect_to("new_subject.php");
}
// Process the form
$menu_name = mysql_prep($_POST["menu_name"]);
$position = (int)$_POST["position"];
$visible = (int) $_POST["visible"];
$fields_with_max_lengths = array("menu_name" => 30);
validate_max_lengths($fields_with_max_lengths);
同时更新这些函数validate_presences:-
function validate_presences($required_fields) {
global $errors;
foreach ($required_fields as $field) {
if(isset($_POST[$field])){
$value = trim($_POST[$field]);
if (!has_presence($value)) {
$errors[$field] = fieldname_as_text($field)." can't be blank";
}
} else {
$errors[$field] = fieldname_as_text($field)." can't be blank";
}
}
}
评论