can I use confirm to stop a select list change?

J

JohnSouth104

Hi

I want to warn the user that changing the selected item in a pull-down
will clear some data he has already entered in a file download box.

I've tried using confirm() to return true or false with a line such as:

onchange="return(confirm('proceed Yes or No')))"
but it always stops the change.

Is what I'm trying to do possible?

John South
Pangbourne UK
 
R

RobG

Hi

I want to warn the user that changing the selected item in a pull-down
will clear some data he has already entered in a file download box.

I've tried using confirm() to return true or false with a line such as:

onchange="return(confirm('proceed Yes or No')))"

There's one to many closing brackets...------^
But maybe that's just a posting typo.
but it always stops the change.

Stops what change?

You can't use onchange to prevent a user changing a selection - it
runs after they've made the change (usually when the element loses
focus, but not always).

Not sure what you mean by "a file download box" - you can't change
Is what I'm trying to do possible?

So far, no, but maybe. Supply a bit more code or fuller explanation
to illustrate what you are trying to do.

A confirm gives the user an opportunity to conditionally execute some
script, I can't see that it has any use as the sole action of an
onchnage event.

Here's a bit of play code:

<form name="formA" action="">
<select name="selA" onchange="
if (confirm('Proceed Yes or No')){
selIdx = this.selectedIndex;
// Do whatever if user says OK
// ...
} else {
// Don't do it and return selection to previous
this.selectedIndex = selIdx;
}
">
<option selected>option 1</option>
<option>option 2</option>
<option>option 3</option>
</select>
</form>
<script type="text/javascript">
// Set selIdx to defalut selected index on page load
var selIdx = document.formA.selA.selectedIndex;
</script>
 
J

JohnSouth104

Rob,
Thanks for the clear and full reply. I think the answer to my question
is no, but I'll explain a bit more detail just in case you see an
opening:

On my form I have an input type="file" upload control and a number of
text boxes. I also have a select control to select the JobType. The
number of text boxes depends on the JobType. When the user selects a
JobType I do an "AutoPostBack" (asp.net c#) to refresh the page with
the correct text boxes. Unfortunately if the user has already browsed
for a file to upload, this is cleared with the refresh.

If the user changes the JobType I'd like to check if the file upload
control contains a value and, if so, warn the user that the control
will be cleared. I'd like to give him the chance to cancel and to
return the JobType to it's value before the last change, and not do the
auto call back.
Am I asking too much?

John South
 
A

ASM

Rob,
Thanks for the clear and full reply. I think the answer to my question
is no, but I'll explain a bit more detail just in case you see an
opening:

On my form I have an input type="file" upload control and a number of
text boxes. I also have a select control to select the JobType. The
number of text boxes depends on the JobType. When the user selects a
JobType I do an "AutoPostBack" (asp.net c#) to refresh the page with
the correct text boxes.

I do not understand Why you have to refresh the page
only to change some fields.
Unfortunately if the user has already browsed
for a file to upload, this is cleared with the refresh.

Except if your php send back the right values
If the user changes the JobType I'd like to check if the file upload
control contains a value and, if so, warn the user that the control
will be cleared. I'd like to give him the chance to cancel and to
return the JobType to it's value before the last change, and not do the
auto call back.
Am I asking too much?

No, because I don't understand what exactly do the
onchange on select if I'd confirm.
Is this select a submit button ?

<select onchange="var n=this.options.selectedIndex;
var o = this.options[n].value
if(confirm('You\'re sure of your choice?\nif OK page will change'))
{ document.myForm.ind.value = o;
document.myForm.submit(); }
else
{ alert('No change done');
this[0].selected=true;
document.myForm.ind.value = '';}">

document.myForm.ind is a hidden field initially empty
(to tell to php what to do)
 
R

RobG

Rob,
Thanks for the clear and full reply. I think the answer to my question
is no, but I'll explain a bit more detail just in case you see an
opening:

On my form I have an input type="file" upload control and a number of
text boxes. I also have a select control to select the JobType. The
number of text boxes depends on the JobType. When the user selects a
JobType I do an "AutoPostBack" (asp.net c#) to refresh the page with
the correct text boxes. Unfortunately if the user has already browsed
for a file to upload, this is cleared with the refresh.

If the user changes the JobType I'd like to check if the file upload
control contains a value and, if so, warn the user that the control
will be cleared. I'd like to give him the chance to cancel and to
return the JobType to it's value before the last change, and not do the
auto call back.
Am I asking too much?

I'm not familiar with ASP.NET or C#, I imagine that an onchange
function is being created for the client-side page that either posts
back the form or uses XMLHTTPrequest to get new content for the page.

Either way, you need to post whatever is being delivered to the client.
The code I posted above conditionally changes a select back to the
previous selection. You need to insert something like that into the
onclick generated by the ASP page (if there is one) or code it
yourself.

Although you can't change the file input's value, you can read it. So
test if it's empty and respond accordingly:


<form name="formA" action="">
<input type="file" name="xx"><br>
<select name="selA" onchange="
if ( '' != this.form.xx.value )
if ( ! confirm('Proceed Yes or No')){
// Return selection to previous & don't proceed
this.selectedIndex = selIdx;
return;
}
}
// Remember selection, do stuff 'cos user said OK
// or hasn't selected a file yet
selIdx = this.selectedIndex;
// ...
}
">
<option selected>option 1</option>
<option>option 2</option>
<option>option 3</option>
</select>
</form>
<script type="text/javascript">
// Set selIdx to default selected index on page load
var selIdx = document.formA.selA.selectedIndex;
</script>


An alternative is to put all of your fields in the form, then hide or
show groups of them depending on which selection the user has made.
Then they don't loose anything that they've already filled in.

This approach should display all the fields by default and use script
to hide them. Then if a user doesn't have scripting enabled, they can
still use the form (autpostback depends on client-side scripting).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,043
Members
48,769
Latest member
Clifft

Latest Threads

Top