开发者

basic php question

Sorry for the poor title, but as im not a experinced programmer i could think of a better one

Instead of storing errors in arrays, then show a list of errors in some of my forms i would like to show them next to the input field. Its so sexy!

How would you suggest I do this?

Set a variable like, $wrongemail = 1;, $tooshortpass = 1; if wrong is wrong and then check in the form?

if (!preg_match($regex, $email))  
$errors[] = "Invalid email address";

if (strlen($password) < 4)
$errors[] = "Password too short";

// No errors?   
if (count($errors) == 0)
{
 // success
}

else
{
    foreach ($errors as $error)
    echo '<li>'.$error.'</li>';
}



<table cellspacing="5" cellpadding="0">

<tr>
<td width="70px">Email:</td>
<td><input type="text" name="email" class="textinput" /></td>
<td class="red"><?php if error with email print here ?></td>
</tr>

<tr>
<td>Password:</td>
<td><input type="text" name="password" class="textinput" /></td>
<td class="red"><?php if error with password print here ?>&开发者_开发问答lt;/td>
</tr>

<tr>
<td>&nbsp;</td>
<td><input type="submit" /></td>
</tr>

</table>


if (!preg_match($regex, $email))  
$errors['email'] = "Invalid email address";

if (strlen($password) < 4)
$errors['password'] = "Password too short";

// No errors?   
if (count($errors) == 0)
{
 // success
}



<table cellspacing="5" cellpadding="0">

<tr>
<td width="70px">Email:</td>
<td><input type="text" name="email" class="textinput" /></td>
<td class="red"><?php if(isset($errors['email'])){ echo $errors['email']; } ?></td>
</tr>

<tr>
<td>Password:</td>
<td><input type="text" name="password" class="textinput" /></td>
<td class="red"><?php if(isset($errors['password'])){ echo $errors['password']; } ?></td>
</tr>

Or, as Savageman pointed out

if (!preg_match($regex, $email))  
$errors['email'][] = "Invalid email address";

if (strlen($password) < 4)
$errors['password'][] = "Password too short";

// No errors?   
if (count($errors) == 0)
{
 // success
}

function print_errors($error_array)
{
    foreach($error_array as $error)
    {
        echo $error;
    }
}



<table cellspacing="5" cellpadding="0">

<tr>
<td width="70px">Email:</td>
<td><input type="text" name="email" class="textinput" /></td>
<td class="red"><?php if(isset($errors['email'])){ print_errors($errors['email']); } ?></td>
</tr>

<tr>
<td>Password:</td>
<td><input type="text" name="password" class="textinput" /></td>
<td class="red"><?php if(isset($errors['password'])){ print_errors($errors['password']); } ?></td>
</tr>


Well, that could be a possibility. But I'll recommand to use arrays.

$errors['email'][] = 'blabla';
$errors['password'][] = 'blabla';

This way is more extensible, as you can more easily add more errrors for each field.


Name the keys of the error array after your field names. E.g.

if (!preg_match($regex, $email))  
$errors['email'] = "Invalid email address";

if (strlen($password) < 4)
$errors['password'] = "Password too short";

And then check whether an error exists for this field and show it:

<td><input type="text" name="email" class="textinput" /></td>
<?php if(array_key_exists($errors['email'])): ?>
    <td class="red"><?php echo $errors['email']; ?></td>
<?php endif; ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜