Button onClick + Confirm problem...

@

@sh

Can anyone help out here please, we have a button that when pressed will
alert the user, should they cancel no action is taken, however should they
confirm, the script will disable the button and then submit the form...

Could someone correct this lump of code for us pleaseeeeeeee ;o)
<input name="Submit" type="button" class="EmployerPostJobForm" value="
Verify Job " id="mFormSubmit" onClick="if(confirm('You are about to verify
this job, doing this will put the job live on the site and it will be
removed from this section of the site and moved to the edit jobs pages. It
will also email all candidates that are interested in this kind of job, are
you sure you wish to continue?'))return document.Verify<%= TheFormCounter
%>.mFormSubmit.disabled = true;document.Verify<%= TheFormCounter
%>.Submit();else return false;">

If we simply disable the button it works fine, however if we then try to
append onto that the form submitting action, or infact any other action, it
doesn't like it - so we're having difficulties making the result of the
Confirm action two Javascript tasks.

Many thanks

Cheers, @sh
 
L

Lee

@sh said:
Can anyone help out here please, we have a button that when pressed will
alert the user, should they cancel no action is taken, however should they
confirm, the script will disable the button and then submit the form...

Could someone correct this lump of code for us pleaseeeeeeee ;o)
<input name="Submit" type="button" class="EmployerPostJobForm" value="
Verify Job " id="mFormSubmit" onClick="if(confirm('You are about to verify
this job, doing this will put the job live on the site and it will be
removed from this section of the site and moved to the edit jobs pages. It
will also email all candidates that are interested in this kind of job, are
you sure you wish to continue?'))return document.Verify<%= TheFormCounter
%>.mFormSubmit.disabled = true;document.Verify<%= TheFormCounter
%>.Submit();else return false;">

If we simply disable the button it works fine, however if we then try to
append onto that the form submitting action, or infact any other action, it
doesn't like it - so we're having difficulties making the result of the
Confirm action two Javascript tasks.

In the first place, that's far too much script to squeeze into an
onclick attribute. It would be easier to spot your problem and to
maintain the code if you called a function to perform these tasks.

Secondly, here's the logic of your code in a more readable form:

if(confirm(longTextString))
return document.VerifyX.mFormSubmit.disabled=true;
document.VerifyX.Submit();
else
return false;

There are a few problems with that code.
1. If the confirm() returns true, the Submit() method is not called
because the function returns.
2. I suspect that there isn't really a Submit() method defined for
this form. Did you mean "submit()". When posting code and asking
people to spot your errors, you absolutely MUST copy and paste the
exact code, not re-enter it by hand.
3. Since there is a statement between the "if" block and the "else",
you've got a syntax error.
 
W

web.dev

Hi Ash,

@sh said:
Can anyone help out here please, we have a button that when pressed will
alert the user, should they cancel no action is taken, however should they
confirm, the script will disable the button and then submit the form...

Maybe you meant, "when they take no action, disable the button", "when
they confirm, submit the form". It's just, there's no point in
disabling the button when you're submitting the form anyway. So I'll
be assuming what I said is true.
Could someone correct this lump of code for us pleaseeeeeeee ;o)
<input name="Submit" type="button" class="EmployerPostJobForm" value="
Verify Job " id="mFormSubmit" onClick="if(confirm('You are about to verify
this job, doing this will put the job live on the site and it will be
removed from this section of the site and moved to the edit jobs pages. It
will also email all candidates that are interested in this kind of job, are
you sure you wish to continue?'))return document.Verify<%= TheFormCounter
%>.mFormSubmit.disabled = true;document.Verify<%= TheFormCounter
%>.Submit();else return false;">
[snip]

This may be the solution you're looking for:

The javascript:

function doConfirm(myForm)
{
var result = confirm("...your question...");

if(result)
{
return true;
}
else
{
myForm.elements["mFormSubmit"].disabled = true;
return false;
}
}

Your form:
<form method = "post" onsubmit = "return doConfirm(this);">
....your other form elements...
<input type = "submit" value = "Submit" id = "mFormSubmit" name =
"mFormSubmit"/>
</form>
 
@

@sh

Many thanks for that, much appreciated - we were thinking that a function is
required but Javascript and its 'exact' nature frustrates me severely and so
generally we give up.... ;o)

Perhaps I'll stick at it next time.

Thanks again.

Cheers, Ash
 
@

@sh

Thanks Lee for your reply, we did think it'd be a little too much to put
into an onClick, eitherway is it possible anyway for future reference, or
can an onClick only run a single process upon a true/false?

Cheers, Ash
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top