Wrox Programmer Forums
|
BOOK: Beginning JavaScript
This is the forum to discuss the Wrox book Beginning JavaScript by Paul Wilton; ISBN: 9780764544057
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning JavaScript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 29th, 2003, 02:22 PM
Registered User
 
Join Date: Oct 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using confirm() in a form

I am trying to use confirm() to "confirm" whether a user wants to proceed. I am unable to even get the prompt to appear and the form is submitted without any popup. I am new to Javascript and I can't seem to locate where the book talks about Confirm(). Please tell me a page number if known. I know that my browser supports confirms because they popup on different websites that I go to. I have even tried it as onClick in the submit input tag itself, didn't work. Any help would be greatly appreciated.

I have tried many variations of this with no luck.
Here is the script I am using:
'...
<HEAD>
<SCRIPT LANGUAGE="Javascript">
function OnSubmit_Timesheet()
{
    var returnvalue = confirm("Really create a timesheet for the week of " + weekDayStart + ?");
    if(returnvalue)
    {
        return true;
    }
    else
    {
        return false;
    }
}
</SCRIPT>
</HEAD>
<BODY>
'...
<FORM ACTION='Finaltimesheet.asp' METHOD='post' NAME=timesheet onSubmit='return OnSubmit_Timesheet()'>
'...
<INPUT TYPE=Submit VALUE="Create Timesheet">
</BODY>


Thank you,
Jason
 
Old October 29th, 2003, 05:30 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

At first I thought that it didn't call the JavaScript and submit directly, but the JavaScript fires, but has an error:

 var returnvalue = confirm("Really create a timesheet for the week of " + weekDayStart + ?");

should have an additional quote:

 var returnvalue = confirm("Really create a timesheet for the week of " + weekDayStart + "?");

You are adding the ? as a string, so it must be enclosed in quotes.

As soon as you add the quote, you'll get another error (weekDayStart is undefined). Where is this variable declared? Where does it get its value?

For easy debugging, add an alert statement as the first line of the code. This way you can make sure your function does get called.
You may also need to check the option "display a notification about every script error" in the options dialog of your browser (if you are using IE).



HtH,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old October 29th, 2003, 07:59 PM
Registered User
 
Join Date: Oct 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot. I added the other " in before the ?. This alone does not make it work. I do delcare the weekDayStart variable in the body of the page, do I need to define it in the script tag(or function) , as well? I am now on a computer that displayed the error (The "). I noticed it when I just went to the page. Any other ideas as to why it does not popup? I tried adding an alert with the same message, it did not popup either. I also tried changing the confirm to an alert with no result. Would that mean that the function is not being called? Is the General syntax for this type of confirm () correct? I want it to submit if they press OK, and go back to the page if they seelect cancel.

Thanks again for the help.

Thank you,
Jason
 
Old October 30th, 2003, 04:11 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Yes, the confirm is OK, and it will work when you declare weekDayStart somewhere, for example like this:

<script language="JavaScript" type="text/javascript">
var weekDayStart;
  function OnSubmit_Timesheet()
  {
      var returnvalue = confirm("Really create a timesheet for the week of " + weekDayStart + "?");
      if(returnvalue)
      {
          return true;
      }
      else
      {
          return false;
      }
  }
</script>

This way, weekDayStart is declared outside the function, so you can use it from within the function and from other parts of your page. Make sure it gets a valid value, or give it a default value, like:

var weekDayStart = "1";

because otherwise the confirm message will use "undefined" as the value for weekDayStart.

Did you look at the debugging option for JavaScript on your browser? Do you have Norton Internet Security installed? They both can prevent JavaScript error messages from appearing....

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old October 30th, 2003, 12:57 PM
Registered User
 
Join Date: Oct 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have script debugging on. They popup on other projects that I do. They are helpful most of the time. I think I figured out how they work. It gives me an error on the line that the button is on with the onClick, and I figured out that it meant there was an error in the function, not the line. This is from another webpage.

Anyway...

I noticed that in your example, you have var weekDayStart; in the script tag. My script tag is in the Head tag, and I Dim the variable weekDayStart in the Body tag in ASP. Does that matter? This whole page is ASP Except for that script tag. I am used to ASP, I think that is why I have some trouble with Javascript. Shoud I var another variable in ths script tag and set it equal to the weekDayStart variable I Dim in the ASP? Such as:

<script language="JavaScript" type="text/javascript">
  var Monday = WeekDayStart
  function OnSubmit_Timesheet()
  {
      var returnvalue = confirm("Really create a timesheet for the week of " + Monday + "?");
      if(returnvalue)
      {
          return true;
      }
      else
      {
          return false;
      }
  }
</script>

I am not sure how one would set var Monday equal to a variable Dim-med in the body of the page. But is that what I should do? Keep in mind, I do not even get the popup, so I don't think the function is being called. Concerning "undefined", the weekDayStart variable is set to what someone selects from a drop down list. The varaiable will always be a Monday's date in the form of 10/20/2003.

I appreciate your help with this.


Thank you,
Jason
 
Old October 30th, 2003, 01:27 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Aha, I see. You're trying to pass a server side ASP variable to client side JavaScript, right?

You'll need to write out the ASP variable, so it ends up in your JavaScript. Try this:
Code:
<script language="JavaScript" type="text/javascript">
  var weekDayStart = <%=ServerSideWeekDayStart%>;
  function OnSubmit_Timesheet()
  {
      var returnvalue = confirm("Really create a timesheet for the week of " + weekDayStart + "?");
      if(returnvalue)
      {
          return true;
      }
      else
      {
          return false;
      }
  }
</script>
This will write out the ASP variable ServerSideWeekDayStart so it ends up in JavaScript like this:

var weekDayStart = 4;

If ServerSideWeekDayStart is a string, you'll need to change it to this:

var weekDayStart = '<%=ServerSideWeekDayStart%>';

so it ends up as:

var weekDayStart = 'monday';


Does that help?

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old October 31st, 2003, 09:35 AM
Registered User
 
Join Date: Oct 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey, I got it to work.

All I did was change it to :

var returnvalue = confirm("Really create a timesheet for the week selected?");

Turned out to be that variable that was causing problems.

Thanks for all your help. Now that I got it to work, I can look in to adding that variable in other ways.



Thank you,
Jason





Similar Threads
Thread Thread Starter Forum Replies Last Post
Confirm in ItemDataBound rstelma ASP.NET 1.0 and 1.1 Basics 3 December 6th, 2006 02:10 AM
Confirm messagebox edwin.debrouwere ASP.NET 2.0 Basics 0 September 5th, 2006 07:29 AM
confirm crmpicco Javascript How-To 2 February 7th, 2005 01:23 PM
HTML Confirm VSnewbie Javascript How-To 3 August 30th, 2004 10:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.