我正在PHP中执行表单验证。我的目的是在表单的同一页面上显示错误消息,以便为用户清楚。
但我有问题。首先,如何隐藏我的表单,提交时没有错误(我希望在这种情况下打印一条消息并隐藏frm)。我正在尝试使用:
if(false === $error)
{
//Validimi perfundoi me sukses!
echo "<script>
document.getElementById('wrap').style.display = 'none';
</script>";
echo $name;
}
但它不起作用。
其次我遇到了复选框验证问题。我正在使用数组$ activity来保存复选框中的值,因为它们可能是多个值,但是当用户在复选框部分根本没有选择任何值时,它会给出错误:Warning: in_array() expects parameter 2 to be array, null given
即使我有将ar活动初始化为arra:$activity=array()
;。
<?php
$name_error='';
$device_error ='';
$OS_error='';
$activity_error='';
$device='';
$OS='';
$activity=array();
if(!empty($_POST['submitted']))
{//nese form eshte submitted atehere validohen fushat
$name = trim($_POST['name']);//heq hapesirat
$error = false;
if(empty($name))
{
$name_error='Emri eshte bosh. Ju lutem plotesoni emrin.';
$error=true;
}
if(empty($_POST['device']))
{
$device_error = "Ju lutem selektoni nje pajisje";
$error=true;
}
else
{
$device = $_POST['device'];
}
if(empty($_POST['OS']))
{
$OS_error ="Ju lutem selektoni sistemin operativ";
$error=true;
}
else
{
$OS = $_POST['OS'];
}
if(empty($_POST['activity']) || count($_POST['activity']) < 2)
{
$activity_error = "Ju lutem selektoni te pakten 2 aktivitete";
$error=true;
}
$activity = $_POST['activity'];
if(false === $error)
{
//Validimi perfundoi me sukses!
echo "<script>
document.getElementById('wrap').style.display = 'none';
</script>";
echo $name;
}
}
?>
<!DOCTYPE html>
<html >
<head>
<title>Computer Form</title>
<link href="compForm.css" rel="stylesheet" type="text/css" />
</head>
<body >
<div id="wrap" style="display: block">
<form method="post" action='?' id="compform" >
<div>
<div class="cont_order">
<fieldset>
<legend>Beni zgjedhjen tuaj!</legend>
<div class='field_container'>
<label >Zgjidhni pajisjen qe perdorni me shpesh:</label>
<span class="error"><?php echo $device_error;?></span>
<label class='radiolabel'><input type="radio" name="device" value="Desktop"
<?php echo ($device=='Desktop')? 'checked':''; ?>/>Desktop</label><br/>
<label class='radiolabel'><input type="radio" name="device" value="Laptop"
<?php echo ($device=='Laptop')? 'checked':''; ?> />Laptop</label><br/>
<label class='radiolabel'><input type="radio" name="device" value="Tablet"
<?php echo ($device=='Tablet')? 'checked':''; ?> />Tablet</label><br/>
</div>
<div class='field_container'>
<label for="OS">Zgjidhni Sistemin e Operimit qe perdorni:</label >
<span class='error'><?php echo $OS_error?></span>
<select id="OS" name='OS' >
<option value="">Zgjidhni OS</option>
<option <?php echo $OS=='Windows'?'selected':''; ?> >Windows</option>
<option <?php echo $OS=='Linux'?'selected':''; ?> >Linux</option>
<option <?php echo $OS=='Mac'?'selected':''; ?> >Mac</option>
</select>
</div>
<div class='field_container'>
<label >Selektoni dy aktivitetet qe preferoni me shume:</label>
<span class='error'><?php echo $activity_error ?></span>
<label><input type="checkbox" value="Programim Desktop" name='activity[]'
<?php echo (in_array('Programim Desktop',$activity)) ?'checked':'' ?> />Programim Desktop</label>
<label><input type="checkbox" value="Programim Web" name='activity[]'
<?php echo (in_array('Programim Web',$activity)) ?'checked':'' ?> />Programim Web</label>
<label><input type="checkbox" value="Dizenjim" name='activity[]'
<?php echo (in_array('Dizenjim',$activity)) ?'checked':'' ?> />Dizenjim</label>
<label><input type="checkbox" value="Analize te dhenash" name='activity[]'
<?php echo (in_array('Analize te dhenash',$activity)) ?'checked':'' ?> />Analize te dhenash</label>
<label><input type="checkbox" value="Kerkim shkencor" name='activity[]'
<?php echo (in_array('Kerkim shkencor',$activity))?> />Kerkim shkencor</label>
</div>
</fieldset>
</div>
<div class="cont_details">
<fieldset>
<legend>Detajet e kontaktit</legend>
<label for='name'>Emri</label>
<input type="text" id="name" name='name'
value='<?php echo htmlentities($name) ?>' />
<span class='error'><?php echo $name_error ?></span>
<br/>
<label for='address'>Adresa e emailit</label>
<input type="email" id="address" name='address' />
<br/>
</fieldset>
</div>
<input type='submit' name='submitted' id='submit' value='Submit' />
</div>
</form>
</div>
</body>
</html>
答案 0 :(得分:0)
这是我的快速解决方案(未经测试)。让我们稍微清理你的代码:
不要为每个错误消息使用(和浪费)单独的变量,而是使用名为$errors
的关联数组。键将是输入的名称,值将是它们各自的错误消息。
为了确保您不会从未声明的变量中收到警告,我们将在页面顶部为每个输入声明变量。
我们还使用新变量$submitted
来了解表单是否已提交。
现在,如果表单为$submitted
且没有(!)$errors
,那么我们会隐藏表单。否则,我们会显示表格和任何错误(如果有)。
<?php
$name = '';
$device = '';
$OS = '';
$activity = array();
$submitted = !empty($_POST['submitted']);
$errors = array();
if ($submitted) {
//nese form eshte submitted atehere validohen fushat
if (empty($_POST['name'])) {
$errors['name'] ='Emri eshte bosh. Ju lutem plotesoni emrin.';
} else {
$name = trim($_POST['name']);
}
if (empty($_POST['device'])) {
$errors['device'] = "Ju lutem selektoni nje pajisje";
} else{
$device = $_POST['device'];
}
if (empty($_POST['OS'])) {
$errors['OS'] = "Ju lutem selektoni sistemin operativ";
} else {
$OS = $_POST['OS'];
}
if (empty($_POST['activity']) || count($_POST['activity']) < 2) {
$errors['activity'] = "Ju lutem selektoni te pakten 2 aktivitete";
} else {
$activity = $_POST['activity'];
}
}
?>
<!DOCTYPE html>
<html >
<head>
<title>Computer Form</title>
<link href="compForm.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php if ($submitted && !$errors) : ?>
<?php echo $name ?>
<?php else : ?>
<div id="wrap">
<form method="post" action='?' id="compform" >
<div>
<div class="cont_order">
<fieldset>
<legend>Beni zgjedhjen tuaj!</legend>
<div class='field_container'>
<label >Zgjidhni pajisjen qe perdorni me shpesh:</label>
<span class="error"><?php echo isset($errors['device']) ? $errors['device'] : '' ?></span>
<label class='radiolabel'><input type="radio" name="device" value="Desktop"
<?php echo $device == 'Desktop' ? 'checked' : '' ?>/>Desktop</label><br/>
<label class='radiolabel'><input type="radio" name="device" value="Laptop"
<?php echo $device == 'Laptop' ? 'checked' : '' ?> />Laptop</label><br/>
<label class='radiolabel'><input type="radio" name="device" value="Tablet"
<?php echo $device == 'Tablet' ? 'checked' : '' ?> />Tablet</label><br/>
</div>
<div class='field_container'>
<label for="OS">Zgjidhni Sistemin e Operimit qe perdorni:</label >
<span class='error'><?php echo isset($errors['OS']) ? $errors['OS'] : '' ?></span>
<select id="OS" name='OS' >
<option value="">Zgjidhni OS</option>
<option <?php echo $OS == 'Windows' ? 'selected' : '' ?> >Windows</option>
<option <?php echo $OS == 'Linux' ? 'selected' : '' ?> >Linux</option>
<option <?php echo $OS == 'Mac' ? 'selected' : '' ?> >Mac</option>
</select>
</div>
<div class='field_container'>
<label >Selektoni dy aktivitetet qe preferoni me shume:</label>
<span class='error'><?php echo isset($errors['activity']) ? $errors['activity'] : '' ?></span>
<label><input type="checkbox" value="Programim Desktop" name='activity[]'
<?php echo in_array('Programim Desktop', $activity) ? 'checked' : '' ?> />Programim Desktop</label>
<label><input type="checkbox" value="Programim Web" name='activity[]'
<?php echo in_array('Programim Web', $activity) ? 'checked' : '' ?> />Programim Web</label>
<label><input type="checkbox" value="Dizenjim" name='activity[]'
<?php echo in_array('Dizenjim', $activity) ? 'checked' : '' ?> />Dizenjim</label>
<label><input type="checkbox" value="Analize te dhenash" name='activity[]'
<?php echo in_array('Analize te dhenash', $activity) ? 'checked' : '' ?> />Analize te dhenash</label>
<label><input type="checkbox" value="Kerkim shkencor" name='activity[]'
<?php echo in_array('Kerkim shkencor', $activity) ? 'checked' : '' ?> />Kerkim shkencor</label>
</div>
</fieldset>
</div>
<div class="cont_details">
<fieldset>
<legend>Detajet e kontaktit</legend>
<label for='name'>Emri</label>
<input type="text" id="name" name='name' value='<?php echo htmlentities($name) ?>' />
<span class='error'><?php echo isset($errors['name']) ? $errors['name'] : '' ?></span>
<br/>
<label for='address'>Adresa e emailit</label>
<input type="email" id="address" name='address' />
<br/>
</fieldset>
</div>
<input type='submit' name='submitted' id='submit' value='Submit' />
</div>
</form>
</div>
<?php endif ?>
</body>
</html>
答案 1 :(得分:0)
你可以查看$ _POST [&#39;提交的&#39;]如下伪代码:
//form is submitted
if isset $_POST['submitted']
//process form and show error message
else
//show form