我已经做了一个简单的表单,试图对它进行验证,但不断得到一些错误,比如未定义的索引。这是代码:
<?php
//echo '<pre>'.print_r($_POST,true).'</pre>';
$display_result = false;
$form_data = [
'name' => [
'value' => '',
'error' => false,
'err_msg' => '',
],
'flavor' => [
'value' => '',
'error' => false,
'err_msg' => '',
],
'cakesize' => [
'value' => '',
'error' => false,
'err_msg' => '',
],
'filling' => [
'value' => [],
'error' => false,
'err_msg' => '',
],
'agree' => [
'value' => '',
'error' => false,
'err_msg' => '',
],
];
if ( isset( $_POST['submit'] ) ) {
$name = trim ( $_POST['name'] );
if (empty($name) ) {
$form_data['name']['error'] = true;
$form_data['name']['err_msg'] = "Please enter a name";
}
else {
$form_data['name']['value'] = $name;
$form_data['name']['error'] = false;
}
$flavor = $_POST['flavor'];
if ( empty($flavor) ) {
$form_data['flavor']['error'] = true;
$form_data['flavor']['err_msg'] = "Please select a flavor";
}
else {
$form_data['flavor']['value'] = $flavor;
$form_data['flavor']['error'] = false;
}
$cakesize = $_POST['cakesize'];
if( empty($cakesize) ){
$form_data['cakesize']['error'] = true;
$form_data['cakesize']['err_msg'] = "Please select the size";
}
else {
$form_data['cakesize']['value'] = $cakesize;
$form_data['cakesize']['error'] = false;
}
$filling = $_POST['filling'];
if( empty($filling) || count($filling) < 2 ) {
$form_data['filling']['error'] = true;
$form_data['filling']['err_msg'] = "Please select atleast 2 fllings";
}
else {
$form_data['filling']['value'] = $filling;
$form_data['filling']['error'] = false;
}
$agree = $_POST['agree'];
if( empty($agree) ) {
$form_data['agree']['error'] = true;
$form_data['agree']['err_msg'] = "Tick the box if you agree to the terms and conditions";
}
else {
$form_data['agree']['value'] = $agree;
$form_data['agree']['error'] = false;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cake ordering Form</title>
<style>
.reqd {
color: red;
}
</style>
</head>
<body>
<form action="" method="post" id="cakeForm">
<fieldset>
<legend>Personal Details</legend>
<p>
<label for="name">Your Full Name: <span class="reqd"><?php print
$form_data['name']['error'] ? $form_data['name']['err_msg'] : '' ?></span></label><br />
<input type="text" name="name" id="name" />
</p>
</fieldset>
<br />
<fieldset>
<legend>Make Your cake!</legend>
<p>
<label for="flavor">Select your flavor: <span class="reqd"><?php print
$form_data['flavor']['error'] ? $form_data['flavor']['err_msg'] : '' ?></span></label><br />
<select id="flavor" name="flavor">
<option value="0">Select Flavor</option>
<option value="yellow" >Yellow</option>
<option value="white">White</option>
<option value="chocolate">Chocolate</option>
<option value="combo">Combo</option>
</select>
</p>
<p>
<label>Size of the cake: </label><br />
<label><input type="radio" name="cakesize" value="round6"/>Round cake 6" - serves 8 people <br /></label>
<label><input type="radio" name="cakesize" value="round8"/>Round cake 8" - serves 12 people <br /></label>
<label><input type="radio" name="cakesize" value="round10"/>Round cake 10" - serves 16 people <br /></label>
<label><input type="radio" name="cakesize" value="round12"/>Round cake 12" - serves 30 people <br /></label>
</p>
<p>
<label>Fillings: </label><br />
<label class="checkFill"><input type="checkbox" name="filling" value="lemon"/>Lemon <br /></label>
<label class="checkFill"><input type="checkbox" name="filling" value="custard"/>Custard <br /></label>
<label class="checkFill"><input type="checkbox" name="filling" value="fudge"/>Fudge <br /></label>
<label class="checkFill"><input type="checkbox" name="filling" value="mocha"/>Mocha <br /></label>
<label class="checkFill"><input type="checkbox" name="filling" value="raspberry"/>Raspberry <br /></label>
<label class="checkFill"><input type="checkbox" name="filling" value="pineapple"/>Pineapple <br /></label>
</p>
</fieldset>
<p>
<label class="inlinelabel"><input type="checkbox" id="agree" name="agree" /> I agree to the terms and conditions </label>
</p>
<p>
<input type="submit" value="submit" name="submit" />
</p>
</form>
</body>
</html>
请帮忙......
答案 0 :(得分:1)
使用isset()
或empty()
检查是否设置了postdata。使用以下代码。
<?php
//echo '<pre>'.print_r($_POST,true).'</pre>';
$display_result = false;
$form_data = [
'name' => [
'value' => '',
'error' => false,
'err_msg' => '',
],
'flavor' => [
'value' => '',
'error' => false,
'err_msg' => '',
],
'cakesize' => [
'value' => '',
'error' => false,
'err_msg' => '',
],
'filling' => [
'value' => [],
'error' => false,
'err_msg' => '',
],
'agree' => [
'value' => '',
'error' => false,
'err_msg' => '',
],
];
if ( isset( $_POST['submit'] ) ) {
$name = trim ( $_POST['name'] );
if (empty($name) ) {
$form_data['name']['error'] = true;
$form_data['name']['err_msg'] = "Please enter a name";
}
else {
$form_data['name']['value'] = $name;
$form_data['name']['error'] = false;
}
$flavor = !empty($_POST['flavor']) ? $_POST['flavor'] : "";
if ( empty($flavor) ) {
$form_data['flavor']['error'] = true;
$form_data['flavor']['err_msg'] = "Please select a flavor";
}
else {
$form_data['flavor']['value'] = $flavor;
$form_data['flavor']['error'] = false;
}
$cakesize = !empty($_POST['cakesize']) ? $_POST['cakesize'] : "";
if( empty($cakesize) ){
$form_data['cakesize']['error'] = true;
$form_data['cakesize']['err_msg'] = "Please select the size";
}
else {
$form_data['cakesize']['value'] = $cakesize;
$form_data['cakesize']['error'] = false;
}
$filling = !empty($_POST['filling']) ? $_POST['filling'] : "";
if( empty($filling) || count($filling) < 2 ) {
$form_data['filling']['error'] = true;
$form_data['filling']['err_msg'] = "Please select atleast 2 fllings";
}
else {
$form_data['filling']['value'] = $filling;
$form_data['filling']['error'] = false;
}
$agree = !empty($_POST['agree']) ? $_POST['agree'] : "";
if( empty($agree) ) {
$form_data['agree']['error'] = true;
$form_data['agree']['err_msg'] = "Tick the box if you agree to the terms and conditions";
}
else {
$form_data['agree']['value'] = $agree;
$form_data['agree']['error'] = false;
}
}
?>
答案 1 :(得分:0)
建议您在分配前检查$_POST
isset。即
$flavor = isset($_POST['flavor']) ? $_POST['flavor'] : NULL;
如果未选中复选框,则可能无法提交复选框,这将导致此情况未定义。