Forum Moderators: open

Message Too Old, No Replies

confirm alerts

mixing server side with client side.......

         

swati

6:00 pm on Dec 7, 2004 (gmt 0)

10+ Year Member



I have this function. If user selects ok then i want a query to be run and if cancel then no action .....

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.

CaseyRyan

6:24 pm on Dec 7, 2004 (gmt 0)

10+ Year Member



I would rewrite it this way:


function confirmChoice()
{
if (confirm("Are you sure you want to go ahead?"))
{
document.<formName>.submit();
}
}

In the above code, <formName> refers to the name of your form.

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=-

Bernard Marx

7:15 pm on Dec 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmmm. Not so adviseable. It will mean that users who do not have Javascript enabled will not be able to submit the form. If that's part of the plan, then OK. But if you want them to be able to submit too you shouldn't use any other button than a submit button.

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]

swati

7:55 am on Dec 8, 2004 (gmt 0)

10+ Year Member



yes but i m having several forms dynamically generated .......all having same name .....so how can i use this function ......even if i name them differently how can i call different functions for each form ......

Bernard Marx

11:32 am on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Call a different function in each onsubmit handler.

swati

12:42 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



i have 15 forms on 1 page ...help!...is there another way out?

Birdman

12:49 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What will be the difference in the confirm function, for each form?

I don't see the problem here, Bernard's advice is sound.

swati

1:27 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



you are right, i was mistaken
thanks you so much for your advice.

Regards
Swati

swati

2:12 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



Hello,
In the same function i inserted the following code:
if (document.getElementById("project").selectedIndex == 0){
alert("Attention:\r\rPlease choose a project first");
return false;
}

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.....
?

Birdman

2:28 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmmm, you need to fix your code that generates the forms first, I think. First, an id needs to be unique on a page. There should be no duplicate ids.

Let's have a look at the code that produces the forms, then we can straighten it out once and for all.

Birdman

swati

3:37 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



Well, I will try to give you a simpler picture of my long code.(i ve cut out all tr,td tags etc). I have the code to name each form/element uniquely. I want to know what change i should make in the javascript to identify elements which are named as php variables like. $id_pro
while($row=mysql_fetch_array($result)){
//form creation is inside this while loop...
<form name="myForm" method="get" onsubmit="return confirmChoice()" action="//same page">
<select name="project" id="project">
<option>select</option>
<option>3G LPM</option>
<option>ZM-FF</option>
</select>
<input type="text" size="4" name="usage" maxlength="3">
<input type="submit" name="go" value="GO">
<?php
if($_GET['go']){
//place where insert query is to be performed when the go button is pressed
}
?>
</form>

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?")
}

}

Birdman

4:01 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's still not clear from that code, unfortunately :(

Please include the code inside the loop.

while($row=mysql_fetch_array($result)){
..show all code..
}

That's where the fix will happen.

swati

4:34 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



The code is simply too long to be posted here, i cannot ...would fill pages ....
inside the while($row=@mysql_fetch_array($sql))
{} loop
As many forms would be created as the number of records returned by the query and all the dropdown menus inside wud get named "project" and have id='project' .......
Supposing i name their id's differently like project1, project2, project3 then wat change should i make in my javasscript function?
or any other idea that you might have?

thank you

Bernard Marx

5:08 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A suggestion - this is mainly a response to message #9:

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]

rocknbil

7:30 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



BernardMarx - doesn't your solution submit the form anyway even if they click cancel? Wrestled with that one for a while now and will test your solution later . . .

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.

Bernard Marx

8:38 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



doesn't your solution submit the form anyway even if they click cancel?

No. This confirm returns false, passed to the event handler, and submission is cancelled.
(at least it did the last time I tested). I imagine it's the 'normal' thing to do, not just something I made up - but you never know.

rocknbil

12:55 am on Dec 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Awesome, actually I've been chasing that one for a while now, figured there was a way . . . .:D