...

View Full Version : checkform then another test



hyefive
03-18-2004, 07:13 PM
Hi,

Extreme JavaScript newb here.

The idea is if they don't select anything from the list and click Remove', they're alerted. If they behave nicely and do select something prior to pressing the 'Remove' button, then it will perform the target = 0 or 1 check.

It is alerting them if nothing is selected prior to pressing 'Remove', but it is still submitting the form!

Thanks for any advice on this one!



<head>
<script language="javascript">
function Target(target)
{
validMessage="Please select one or more Cities/Zips/Areas to remove";
dataEntered = new Boolean(true)

if (document.form2.CITY_LIST.value == ""){
dataEntered = false;
}

if(dataEntered==false){
alert(validMessage);
return false;
}

else
{
if (target == 0) document.form2.action="custsel.asp?re=move";
document.form2.submit;
if (target == 1) document.form2.action="custsel.asp?re=set";
document.form2.submit;
}
}
</script>
</head>

<html>
<form action="custsel.asp" method="post" id=form2 name=form2>
<select name="CITY_LIST" size="20" multiple>
<option value="A">City A</option>
<option value="B">City B</option>
<option value="C">City C</option>
etc...
</select>
<input type="SUBMIT" value="Remove" id=SUBMIT1 name=SUBMIT1 onClick="Target(0)">&nbsp;&nbsp;
<input type="SUBMIT" value="Reset" id=SUBMIT2 name=SUBMIT2 onClick="Target(1)">
</form>

Roy Sinclair
03-18-2004, 07:47 PM
<input type="SUBMIT" value="Remove" id=SUBMIT1 name=SUBMIT1 onClick="return Target(0)">
<input type="SUBMIT" value="Reset" id=SUBMIT2 name=SUBMIT2 onClick="return Target(1)">


The onclick function also has to return the "false" value that your validation function returns in order to cancel the submit action.

hyefive
03-18-2004, 09:18 PM
Aha, thanks Roy!

I thought next I'd try to enhance a line (red) to my Target() function to verify that in fact it is the Remove button being clicked and not the Reset button.

Question: How to tell which submit button in the form is being pressed? The following code issues an alert if either button is clicked!

<head>
<script language="javascript">
function Target(target)
{
validMessage="Please select one or more Cities/Zips/Areas to remove";
dataEntered = new Boolean(true)
if (document.form2.SUBMIT1.value == "Remove" && document.form2.CITY_LIST.value == ""){
dataEntered = false;
}

if(dataEntered==false){
alert(validMessage);
return false;
}

else
{
if (target == 0) document.form2.action="custsel.asp?re=move";
document.form2.submit;
if (target == 1) document.form2.action="custsel.asp?re=set";
document.form2.submit;
}
}
</script>
</head>

<html>
<form action="custsel.asp" method="post" id=form2 name=form2>
<select name="CITY_LIST" size="20" multiple>
<option value="A">City A</option>
<option value="B">City B</option>
<option value="C">City C</option>
etc...
</select>
<input type="SUBMIT" value="Remove" id=SUBMIT1 name=SUBMIT1 onClick="Target(0)">
<input type="SUBMIT" value="Reset" id=SUBMIT2 name=SUBMIT2 onClick="Target(1)">
</form>

Roy Sinclair
03-18-2004, 10:15 PM
Since you're passing a different value to the submit function depending on which submit button was pressed, just test for that value.

hyefive
03-18-2004, 11:18 PM
Thanks again for the input, Roy.

To make things clearer, I've changed the function a bit.

This works if I select some cities and click Remove. But if I press the Remove All button why am I still being alerted?



<script language="javascript">
function Target(target)
{
if (target == 0 && document.form2.CITY_LIST.value != ""){
document.form2.action="custsel.asp?re=move";
document.form2.submit;
}
else{
alert("Please select one or more Cities to remove");
return false;
}

if (target == 1){
document.form2.action="custsel.asp?re=moveall";
document.form2.submit;
}
}
</script>

<form action="custsel.asp" method="post" id=form2 name=form2>
<select name="CITY_LIST" size="20" multiple>
<option value="A">City A</option>
<option value="B">City B</option>
<option value="C">City C</option>
etc...
</select>
<input type="SUBMIT" value="Remove" id=SUBMIT1 name=SUBMIT1 onClick="return Target(0)">
<input type="SUBMIT" value="Remove All" id=SUBMIT2 name=SUBMIT2 onClick="return Target(1)">
</form>
</html>

Willy Duitt
03-19-2004, 12:06 AM
Originally posted by hyefive
This works if I select some cities and click Remove. But if I press the Remove All button why am I still being alerted?

Because your script reads:

If target equals 0 and CITY_LIST value is not empty do this.
Else If not the above alert....

Clicking target 1 without choosing any of the options would be an else!

Try switching what the first conditional checks for.

eg:
<script language="javascript">
function Target(target)
{
if (target == 0 && document.form2.CITY_LIST.value == ""){
alert("Please select one or more Cities to remove");
return false;
}
else{
document.form2.action="custsel.asp?re=move";
document.form2.submit;
}

if (target == 1){
document.form2.action="custsel.asp?re=moveall";
document.form2.submit;
}


}
</script>

.....Willy

hyefive
03-19-2004, 01:00 AM
Aw man, no! Thanks for pointing out the obvious Willy!

I'll get the hang of this yet...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum