Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

simple javascript on text field

Options
  • 21-02-2008 3:15pm
    #1
    Registered Users Posts: 26,581 ✭✭✭✭


    i'm doing a bit of javascript that's part of a cgi file that i need to have a text field that will contain a date 'YYYY-MM-DD' format and this date then needs to be stored in a cgi variable so that it can be passed to another cgi file.

    at the moment i have a text field that when you focus on it it pops up a little calendar so you can pick the date ie.
    my $myform = "
             <form>
                follow-up date:
                     <input type=\"text\" name=\"followupdate\" id=\"followupdate\" onfocus=\"showCalendarControl(this);\">
    

    that will work perfectly at displaying a box and when you click on it, a calendar pops up and when you select your date the date gets inputted into the text field in the format 'YYYY-MM-DD'.

    now i want to be able to extract that info, so that it can be stored in a perl variable.

    here's my javascript to get the date.
    function getFollowupDate()
        {
            var date = document.getElementById('followupdate');
            return date;
        }
    
    

    then to store it into a perl var.
    my $followupdate = "<script type=\"text/javascript\">getFollowupDate();</script>";
    

    then if i try to print it out i get nothing. anyone see where i'm going wrong?

    if a mod feels this is more of a programming issue can you please move it.

    thanks


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Use var date = document.getElementById('followupdate').value; to get the text inside the box rather than the box itself? Been a while since I played with Javascript, so could be completely wrong.


  • Registered Users Posts: 26,581 ✭✭✭✭Creamy Goodness


    ok so i have my javascript embedded in my cgi file like this and changed the .value as aidan said but to no avail.
    my $Loggedin='
    	<script type=text/javascript>
    		function getFollowupDate()
    		{
    			alert("hey!");
    			var date = document.getElementById(\'followupdate\').value;
    			document.write(date);
    			return date;
    		}
    	</script>
    	
    	...
    	...
    	...
    	...
    
    	';
    

    this is the form now i added an onblur=\"getFollowupDate();\" the following code is still in the same perl var as above.
    	<form>
    		follow-up date:
    		<input type=\"text\" name=\"followup\" id=\"followupdate\" onfocus=\"showCalendarControl(this);\" onblur=\"getFollowupDate();\" value=\"\">
    	</form>
    

    so getFollowupDate() should be called when the text box loses focus because of the onblur and i see the alert box.

    is there any way to store what gets returned from getFollowupDate() into a perl variable.


  • Registered Users Posts: 6,511 ✭✭✭daymobrew


    I can't help but feel that you are mixing up client and server side actions.

    You say that the date gets put into the text box after the popup is called. This textbox seems to be called 'followup'. So, in your perl CGI script:
    my $TheDateYouWant = param( 'followup' );
    

    Aside: it might be easier to put the Javascript in a separate file and simply include it in the HTML code. It will certainly reduce the amount of escaping of quotes you have to do.
    print $q->start_html(-title=>'Script Title', -script=>{-type=>'text/javascript', -src=>'/path/to/javascript/file.js'} );
    


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    Yup as daymo said it's a problem with your understanding of the server-client model. Solution provided above will work and as suggested move the scripts out to a .js file then all you need to do is call the function.

    -RD


  • Registered Users Posts: 26,581 ✭✭✭✭Creamy Goodness


    aye, got it now, i do have an external scripts file but i just put it in the cgi to test it was working first.

    thanks for your help guys.


  • Advertisement
Advertisement