我正在开发一个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中可见
注意:未定义的索引:在/Users/eak/Sites/widget_corp/includes/validation_functions.php上可见 第22行
- 警告:无法修改标头信息 - 已经发送的标头(输出从
开始 /Users/eak/Sites/widget_corp/public/create_subject.php:10)in
第4行/susers/eak/Sites/widget_corp/includes/functions.php
因此输出开始于$ _POST [&#34;可见&#34;]被检测为未定义的位置。这里有什么解决方案?
答案 0 :(得分:0)
解决方案非常简单。正如您所说$_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);
答案 1 :(得分:0)
您好,请用这些替换代码。请先进行验证
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";
}
}
}