Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Apr 2006
    Location
    At my desk
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Html Form and JavaScript

    Okay I'm at work so thats why I have not searched through the forum so I hope you don't go to hard on me (I know I usually flame people for that). Anyway what I am trying to do is enter data into a set of text boxes and have that info ran through a function in javascript. I made a simple demo just to see if I could even do this and have so far failed. I was hoping someone could help.

    Thanks in advanced,
    DenKain

    Code:
    <html>
    <head>
    <script language="JavaScript">
    
    function dude()
    {
    theText = my;
    alert(theText);
    }
    </script>
    </head>
    
    <body>
    
    <form>
    <input ="text" name="my">
    <input type ="submit" Value ="Submit" onclick="dude()">
    </form>
    
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Code:
    function dude()
    {
    	theText = document.forms[0].my.value;
    	alert(theText);
    }
    
    ...
    
    <form>
    	<input="text" name="my">
    	<input type="button" value="Submit" onclick="dude()">
    </form>
    (Incidentally, I changed the input button from a submit button to a generic button in order to prevent it submitting in this demo.)

  • #3
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/javascript">
    function dude()
    {
    	//this gets the value, forms[0] represents the first form on the page
    	theText = document.forms[0].my.value; 
    	//you can also use document.getElementById(), the input needs to have id="..."
    	theTextById = document.getElementById('my').value;
    	alert("This uses the name of the input: " + theText);
    	alert("This uses the id of the input: " + theTextById);
    	//this prevents the form from actually being submitted but still allowing use to hit enter or submit
    	return false; 
    }
    </script>
    </head>
    
    <body>
    <!--since the dude() function returns something we need to return that value to the form-->
    <form action="#" onSubmit="return dude()">
    <input type="text" name="my" id="my">
    <input type="submit" value="Submit">
    </form>
    </body>
    </html>
    Edit: Bill beat me to it

    Codes are similar but a bit different I prefer the submit button just because users can still hit the enter key or submit button.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #4
    New to the CF scene
    Join Date
    Apr 2006
    Location
    At my desk
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you so much that worked great. The thing is I applied that to the actual script I was making and it is having a separate issue. As you may notice when you run it the function starts even when it is not called to. Any ideas??

    Code:
    <html>
    <head>
    
    <script language="JavaScript">
    
    function theDate()
    {
    myMonth = document.forms[0].month.value; 
    myDay = document.forms[0].day.value;
    myYear = document.forms[0].year.value;
    MyDate = myMonth+"/"+myDay+"/"+myYear;
    //TargetDate = "5/3/2006 11:00 PM";
    
    TragetDate = "'MyDate' + 11:00 PM"; //yes thats pseduo code for the moment
    BackColor = "palegreen";
    ForeColor = "navy";
    CountActive = true;
    CountStepper = -1;
    DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
    FinishMessage = "It is finally here!";
    }
    </script>
    <script language="JavaScript" src="http://www.mythicsoftware.com/work/countdown.js"></script>
    
    </head>
    <body>
    
    <form action="#" onSubmit="return theDate">
    	
    Month:
    <input ="text" size = "2" name="month">
    <br>
    Day:
    <input ="text" size = "2" name="day">
    <br>
    Year:
    <input ="text" size = "4" name="year">
    <br>
    
    <input type ="submit" Value ="Submit">
    
    </form>
    
    <script language="JavaScript">
    
    myMonth = document.forms[0].month.value; 
    myDay = document.forms[0].day.value;
    myYear = document.forms[0].year.value;
    MyDate = myMonth+"/"+myDay+"/"+myYear;
    //TargetDate = "5/3/2006 11:00 PM";
    
    TragetDate = MyDate;
    BackColor = "palegreen";
    ForeColor = "navy";
    CountActive = true;
    CountStepper = -1;
    DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
    FinishMessage = "It is finally here!";
    </script>
    <script language="JavaScript" src="http://www.mythicsoftware.com/work/countdown.js"></script>
    
    </body>
    </html>
    Last edited by DenKain; 05-04-2006 at 02:47 AM.

  • #5
    New to the CF scene
    Join Date
    Apr 2006
    Location
    At my desk
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Any ideas, anyone?

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    onSubmit="return theDate"

    should be

    onSubmit="return theDate()"
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    New to the CF scene
    Join Date
    Apr 2006
    Location
    At my desk
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is what I was trying to do....still can't get the time to work though.

    inputs.php
    Code:
    <HTML>
    
    <FORM action="countdown.php" method="POST">
    
    Enter Month: <INPUT type="text" name="month"><BR>
    
    Enter Day:<INPUT type="text" name="day"><BR>
    
    Enter Year:<INPUT type="text" name="year"><BR>
    
    <INPUT type="submit" name="submit" value="Submit">
    
    </FORM>
    
    </HTML>
    countdown.php
    Code:
    <html>
    <head>
    </head>
    <body>
    
    <script language="JavaScript">
    
    TargetDate = 
    
    <?PHP 
    
    echo CHR(34) . $_POST["month"] . CHR(47) . CHR(34) . CHR(43); 
    
    echo CHR(34) . $_POST["day"] . CHR(47) . CHR(34) . CHR(43);
    
    echo CHR(34) . $_POST["year"] . CHR(34) . CHR(43);
    
    ?> " 10:30 PM";
    
    BackColor = "palegreen";
    
    ForeColor = "navy";
    
    CountActive = true;
    
    CountStepper = -1;
    
    DisplayFormat = "%%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
    
    FinishMessage = "It is finally here!";
    
    </script>
    
    <script language="JavaScript" src="http://www.mythicsoftware.com/work/countdown.js"></script>
    
    </body>
    </html>


  •  

    Posting Permissions

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