Jump to content

how to get from errors next to their respective fields


frijole

Recommended Posts

I am working on a registration script and I cannot figure out how to make the errors show up next to the text boxes that they refer to. Does anyone know how to do this?

 

include an echo for your error near the field. for instance, for a text field for Address:

 

<?php
echo $address_error;
?>
<INPUT TYPE='text' VALUE='' NAME='address' SIZE='30'>

 

so when the form is submitted and there is an error in the address, set $address_error = 'The error message.' and it will appear near the field. do this for all fields.

I do something like this ...

<?php
if (isset($_POST['submit']))
{
$error_name = empty($_POST['name']) ? TRUE : FALSE;
$error_email = empty($_POST['email']) ? TRUE : FALSE;

$errors = FALSE;
if (!($error_name || $error_email))
{
	// process form, maybe redirect page
}
else
{
	$errors = TRUE;
}
}
// otherwise will show the form again, or for the first time
?>
<?php echo ($errors) ? "<p>There are errors.</p>" : ""; ?>
<form>
<input type="text" name="name" value="<?php echo $name; ?>" />
<?php echo ($error_name) ? "Please enter your name." : ""; ?>

<input type="text" name="email" value="<?php echo $email; ?>" />
<?php echo ($error_email) ? "Please enter your email." : ""; ?>

<input type="submit" name="submit" value="Submit" />
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.