How can I keep the value in my fields?
Posted: Thu Jan 20, 2005 6:00 pm
This code works find for me in that it validates two fields that if left empty (the Name and Details) it will return an error with the messages displayed. My problem is that if I enter information into say the ‘Details’ field only and leave the ‘Name’ blank the validation runs and a message displays an error beside the relevant field saying 'You must give your name' but at this point it clears both fields which is very annoying especially if someone had gone to the trouble to leave a large text message and forgets to insert their name.
Can anyone help me sort this as I’m very new to PHP and thought that I was getting somewhere with this up to now. I appreciate that I need some sort of variable that will hold the value but I don’t know how to do this.
Many thanks
Brian
Can anyone help me sort this as I’m very new to PHP and thought that I was getting somewhere with this up to now. I appreciate that I need some sort of variable that will hold the value but I don’t know how to do this.
Many thanks
Brian
Code: Select all
if ($_POST && array_key_exists('sendCom',$_POST)) {
$nomessage='';
$error=array();
$message='';
$email='';
$con_email='';
if (isset($_POSTї'GuestDetails']) && !empty($_POSTї'GuestDetails'])) {
$message=strip_tags($_POSTї'GuestDetails']);
}
else {
$nomessage = 'You Must leave a Message';
}
if (isset($_POSTї'GuestName']) && !empty($_POSTї'GuestName'])) {
$GuestName=trim($_POSTї'GuestName']);
}
else {
$errorї'GuestName'] = 'You must give your name';
}
// If no errors, send email and redirect to acknowledgment page
if (!$nomessage && !$error) {
echo 'no errors!';
exit();
}
}
?>
<?php
if (isset($nomessage) || isset($error)) {
?>
<strong>Error. Please check the points highlighted in bold </strong>
<?php
}
?><form method="post" name="form1" action="">
GuestName:
<?PHP
if(isset($error)) {//Display error essage.Otherwise skip.
foreach ($error as $key=> $value){ //Loop through error message, and display
echo $value;
}}?>
<input type="text" name="GuestName" value="" size="32"></td>
<input type="text" name="GuestLocation" value="" size="32"></td>
<?php if (isset($nomessage) && !empty($nomessage)) {
echo $nomessage; } else {
} ?></td>
<?php if (isset($_POSTї'comments'])) echo $_POSTї'comments']; ?>
<input type="text" name="GuestWebsite" value="" size="32">
<input type="text" name="GuestEmail" value="" size="32">
<input name="sendCom" type="submit" id="sendCom" value="Post Message" />
<input name="Reset" type="reset" value="Reset"><
<input type="hidden" name="MM_insert" value="form1">
</form>