Forum Moderators: open
function confirmChoice(){
answer = confirm("Are you sure you want to go ahead?")
if( answer!= 0){
return true;
}
else {
return false;
} // end of if-else
} //end of confirmChoice()
Inside the button i wrote :-
onClick="confirmChoice();return false;"
however the query runs even if i click cancel ....where m i wrong......
i m using PHP for my other coding.
function confirmChoice()
{
if (confirm("Are you sure you want to go ahead?"))
{
document.<formName>.submit();
}
}
I suspect that your onclick event is in an input tag of "type=submit". You should change that to "type=button" and have the onclick event this way:
<input type=button value=Go onClick="confirmChoice();">
When the user clicks the button, it will pop the confirm dialog. If they Click the Cancel button, nothing will happen. If they click the OK button, the form will be submitted.
-=casey=-
This is what you do:
[pre]
----script----function confirmChoice() {
return confirm("Are you sure you want to go ahead?")
}
----html-----
<form ..etc.. onsubmit="return confirmChoice()">
...
<input type="submit">
</form>
[/pre]
however i have multiple dropdown lists named 'project'(each one in a different form) having id=project so it is creating a problem. It is searching in the whole document for elements with id=project and finds there are several such elements.
My objective is not to allow the form to get submitted unless the user has selected an option from the dropdown menu.....
?
javascript function :
function confirmChoice(){
if (document.getElementById("project").selectedIndex == 0){
alert("Attention:\r\rPlease choose a project first");
return false;
}
return confirm("r u sure u want to go ahead?")
}
}
thank you
however i have multiple dropdown lists named 'project'(each one in a different form) having id=project so it is creating a problem. It is searching in the whole document for elements with id=project and finds there are several such elements.
In fact, getElementById will return the first such elemnt, if there are more than one.
Birdman is right, in that, really, an id should be unique. If this is proving difficult, or otherwise, try this..
Give the elements in question the same name,
name = "project" Then send a reference to the form to the function:
<form ..etc.. onsubmit="return confirmChoice([blue]this[/blue])"> Now, inside the function, refer to the element like so:
[pre]
function confirmChoice([blue]form[/blue]){
if (form.elements.project.selectedIndex == 0){
..etc..
[/pre]
i have 15 forms on 1 page ...help!...is there another way out?
There is, I think. Pass the form object to your function. You can have fifteen forms like this, don't even need to name the forms (but can:)
<form action="foo.pl">
<input type="button" onclick="confirmChoice(this.form);" VALUE="submit"></form>
<script language="javascript">
//"form" is the variable name that represents the form object.
function confirmChoice(form) {
if (confirm("Are you sure you want to go ahead?"){
form.submit();
}
}
</script>
When submitted, this submits **only** the form that is clicked on.