Closed Thread
Results 1 to 3 of 3

Thread: How do I use a variable in a function from a previous function?

  1. #1
    system32 is offline Newbie
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    How do I use a variable in a function from a previous function?

    I am having very much difficulty trying to use a variable that I made in a previous function to work with my new function. I have tried to make the variable global but this is not working and I have no idea why. Can anyone please help me out?

    Thanks

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,494
    Blog Entries
    75
    Rep Power
    143

    Re: How do I use a variable in a function from a previous function?

    You'll have to provide some code before we can tell you what went wrong.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    system32 is offline Newbie
    Join Date
    Feb 2010
    Posts
    24
    Rep Power
    0

    Re: How do I use a variable in a function from a previous function?

    here is my Javascript code:

    Code:
    	function checkProvince()
    	{
    		var myForm = document.pizzaForm;	
    		if (myForm.province.value == "Nunavut" || myForm.province.value == "Yukon")
    			{
    				alert("We are sorry but we do not deliver here.");
    			}
    	}
    	
    	function checkName()
    	{
    		var errMessage = " ";
    		var checkk = 0;
    		var myForm = document.pizzaForm;
    		var rePhone = /^\(?(\d{3})\)?[\.\-\/\s]?(\d{3})[\.\-\/\s]?(\d{4})$/ ;
    		var rePostal = /^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/;
    		//var reEmail = /^(\w)+\@(\w).(\w)$/;
    		var validPhone = document.pizzaForm.telephone;
    		var validPostal = document.pizzaForm.postalCode;
    		var pizzaAmount = document.pizzaForm.pizzaNumber.value
    		//var validEmail = document.pizzaForm.email;
    		var finalAmount = " ";
    		
    		if (myForm.firstName.value == "" || myForm.lastName.value == "")
    			{
    				errMessage += "Please type in your name\n";
    			}
    			if (myForm.address.value == "")
    			{
    				errMessage += "Please type in your address\n";
    			}
    			if (myForm.city.value == "") 
    			{
    				errMessage += "Please type in your city\n";
    			}
    			if (myForm.postalCode.value == "")
    			{
    				errMessage += "Please type in your postal code\n";	
    			}
    			if (myForm.province.value == "")
    			{
    				errMessage += "Please select a province\n";
    			}
    			if (myForm.telephone.value == "")
    			{
    				errMessage += "Please type in a phone number\n";
    			}
    			if (myForm.email.value == "")
    			{
    				errMessage += "Please type in your email\n";
    			}
    
    			if (!rePhone.test(validPhone.value))
    			{
    				errMessage += "Please type in a valid phone number, such as: 519-555-5555\n";
    			}
    			if (!rePostal.test(validPostal.value.toUpperCase()))
    			{
    				errMessage += "Please type in a valid postal code, such as: N2N3M8\n";
    			}
    			/*if (!reEmail.test(vaildEmail.value))
    			{
    				errMessage += "Please enter a vaild email such as: example@example.com\n";
    			}*/
    			if (isNaN(myForm.pizzaNumber.value))
    			{
    				errMessage += "Please enter a valid number of pizzas\n";
    			}
    			else if (myForm.pizzaNumber.value == 0)
    			{
    				errMessage += "You must order at least one pizza\n";	
    			}
    			
    			//Check to see the if the Pizza Size has been selected
    			 if (!(document.pizzaForm.pizzaSize[0].checked) && 
    												!(document.pizzaForm.pizzaSize[1].checked) && 
    												!(document.pizzaForm.pizzaSize[2].checked) && 
    												!(document.pizzaForm.pizzaSize[3].checked))
    			 {
    				 errMessage += "You must select a pizza size\n";
    			 }
    			 
    			 
    			 //calculate order
    			 if (!(errMessage) && (document.pizzaForm.pizzaSize[0].checked))
    			 {
    				 finalAmount += pizzaAmount * 5;
    			 }
    			 else if (document.pizzaForm.pizzaSize[1].checked)
    			 {
    				 finalAmount += pizzaAmount * 10;
    			 }
    			 else if (document.pizzaForm.pizzaSize[2].checked)
    			 {
    				 finalAmount += pizzaAmount * 15;
    			 }
    			 else if (document.pizzaForm.pizzaSize[3].checked)
    			 {
    				 finalAmount += pizzaAmount * 20
    			 }
    			 
    			
    			if (errMessage != "")
    			{
    				alert(errMessage);
    			}
    									
    	}
    	
    	
    	//check to see if more then 5 toppings are selected
    	function checkTopping()
    	{
    		var boxCheck = 0;
    		for (var i=1;i<=11;i++)
    		{
    			boxName = "topping" + i;
    			var toppingAmount = boxCheck * 0.75;
    			if (document.pizzaForm[boxName].checked)
    			{
    				boxCheck++;
    			}
    			
    		}
    		if (boxCheck == 0)
    		{
    			alert( "you must select at least one topping");
    		}
    		else if (boxCheck > 5)
    		{
    			alert( "You can only select a maximum of 5 toppings");
    			return false;	
    		}
    		alert ("Total cost for topping will be: $" + toppingAmount);
    	}
    	
    	
    	//To call all functions togther
    	function allFunctions()
    	{
    		checkTopping();
    		checkName();
    	}
    	
    </script>
    This is in my BODY Tag:
    Code:
    <form action="pizzaForm" method="get" name="pizzaForm" id="pizzaForm" onSubmit="return allFunctions()">
    <table width="475" border="0">
        <tr>
          <td width="329">
    <fieldset style="width:250px">
    <legend align="top">Personal Information</legend>
    First Name <input name="firstName" type="text" id="firstName"  /><br />
    Last Name <input name="lastName" type="text" id="lastName"  /><br/>
    Address <input name="address" type="text" id="address"  /><br />
    City <input name="city" type="text" id="city"  /><br />
    Province <select name="province">
      <option value="">Please Select a Province</option>
      <option>Alberta</option>
      <option>British Columbia</option>
      <option>Manitoba</option>
      <option>New Brunswick</option>
      <option>Newfoundland and Labrador</option>
      <option>Northwest Territories</option>
      <option>Nova Scotia</option>
      <option onClick="checkProvince()">Nunavut</option>
      <option>Ontario</option>
      <option>Prince Edward Island</option>
      <option>Québec</option>
      <option>Saskatchewan</option>
      <option onClick="checkProvince()">Yukon</option>
    </select><br/>
    Postal Code <input name="postalCode" size="9" maxlength="6" type="text" id="postalCode"  /><br/>
    Telephone <input name="telephone" type="text" id="telephone"  /><br />
    Email <input name="email" type="email" id="email" />
    </fieldset>
    
    <!--Order Information Groupbox -->
    </td>
          <td width="130">
    <fieldset style="width:300px">
    <legend align="top">Order Information</legend>
    Number Of Pizza's <input name="pizzaNumber" maxlength="2" type="text" id="pizzaNumber"  /><br /><br  />
    Size<br />
    <label>
          <input name="pizzaSize" type="radio" value="radio"  />
          Small</label>
        <br />
        <label>
          <input type="radio" name="pizzaSize" value="radio"  />
          Medium</label>
        <br />
        <label>
          <input type="radio" name="pizzaSize" value="radio"  />
          Large</label>
        <br />
        <label>
          <input type="radio" name="pizzaSize" value="radio" />
          Extra Large</label><br  /><br /><br />
    Crust Type <select name="crustType">
    <option>Hand-Tossed Pizza</option>
    <option>Pan Pizza</option>
    <option>Stuffed Crust</option>
    <option>Thin Crust</option>
    </select><br /><br />
    Toppings<br />
    <input name="topping1" type="checkbox" id="sausage" />Sausage
    <input name="topping2" type="checkbox" id="pepperoni" />Pepperoni
    <input name="topping3" type="checkbox" id="beef" />Beef
    <input name="topping4" type="checkbox" id="bacon" />Bacon<br />
    <input name="topping5" type="checkbox" id="chicken" />Chicken
    <input name="topping6" type="checkbox" id="ham" />Ham
    <input name="topping7" type="checkbox" id="olives" />Olives
    <input name="topping8" type="checkbox" id="pepper" />Peppers<br />
    <input name="topping9" type="checkbox" id="tomatoes" />Tomatoes
    <input name="topping10" type="checkbox" id="mushrooms" />Mushrooms
    <input name="topping11" type="checkbox" id="pinapple" />Pinapple<br /><br />
    Extra<br />
    <input name="extra" type="checkbox" id="extra" />Add small (5 wings) for an additional $2.00
    </fieldset>
    <input name="Submit" type="button" id="submit" value="Submit"  onClick="allFunctions()" />
    <input name="Reset" type="reset" id="reset" value="Reset" />
    </td>
        </tr>
      </table>
    </form>
    I want to finalize the validation so that once all fields are validated i can show an alert of how much the total cost of the order will be. I want to use the variable toppingAmount that is in the checkTopping function in my checkName function so I can use it and show an alert to show the total cost of the order. I tried to place it outside of all functions to make it global but that is not working. Could you please help me?

    Thanks

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. MCQ: variable declared in arguments of this function
    By jackson6612 in forum C and C++
    Replies: 8
    Last Post: 06-05-2011, 07:14 AM
  2. Replies: 10
    Last Post: 05-17-2011, 04:16 AM
  3. how to write function for variable arguments
    By sai in forum Managed C++
    Replies: 1
    Last Post: 01-03-2011, 10:10 AM
  4. Replies: 3
    Last Post: 11-12-2010, 05:47 PM
  5. Passing an ifstream variable to a function?
    By KeilanS in forum C and C++
    Replies: 2
    Last Post: 11-16-2008, 04:11 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts