I am using javascript to validate user input before processing the form data. The javascript works fine, however if an error is detected and an alert message is displayed then the text that has been input so far is lost once 'OK' has been clicked. The form data is also posted with null fields. How do i stop this from happening?
<?php
//process form data
?>
<head>
<title>select exceptions</title>
</head>
<script language = "JavaScript1.1">
function isBlank (s)
{
for (var i=0; i < s.length; i++) {
var c = s.charAt(i);
if ( (c != ' ') && (c != '\n') && (c != '\t') ) return false;
}
return true;
}
function validate (j)
{
var msg;
var empty_fields = "";
var errors = "";
if ( (j.collect_id.value == null) || (j.collect_id.value == "") || isBlank (j.collect_id.value) ) {
empty_fields += "\n Collection Number" ;
}
if ( (j.basis.value == null) || (j.basis.value == null) || isBlank (j.basis.value) ) {
empty_fields += "\n Basis" ;
}
if (isNaN(j.priority.value) ||
( (j.priority.value != null) && (j.priority.value<j.priority.min) ) ||
( (j.priority.value != null) && (j.priority.value>j.priority.max) ) ) {
errors += "The field priority must be a number that is greater than " + j.priority.min;
errors += " and less than " + j.priority.max;
errors += ".\n";
}
if (!empty_fields && !errors) return true;
msg = "____________________________________________________\n\n";
msg += "The form was not submitted because of the following error(s).\n";
msg += "Please correct these error(s) and re-submit.\n";
msg += "____________________________________________________\n\n";
if (empty_fields) {
msg += ". The following required field(s) are empty:"
+ empty_fields + "\n";
if (errors) msg += "\n";
}
msg+= errors;
alert (msg);
return false;
}
</script>
<body>
<h1>Select the collection number and exceptions</h1>
<form name="add_new"
onSubmit="document.add_new.priority.min = 0;
document.add_new.priority.max = 9;
validate(document.add_new)"
method = 'PUT'
action = 'test4.php'>
<table width='45%' border='0'>
<tr>
<td width='41%'>Collection Number</td>
<td width='59%'>
<input type='text' name='collect_id'>
</td>
</tr>
<tr>
<td width='41%'>Author</td>
<td width='59%'>
<select name='selected_author'>
<option>Select Author</option>
<?php
$conn = pg_connect("dbname=excdb user=postgres port=5432");
if(!$conn) {echo "An error occured in connection.\n"; exit;}
//order names alphabetically
$result9 = pg_Exec( $conn, "SELECT * from author");
if(!$result9) {echo "author initials could not be retrieved.\n";exit;}
$num9 = pg_NumRows($result9);
for ($j=0; $j<$num9; $j++) {
$author_initials = pg_Result($result9, $j, "initials");
echo "<option>$author_initials</option>\n";
}
?>
</select>
</td>
</tr>
<tr>
<td width='41%'>Basis</td>
<td>
<textarea name='basis' cols='34' rows='3' wrap='virtual'></textarea>
</td>
</tr>
<tr>
<td width='41%'>Priority (0-9)</td>
<td width='59%'>
<input type='text' name='priority'>
</td>
</tr>
</table>
<input type='SUBMIT' name='SUBMIT' value='PRESS TO submit exceptions'>
</form>
</body>