Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script Error - simple fix!

Status
Not open for further replies.

shawkz

Programmer
Joined
Oct 25, 2005
Messages
84
Location
GB
Please can someone help me fix this so if the user clicks cancel to the prompt the form will not submit?

Kindest thanksm

Steven Hawkz

<script LANGUAGE="JavaScript">
function ConfirmClose(j,id) {
if (document.form1["fix" + j].checked = true) {
if (confirm("Are you sure you want to Fix this?")) {
var Name = if(window.prompt("Please enter your Name")) {
form1.action = "fix.asp?id="+[id]+"&Name="+Name;
form1.submit();
return true;
} else {
return false;
}

} else {
document.form1["fix" + j].checked = false
return false;
}
}
else
{
}
}
</script>
 
You've got a bunch of errors in your code. Your logic is sound, but you need to evaluate a few things first.

Are you calling your function from the onclick handler of a button of type="submit" or type="button"? If it's of type="submit" then the form will submit no matter what value is returned from the function.

Also, what is the name of your form? The way you are referencing elements seems incorrect - document.form1["fix" + j]. The correct way to do it would be like this:
Code:
document.forms["[!]formName[/!]"].elements["[!]elementName[/!]"]

In your if statement you are using a single equals sign to evaluate the checked property - if (document.form1["fix" + j].checked = true) {. The correct way would be to use a double equals like this:
Code:
if (document.forms["formName"].elements["elementName"].checked [!]=[/!]= true) {

Or, since the checked value returns a boolean value, you don't even need to put the comparison, this alone would be enough:
Code:
if (document.forms["formName"].elements["elementName"].checked) {

and then finally, when a user clicks cancel on a prompt box it returns a value of null, so you need to check for that:
Code:
var Name = window.prompt("Please enter your Name");
[!]if (Name != null) {[/!]
   form1.action = "fix.asp?id="+[id]+"&Name="+Name;
   form1.submit();
   return true;
}
else {
   alert("you clicked cancel");
   return false;
}

-kaht

Looking for a puppy?

[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top