/    Sign up×
Community /Pin to ProfileBookmark

Form error messages help

Hi I have a working form with validation all working perfectly, however I have decided to change how I display the messages in that form.

I have both Javascript and PHP validation running and both give separate error messages and they are grouped together for convenience using a <span> tag, the only problem I have is that the error messages when they aren’t active take up too much whitespace above the form, whereas I would like them to push the form down when they become active. Can anyone see how this can be done?

[code]
<?php
session_Start();

include “admin/connect_details.php”;

$Forename = $_POST[‘Forename’];
$Surname = $_POST[‘Surname’];
$Email = $_POST[‘Email’];
$ConfirmEmail = $_POST[‘ConfirmEmail’];
$User = $_POST[‘Username’];

if (isset($_POST[‘SubmitB’]))
{

$conn = mysql_connect($Host,$Username,$Password) or die (‘Can’t connect to Server’);
if (!mysql_select_db($Dbname, $conn)) die(‘Can’t connect to Database’);

$sql = “SELECT Count(*) FROM UserAccount WHERE Username=’$User'”;
$rs = mysql_query($sql,$conn) or die(‘Problem with query: ‘ . $sql . ‘<br />’ . mysql_error());

if (!$rs) {
echo(‘<head2>Database Error! – Please Try Again</head2>’);
die();
}
if (mysql_result($result,0,0)>0) {
echo(‘<head2>That Username has been taken! – Please Try Again</head2>’);
die();
}

if(empty($Forename))
{
$Errors++;
$MsgForename = “! Please enter your Forename”;
}

if(empty($Surname))
{
$Errors++;
$MsgSurname = “! Please enter your Surname”;
}

if (!preg_match(‘/^[A-Z0-9._%-]+@[A-Z0-9.-]+.[A-Z]{2,4}$/i’, $Email))
{
$Errors++;
$MsgEmail = “! Please Enter a valid Email Address”;
}
elseif($Email != $ConfirmEmail)
{
$Errors++;
$MsgEmail = “! The Email Addresses do not match please re-confirm them”;
}

if(empty($User))
{
$Errors++;
$MsgUser = “! Please enter a Username”;
}
}

?>

<html>
<head>
<title>Clothing Line</title>
<link href=”admin/stylesheetCL.css” rel=”stylesheet”>
<?php require(‘admin/jscript.inc’)?>

<script language=’JavaScript’ type=’text/JavaScript’>
<!–
function Validate(f){
return (ValidateValues(f)==0 ? true : false );
}

function ValidateValues(f)
{
var Errors = 0
if((document.registration_form.Forename.value+”).length<1)
{
document.getElementById(‘mySpan1a’).innerHTML=’Please Enter your Forename!’;
document.getElementById(‘mySpan1b’).innerHTML=’! ‘;
Errors++;
<?php $errForename =true; ?>
}
else
{
document.getElementById(‘mySpan1a’).innerHTML=”;
document.getElementById(‘mySpan1b’).innerHTML=”;
<?php unset($errForename); ?>
}

if((document.registration_form.Surname.value+”).length<1)
{
document.getElementById(‘mySpan2a’).innerHTML=’Please Enter your Surname!’;
document.getElementById(‘mySpan2b’).innerHTML=’! ‘;
Errors++;
<?php $errSurname =true; ?>
}
else
{
document.getElementById(‘mySpan2a’).innerHTML=”;
document.getElementById(‘mySpan2b’).innerHTML=”;
<?php unset($errSurname); ?>
}

if(document.registration_form.Email.value ==””)
{
Errors++
document.getElementById(‘mySpan3a’).innerHTML=’Please Enter an Email Address!’;
document.getElementById(‘mySpan3b’).innerHTML=”;
<?php $errEmail =true; ?>
}
else if(!document.registration_form.Email.value.match(/^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/))
{
Errors++
document.getElementById(‘mySpan3a’).innerHTML=’Please Enter a Valid Email Address!’;
document.getElementById(‘mySpan3b’).innerHTML=’! ‘;
<?php $errEmail =true; ?>
}
else if(document.registration_form.Email.value !== document.registration_form.ConfirmEmail.value)
{
Errors++
document.getElementById(‘mySpan3a’).innerHTML=’The Email Addresses do not match, please re-confirm them!’;
document.getElementById(‘mySpan3b’).innerHTML=’! ‘;
<?php $errEmail =true; ?>
}
else
{
document.getElementById(‘mySpan3a’).innerHTML=”;
document.getElementById(‘mySpan3b’).innerHTML=”;
<?php unset($errEmail); ?>
}

if((document.registration_form.Username.value+”).length<1)
{
document.getElementById(‘mySpan4a’).innerHTML=’Please Enter a Username!’;
document.getElementById(‘mySpan4b’).innerHTML=’! ‘;
Errors++;
<?php $errUsername=true; ?>
}
else
{
document.getElementById(‘mySpan4a’).innerHTML=”;
document.getElementById(‘mySpan4b’).innerHTML=’! ‘;
<?php unset($errUsername); ?>
}

return Errors;
}

//–>
</script>
</head>
<body>

<?php require(‘admin/header.inc’);
require(‘menu.inc’) ?>

<div class=”DivMain”>
<span class=”head2″>Registration Form</span><br>
<span class=”errmsg”>* All Marked fields required</span><br>

<span id=”mySpan1a” class=”errmsg”><?php echo $MsgForename?></span><br>
<span id=”mySpan2a” class=”errmsg”><?php echo $MsgSurname?></span><br>
<span id=”mySpan3a” class=”errmsg”><?php echo $MsgEmail?></span><br>
<span id=”mySpan4a” class=”errmsg”><?php echo $MsgUser?></span><br>

<br>
<form method=”post” name=”registration_form” action=”registration_form.php” onSubmit=”return Validate(this);”>

<table style=”width: auto; position: absolute;”>
<tr><td><span id=”mySpan1b” class=”errmsg”></span><span class=”head4″>Forename:</span><span class=”errmsg”> *</span></td>
<td width=”300″><label><input type=”text” maxlength=”15″ name=”Forename” value=”<?php echo $Forename?>”/></label></td></tr>

<tr><td><span id=”mySpan2b” class=”errmsg”></span><span class=”head4″>Surname:</span><span class=”errmsg”> *</span></td>
<td><label><input type=”text” maxlength=”20″ name=”Surname” value=”<?php echo $Surname?>”/></label></td></tr>
<tr><td><span id=”mySpan3b” class=”errmsg”></span><span class=”head4″>Email:</span><span class=”errmsg”> *</span></td>
<td width=”300″><label> <input type=”text” size=”45″ maxlength=”50″ name=”Email” value=”<?php echo $Email?>”/>
</label></td></tr>
<tr><td><span id=”mySpan3b” class=”errmsg”></span><span class=”head4″>Confirm Email:</span><span class=”errmsg”> *</span></td>
<td width=”300″><label><input type=”text” size=”45″ maxlength=”50″ name=”ConfirmEmail” value=”<?php echo $ConfirmEmail?>”/>
</label></td></tr>
<tr><td><span id=”mySpan4b” class=”errmsg”></span><span class=”head4″>Username:</span><span class=”errmsg”> *</span></td>
<td width=”300″><label><input type=”text” name=”Username” value=”<?php echo $User?>”/></label></td></tr>
<tr></tr>

<tr><td>
<input type=”submit” style=”overflow:visible; width:auto” class=”buttonS”
name=”SubmitB” value=”Submit” onMouseOver=”OverMouse(this)”; onMouseOut=”OutMouse(this)”/>
<input type=”reset” class=”buttonS” style=”overflow:visible; width:auto” name=”ResetB” value=”Reset” onmouseover=”OverMouse(this)”; onmouseout=”OutMouse(this)”/></td></tr>
</table>
</form>
</div>
</body>
</html>
[/code]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@KDLAMar 20.2008 — You need to apply some CSS to your span items.

Example:
<i>
</i>#mySpan1a {display: none;}

Then in your javascript, add to your else statements for each non-valid return:
<i>
</i>document.getElementById('mySpan1a').style.display='block';


BTW -- It would be better to change your spans to divs.

KDLA
Copy linkTweet thisAlerts:
@psychofox19authorMar 20.2008 — You need to apply some CSS to your span items.

Example:
<i>
</i>#mySpan1a {display: none;}

Then in your javascript, add to your else statements for each non-valid return:
<i>
</i>document.getElementById('mySpan1a').style.display='block';


BTW -- It would be better to change your spans to divs.

KDLA[/QUOTE]


How would I go about doing that? Would it still display the message if the Div id="mySpan1a" and so forth?

Also does this method reverse the effects or do i need to place a <i>
</i>document.getElementById('mySpan1a').style.display='none';
To the valid returns to hide it again if they resubmit the form?
Copy linkTweet thisAlerts:
@psychofox19authorMar 21.2008 — Hi, thanks for you help with the error messages thread, I got it working mostly aside from a slight issue that I was hoping you would be able to help with.

The messages are now being hidden thanks to the CSS however the spans I use for the ! marks before the input tend to appear in the line above despite being A) in the same table column and row and ? no matter how wide I make the table column they both reside in.

If I could get it to stop them appearing on the line above that would be all I need to get this form finished.

The relevant code is below, and if you would like to see the form in action go to Registration Form.

<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Clothing Line&lt;/title&gt;
&lt;link href="admin/stylesheetCL.css" rel="stylesheet"&gt;
&lt;?php require('admin/jscript.inc')?&gt;

&lt;script language='JavaScript' type='text/JavaScript'&gt;
&lt;!--
function Validate(f){
return (ValidateValues(f)==0 ? true : false );
}

function ValidateValues(f)
{
var Errors = 0

if((document.registration_form.Forename.value+'').length&lt;1)
{
document.getElementById('mySpan1a').style.display='block';
document.getElementById('mySpan1a').innerHTML='Please Enter your Forename!';
document.getElementById('mySpan1b').style.display='block';
document.getElementById('mySpan1b').innerHTML='! ';
Errors++;
}
else
{
document.getElementById('mySpan1a').style.display='none';
document.getElementById('mySpan1a').innerHTML='';
document.getElementById('mySpan1b').style.display='none';
document.getElementById('mySpan1b').innerHTML='';
}

if((document.registration_form.Surname.value+'').length&lt;1)
{
document.getElementById('mySpan2a').style.display='block';
document.getElementById('mySpan2a').innerHTML='Please Enter your Surname!';
document.getElementById('mySpan2b').style.display='block';
document.getElementById('mySpan2b').innerHTML='! ';
Errors++;
}
else
{
document.getElementById('mySpan2a').style.display='none';
document.getElementById('mySpan2a').innerHTML='';
document.getElementById('mySpan2b').style.display='none';
document.getElementById('mySpan2b').innerHTML='';
}

if(document.registration_form.Email.value =="")
{
document.getElementById('mySpan3a').style.display='block';
document.getElementById('mySpan3a').innerHTML='Please Enter an Email Address!';
document.getElementById('mySpan3b').style.display='block';
document.getElementById('mySpan3b').innerHTML='! ';
Errors++
}
else if(!document.registration_form.Email.value.match(/^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/))
{
document.getElementById('mySpan3a').style.display='block';
document.getElementById('mySpan3a').innerHTML='Please Enter a Valid Email Address!';
document.getElementById('mySpan3b').style.display='block';
document.getElementById('mySpan3b').innerHTML='! ';
Errors++
}
else
{
document.getElementById('mySpan3a').style.display='none';
document.getElementById('mySpan3a').innerHTML='';
document.getElementById('mySpan3b').style.display='none';
document.getElementById('mySpan3b').innerHTML='';
}

if(document.registration_form.ConfirmEmail.value =="")
{
document.getElementById('mySpan3c').style.display='block';
document.getElementById('mySpan3c').innerHTML='Please Enter an Email Address!';
document.getElementById('mySpan3d').style.display='block';
document.getElementById('mySpan3d').innerHTML='! ';
Errors++
}
else if(!document.registration_form.ConfirmEmail.value.match(/^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/))
{
document.getElementById('mySpan3c').style.display='block';
document.getElementById('mySpan3c').innerHTML='Please Enter a Valid Email Address!';
document.getElementById('mySpan3d').style.display='block';
document.getElementById('mySpan3d').innerHTML='! '; <br/>
Errors++
}
else
{
document.getElementById('mySpan3c').style.display='none';
document.getElementById('mySpan3c').innerHTML='';
document.getElementById('mySpan3d').style.display='none';
document.getElementById('mySpan3d').innerHTML='';
}

if(document.registration_form.Email.value !== document.registration_form.ConfirmEmail.value)
{
document.getElementById('mySpan3a').style.display='block';
document.getElementById('mySpan3a').innerHTML='The Email Addresses do not match, please re-confirm them!';
document.getElementById('mySpan3b').style.display='block';
document.getElementById('mySpan3b').innerHTML='! ';
document.getElementById('mySpan3d').style.display='block';
document.getElementById('mySpan3d').innerHTML='! ';
Errors++
}
else
{
document.getElementById('mySpan3a').style.display='none';
document.getElementById('mySpan3a').innerHTML='';
document.getElementById('mySpan3b').style.display='none';
document.getElementById('mySpan3b').innerHTML='';
document.getElementById('mySpan3c').style.display='none';
document.getElementById('mySpan3c').innerHTML='';
document.getElementById('mySpan3d').style.display='none';
document.getElementById('mySpan3d').innerHTML='';
}

if((document.registration_form.Username.value+'').length&lt;1)
{
document.getElementById('mySpan4a').style.display='block';
document.getElementById('mySpan4a').innerHTML='Please Enter a Username!';
document.getElementById('mySpan4b').style.display='block';
document.getElementById('mySpan4b').innerHTML='! ';
Errors++;
}
else
{
document.getElementById('mySpan4a').style.display='none';
document.getElementById('mySpan4a').innerHTML='';
document.getElementById('mySpan4b').style.display='block';
document.getElementById('mySpan4b').innerHTML='! '; <br/>
}

return Errors;
}

//--&gt;
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php require('admin/header.inc');
require('menu.inc') ?&gt;

&lt;div class="DivMain"&gt;

&lt;span class="head2"&gt;Registration Form&lt;/span&gt;&lt;br&gt;
&lt;span class="errmsg"&gt;* All Marked fields required&lt;/span&gt;&lt;br&gt;

&lt;span id="mySpan1a" class="mySpan1a"&gt;&lt;?php echo $MsgForename?&gt;&lt;br&gt;&lt;/span&gt;
&lt;span id="mySpan2a" class="mySpan2a"&gt;&lt;?php echo $MsgSurname?&gt;&lt;br&gt;&lt;/span&gt;
&lt;span id="mySpan3a" class="mySpan3a"&gt;&lt;?php echo $MsgEmail?&gt;&lt;br&gt;&lt;/span&gt;
&lt;span id="mySpan3c" class="mySpan3c"&gt;&lt;?php echo $MsgConfirmEmail?&gt;&lt;br&gt;&lt;/span&gt;
&lt;span id="mySpan4a" class="mySpan4a"&gt;&lt;?php echo $MsgUser?&gt;&lt;br&gt;&lt;/span&gt;

&lt;br&gt;
&lt;form method="post" name="registration_form" action="registration_form.php" onSubmit="return Validate(this);"&gt;

<i> </i>&lt;table style="width: auto; position: absolute;"&gt;
<i> </i>&lt;tr&gt;&lt;td&gt;&lt;span id="mySpan1b" class="mySpan1b"&gt;&lt;?php echo $Err1?&gt;&lt;/span&gt;&lt;span class="head4"&gt;Forename:&lt;/span&gt;&lt;span class="errmsg"&gt; *&lt;/span&gt;&lt;/td&gt;
<i> </i>&lt;td width="347"&gt;&lt;label&gt;&lt;input type="text" maxlength="15" name="Forename" value="&lt;?php echo $Forename?&gt;"/&gt;&lt;/label&gt;&lt;/td&gt;&lt;/tr&gt;

<i> </i>&lt;tr&gt;&lt;td&gt;&lt;span id="mySpan2b" class="mySpan2b"&gt;&lt;?php echo $Err2?&gt;&lt;/span&gt;&lt;span class="head4"&gt;Surname:&lt;/span&gt;&lt;span class="errmsg"&gt; *&lt;/span&gt;&lt;/td&gt;
<i> </i>&lt;td&gt;&lt;label&gt;&lt;input type="text" maxlength="20" name="Surname" value="&lt;?php echo $Surname?&gt;"/&gt;&lt;/label&gt;&lt;/td&gt;&lt;/tr&gt;

<i> </i>&lt;tr&gt;&lt;td&gt;&lt;span id="mySpan3b" class="mySpan3b"&gt;&lt;?php echo $Err3?&gt;&lt;/span&gt;&lt;span class="head4"&gt;Email:&lt;/span&gt;&lt;span class="errmsg"&gt; *&lt;/span&gt;&lt;/td&gt;
<i> </i>&lt;td&gt;&lt;label&gt; &lt;input type="text" size="45" maxlength="50" name="Email" value="&lt;?php echo $Email?&gt;"/&gt;
<i> </i>&lt;/label&gt;&lt;/td&gt;&lt;/tr&gt;
<i> </i>&lt;tr&gt;&lt;td&gt;&lt;span id="mySpan3d" class="mySpan3c"&gt;&lt;?php echo $Err4?&gt;&lt;/span&gt;&lt;span class="head4"&gt;Confirm Email:&lt;/span&gt;&lt;span class="errmsg"&gt; *&lt;/span&gt;&lt;/td&gt;
<i> </i>&lt;td&gt;&lt;label&gt;&lt;input type="text" size="45" maxlength="50" name="ConfirmEmail" value="&lt;?php echo $ConfirmEmail?&gt;"/&gt;
<i> </i>&lt;/label&gt;&lt;/td&gt;&lt;/tr&gt;

<i> </i>&lt;tr&gt;&lt;td&gt;&lt;span id="mySpan4b" class="mySpan4b"&gt;&lt;?php echo $Err5?&gt;&lt;/span&gt;&lt;span class="head4"&gt;Username:&lt;/span&gt;&lt;span class="errmsg"&gt; *&lt;/span&gt;&lt;/td&gt;
<i> </i>&lt;td&gt;&lt;label&gt;&lt;input type="text" name="Username" value="&lt;?php echo $User?&gt;"/&gt;&lt;/label&gt;&lt;/td&gt;&lt;/tr&gt;
<i> </i>&lt;tr&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;

<i> </i>&lt;input type="submit" style="overflow:visible; width:auto" class="buttonS"
<i> </i>name="SubmitB" value="Submit" onMouseOver="OverMouse(this)"; onMouseOut="OutMouse(this)"/&gt;
<i> </i>&lt;input type="reset" class="buttonS" style="overflow:visible; width:auto" name="ResetB" value="Reset" onmouseover="OverMouse(this)"; onmouseout="OutMouse(this)"/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;
&lt;/div&gt;


Thanks in advance for your help!
Copy linkTweet thisAlerts:
@psychofox19authorMar 21.2008 — The CSS is the same for all of them:

<i>
</i>#mySpan1a {display: none; font-family:arial; font-size:10pt; font-weight:bold;
color:red}
Copy linkTweet thisAlerts:
@KDLAMar 21.2008 — For the spans that you have set for "b" (as in 3b or 4b, etc.), change the "block" setting in the javascript to "inline" block makes the span a block element, pushing down the input to the next line.

KDLA
Copy linkTweet thisAlerts:
@psychofox19authorMar 21.2008 — That's it, great! Thanks so much for your help!
×

Success!

Help @psychofox19 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...