Sorry to come back to this issue but I'm still having trouble solving it. Just to recap, the following code has a javascript onblur event in the input fields so that if someone clicks out of the field without typing something in it, the original label, eg. 'First name' is reinserted into the field. The field is also being validated by some php code, however the onblur event creates a problem because the form still submits even if a name isn't typed into the field. It's assuming that 'First name' or 'Last name' are entries and therefore isn't validating it correctly.
<div>
<label for="firstname">First Name:</label>
<input id="firstname" name="firstname" type="text" value="First name" onfocus="if (this.value == 'First name') this.value=''" onblur="if (this.value == '') this.value='First name'" />
<?php
if (ValidatedField('index_965','index_965')) {
if ((strpos((",".ValidatedField("index_965","index_965").","), "," . "1" . ",") !== false || "1" == "")) {
if (!(false)) {
?>
<span style="color: red"> First name is required </span>
<?php
}
}
}?>
</div>
<div>
<label for="lastname">Last Name:</label>
<input id="lastname" name="lastname" type="text" value="Last name" onfocus="if (this.value == 'Last name') this.value=''" onblur="if (this.value == '') this.value='Last name'" /> </div>
<?php
if (ValidatedField('index_965','index_965')) {
if ((strpos((",".ValidatedField("index_965","index_965").","), "," . "2" . ",") !== false || "2" == "")) {
if (!(false)) {
?>
<span style="color: red"> Last name is required </span>
<?php
}
}
}?>
<div>
Since I'm only using server side validation for the fields and the errors are output to spans, I didn't want to go the javascript way as you suggested. To do it the php way, I'm guessing I would have to alter the ValidatedField() function so that it returns false on blank or "First name", or "Last Name", and if yes then validation would fail and so the span with the error text "First name is required" would show, then the same with the second field.
<?php
if (!session_id()) {
session_start();
}
function ValidatedField($page,$field) {
$theFields= "";
$retVal = "";
if (isset($_SESSION["WAVT_".$page."_Errors"])) {
$theFields = "&".$_SESSION["WAVT_".$page."_Errors"];
}
if (strpos($theFields,"&WAVT_".$field."=") !== false) {
$retVal = substr($theFields,strpos($theFields,"&WAVT_".$field."=")+strlen("&WAVT_".$field."="));
}
if (strpos($retVal,"&WAVT_") !== false) {
$retVal = substr($retVal,0,strpos($retVal,"&WAVT_"));
}
if ($retVal == "" && $page == $field) {
$retVal = ValidatedField($page,$field."_Errors");
}
return $retVal;
}
?>
As I'm not really sure how the above function works, can I get some help in revising it if this is the way to solve this? I understand though if this is beyond the scope of help in these forums.