JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 4th, 2008, 05:56 AM
acidedge2004 acidedge2004 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 139 acidedge2004 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 32 m 12 sec
Reputation Power: 6
Problems with passing integer variables from HTML form to a function

Hi, I have started learning javascript yesterday and so got the basics down. But I wanted to get a bit more complicated today, using variable passing with alert boxes etc. So, I wrote the script to pass set variables from one script to another after a button was clicked, and then display it in an alert box.

Well, now I want to make it so the user can choose the variables themselves. So I created a simple HTML form with two input boxes, which was then supposed to be sent to the functions to be multiplied and displayed in the alert box.

Here is what I had:
Code:
<html>
<head>

<script type="text/javascript">
	function func_math(x,y)
	{
	return x*y;
	}
</script>

<script type="text/javascript">
	function func_alert()
	{
	alert("The number is " + func_math(2,3));
	}
</script>


</head>
<body>

<input type="button" onclick="func_alert()" value="click">

</body>
</html>


So this work perfectly, no problems what so ever. But, now I tried to use a HTML form to allow users to choose the variables, which is easy if I wanted to POST to another page, but I do not, I just want the alert box to display. I have gone through many different versions and used different approaches, tried to use resources but found nothing that helped They had passing variables, even to alert boxes, but they were just text so did not have to be manipulated first.

this is what I have now,

Code:
<html>
<head>

<script type="text/javascript">
	function func_math(x,y)
	{
	return z=x*y;
	}
</script>

<script type="text/javascript">
	function func_alert()
	{
	alert("The number is " + func_math(z));
	}
</script>


</head>
<body>

<form name="numform" >
x:
<input type="integer" name="x" value="x">
y:
<input type="integer" name="y" value="y">
<input type="submit" value="click" onsubmit="func_alert(x,y)">
</form>


</body>
</html>


Now, the html form displays no probs, but clicking submit just leads to the page being reloaded and no display box appears or even error messages. Can any one give me a hand or point me in the right direction?

Thanks for any help received, Mike

Reply With Quote
  #2  
Old January 4th, 2008, 09:19 AM
lokisapocalypse lokisapocalypse is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2005
Posts: 623 lokisapocalypse User rank is First Lieutenant (10000 - 20000 Reputation Level)lokisapocalypse User rank is First Lieutenant (10000 - 20000 Reputation Level)lokisapocalypse User rank is First Lieutenant (10000 - 20000 Reputation Level)lokisapocalypse User rank is First Lieutenant (10000 - 20000 Reputation Level)lokisapocalypse User rank is First Lieutenant (10000 - 20000 Reputation Level)lokisapocalypse User rank is First Lieutenant (10000 - 20000 Reputation Level)lokisapocalypse User rank is First Lieutenant (10000 - 20000 Reputation Level)lokisapocalypse User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 5 Days 22 h 58 m 20 sec
Reputation Power: 144
I would just change <input type="submit"... to <input type="button"...

Reply With Quote
  #3  
Old January 4th, 2008, 09:23 AM
RyanT RyanT is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 71 RyanT User rank is Sergeant (500 - 2000 Reputation Level)RyanT User rank is Sergeant (500 - 2000 Reputation Level)RyanT User rank is Sergeant (500 - 2000 Reputation Level)RyanT User rank is Sergeant (500 - 2000 Reputation Level)RyanT User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 2 h 3 m 32 sec
Reputation Power: 13
Code:
<script type="text/javascript">
	function func_math()
	{
	var z=document.numform.x.value*document.numform.y.value;

	alert("The number is " + z);
	}
</script>


</head>
<body>

<form name="numform" >
x:
<input type="text" name="x">
y:
<input type="text" name="y">
<input type="button" value="click" onclick="func_math()">

</form>

Reply With Quote
  #4  
Old January 4th, 2008, 09:27 AM
acidedge2004 acidedge2004 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 139 acidedge2004 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 32 m 12 sec
Reputation Power: 6
Thanks. I found a solution to my problem on another site, but that was about the same length, and as complicated, as what I had originally. Yours is much much simpler, so will learn to do it that way instead. Thanks for your help, Mike

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Problems with passing integer variables from HTML form to a function

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap