PDA

View Full Version : Problem with One Form, Two Submits



Manny_56
05-08-2007, 11:27 PM
Hi,

I have a problem where I'm using one form with two actions (and hence two submits) - the first submit is to query a database and the second submit is to write to the database.

However, I've got a slight problem - I'm validating entry in certain fields via onSubmit to ensure valid data is entered into the database. This is causing problems when your press the query button as the validation rule is returning false when you query (because the onSubmit action is attached to the form, not the submits).

Is it possible so that validation is only used when the add button is used (bypassed for the query button)?

Form example below:

<html>
<head>
<title>Users</title>
</head>
<body>

<script language="JavaScript" type="text/javascript">

function val(form1) {
strusername = document.forms['form1'].elements['username'].value;
strpwd = document.forms['form1'].elements['pwd'].value;
strfname = document.forms['form1'].elements['fname'].value;
strlname = document.forms['form1'].elements['lname'].value;
stremailaddress = document.forms['form1'].elements['emailaddress'].value;

valid=true;

if (strusername =="" || strusername == null || strusername.charAt(0) == ' '){
document.forms['form1'].elements['username'].style.backgroundColor='ffff99';
valid= false;
}
else {
document.forms['form1'].elements['username'].style.backgroundColor='ffffff';
}

if (strpwd =="" || strpwd == null || strpwd.charAt(0) == ' '){
document.forms['form1'].elements['pwd'].style.backgroundColor='ffff99';
valid= false;
}
else {
document.forms['form1'].elements['pwd'].style.backgroundColor='ffffff';
}

if (strfname =="" || strfname == null || strfname.charAt(0) == ' '){
document.forms['form1'].elements['fname'].style.backgroundColor='ffff99';
valid= false;
}
else {
document.forms['form1'].elements['fname'].style.backgroundColor='ffffff';
}

if (strlname =="" || strlname == null || strlname.charAt(0) == ' '){
document.forms['form1'].elements['lname'].style.backgroundColor='ffff99';
valid= false;
}
else {
document.forms['form1'].elements['lname'].style.backgroundColor='ffffff';
}

if (strusername =="" || strusername == null || strusername.charAt(0) == ' ' || strpwd =="" || strpwd == null || strpwd.charAt(0) == ' ' || strfname =="" || strfname == null || strfname.charAt(0) == ' ' || strlname =="" || strlname == null || strlname.charAt(0) == ' ' ){
document.form1.InputError.value="** Please ensure all fields highlighted in yellow are complete **" ;
valid= false;
}
else {
document.form1.InputError.value="" ;
}

return valid;
}
</script>

<center>
<table border="0" cellspacing="0" cellpadding="3" width="75%">
<tr>
<td valign="top" width="75%" align="center" height="1">

<table border="1" width="90%" cellpadding="3" cellspacing="0" bordercolorlight="#666666" bordercolordark="#666666">
<form name="form1" method="POST" action="updateexistinguser1.asp" onSubmit="return val(this)">
<tr>
<td valign="top" align="center" class="trl">

<table border="1" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td align="right">Username:&nbsp;&nbsp;</td>
<td><input type="text" id="username" name="username" value="" size="45">&nbsp;*</td>
</tr>
<tr>
<td align="right">Password:&nbsp;&nbsp;</td>
<td><input type="password" id="pwd" name="pwd" value="" size="45" onFocus="x=true" onBlur="x=false">&nbsp;*</td>
</tr>
<tr>
<td align="right">Forename:&nbsp;&nbsp;</td>
<td><input type="text" id="fname" name="fname" value="" size="45">&nbsp;*</td>
</tr>
<tr>
<td align="right">Surname:&nbsp;&nbsp;</td>
<td><input type="text" id="lname" name="lname" value="" size="45">&nbsp;*</td>
</tr>
<tr>
<td align="right">Email Address:&nbsp;&nbsp;</td>
<td><input type="text" id="emailaddress" name="EmailAddress" value="" size="45" onChange="javascript:this.value.toLowerCase();"></td>
</tr>
<tr>
<td colspan="2" align="right">
<br><input type="submit" name="query" value="Query Users" onClick="document.form1.action='query.asp';">
&nbsp; <input type="submit" name="add" value="Add User" onClick="document.form1.action='add.asp';">
&nbsp; <input type="button" value="Reset" onClick="history.go()"> &nbsp; <br><br></td>
</tr>
</table>
</td>
</tr>
</table>

</td>
</tr>
<tr>
<td colspan="2" align="center" align="top">
<INPUT type="text" name="InputError" size="60" readonly=readonly><br>
</form>


</td>
</tr>
</table>
</center>

<body>
</html>

Thanks in anticipation.

Manny

bklurchio
05-09-2007, 04:05 PM
See if this works

<form name="form1" id="form1" method="POST" action="updateexistinguser1.asp" onSubmit="if (this.action != 'query.asp') return val(this);">

Manny_56
05-09-2007, 06:29 PM
You my friend are a genius - that has worked perfectly.

Thanks for all your help.

Manny