www.webdeveloper.com
Results 1 to 10 of 10

Thread: Automatically fill in date in form

  1. #1
    Join Date
    Aug 2014
    Posts
    5

    Automatically fill in date in form

    Hello, I have an html form and one of the fields is date. I have the option to open a calendar and click the date to be inserted in the field, but i am trying to get it to be auto filled also if possible with the current date. My code is:

    <li class="mainForm" id="fieldBox_2">
    <label class="formFieldQuestion">DAY&nbsp;*&nbsp;<a class=info href=#><img src=imgs/tip_small.png border=0><span class=infobox>Ticket date</span></a>
    <input name=field_2 type=text id=field_2 autocomplete="on" value="">
    </label>
    <button type=reset class=calendarStyle id=fieldDateTrigger_2></button><SCRIPT type='text/javascript'> Calendar.setup({
    inputField : "field_2",
    ifFormat : "%m/%d/%Y",
    showsTime : false,
    button : "fieldDateTrigger_2",
    singleClick : true,
    step : 1
    });</SCRIPT></li>

    Any ideas how i can make it auto fill the date?

  2. #2
    Join Date
    Nov 2010
    Posts
    1,084
    add this to your script (relies on user's computer being set to the right date...)
    Code:
    var d = new Date();
    document.getElementById("field_2").value=(d.getMonth()+1)+"/"+d.getDate()+"/"+d.getFullYear();

  3. #3
    Join Date
    Aug 2014
    Posts
    5
    Thank you my friend, it worked perfect. I was trying to do it by getting the date from php server, but it didn't work

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,346
    Well that depends on how you were trying to use PHP. Getting date information out of PHP in to any part of the document is easy, the problems arise when you don't consider certain things like now you have to set your server location or a reference to a location so that the date function can actually churn out a reliable date.

    PHP Code:
    <?php
    date_default_timezone_set
    ('Europe/London');

    $dateSet date("d/m/Y");

    echo <<<HEREDOC

        <input name="field_2" type="text" id="field_2" value="
    {$dateSet}">

    HEREDOC;

    ?>
    the HEREDOC method allows you to output large strings of HTML with data inserted in to them.

    So how hard was that?
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  5. #5
    Join Date
    Feb 2010
    Posts
    22
    I was just about to ask this question and the answer's already there! Props to \\.\ for posting the answer. YOU ROCK!

    Quote Originally Posted by \\.\ View Post
    Well that depends on how you were trying to use PHP. Getting date information out of PHP in to any part of the document is easy, the problems arise when you don't consider certain things like now you have to set your server location or a reference to a location so that the date function can actually churn out a reliable date.

    PHP Code:
    <?php
    date_default_timezone_set
    ('Europe/London');

    $dateSet date("d/m/Y");

    echo <<<HEREDOC

        <input name="field_2" type="text" id="field_2" value="
    {$dateSet}">

    HEREDOC;

    ?>
    the HEREDOC method allows you to output large strings of HTML with data inserted in to them.

    So how hard was that?

  6. #6
    Join Date
    Aug 2014
    Posts
    5
    Quote Originally Posted by \\.\ View Post
    Well that depends on how you were trying to use PHP. Getting date information out of PHP in to any part of the document is easy, the problems arise when you don't consider certain things like now you have to set your server location or a reference to a location so that the date function can actually churn out a reliable date.

    PHP Code:
    <?php
    date_default_timezone_set
    ('Europe/London');

    $dateSet date("d/m/Y");

    echo <<<HEREDOC

        <input name="field_2" type="text" id="field_2" value="
    {$dateSet}">

    HEREDOC;

    ?>
    the HEREDOC method allows you to output large strings of HTML with data inserted in to them.

    So how hard was that?
    I am sorry, but i am not that advanced and i do not understand your example. Can you please show me using my script, how i would add yours to make it work?

  7. #7
    Join Date
    Feb 2010
    Posts
    22
    Try this...
    REMOVE THIS:
    <li class="mainForm" id="fieldBox_2">
    <label class="formFieldQuestion">DAY***<a class=info href=#><img src=imgs/tip_small.png border=0><span class=infobox>Ticket date</span></a>
    <input name=field_2 type=text id=field_2 autocomplete="on" value="">
    </label>
    <button type=reset class=calendarStyle id=fieldDateTrigger_2></button><SCRIPT type='text/javascript'> Calendar.setup({
    inputField : "field_2",
    ifFormat : "%m/%d/%Y",
    showsTime : false,
    button : "fieldDateTrigger_2",
    singleClick : true,
    step : 1
    });</SCRIPT></li>



    REPLACE WITH THIS (Making sure you change the date_default_timezone_set to your timezone as per the timezone supported list at http://php.net/manual/en/timezones.php

    <li class="mainForm" id="fieldBox_2">
    <label class="formFieldQuestion">DAY***<a class=info href=#><img src=imgs/tip_small.png border=0><span class=infobox>Ticket date</span></a>
    <input disabled id="field_2" type="text" name="field_2" value="<?php date_default_timezone_set('America/Chicago');echo date("m/d/Y"); ?>"/>
    </label>
    <button type=reset class=calendarStyle id=fieldDateTrigger_2></button>
    </li>

    It will not only post the date on the form, it will also disable it to prevent editing of that form input box only, to enable it, simple remove disabled after <input

    Good Luck!

  8. #8
    Join Date
    Aug 2014
    Posts
    5
    <li class="mainForm" id="fieldBox_2">
    <label class="formFieldQuestion">DAY&nbsp;*&nbsp;<a class=info href=#><img src=imgs/tip_small.png border=0><span class=infobox>Ticket date</span></a>
    <input id="field_2" type="text" name="field_2" value="<?php date_default_timezone_set('Europe/Paris');echo date("m/d/Y"); ?>"/>
    </label>
    <button type=reset class=calendarStyle id=fieldDateTrigger_2></button>
    </li>
    Above code gives me this result in the form

    DAY * <?php date_default_timezone_set('Europe/Paris');echo date("/>

    I would add a print screen, but i cannot see the option here.

  9. #9
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,346
    My example uses a method that allows you to output multiple lines of HTML, you will find that allot of people will echo a line of HTML at a time which is time consuming for the PHP parser and it is untidy in the editor.

    As I don't know what your code is supposed to be doing I will assume the following will do what you want.

    PHP Code:
    date_default_timezone_set('Europe/London');

    $dateSet date("d/m/Y");

    echo <<<HEREDOC
    <li class="mainForm" id="fieldBox_2">
        <label class="formFieldQuestion">DAY&nbsp;*&nbsp;
            <a class=info href=#><img src=imgs/tip_small.png border=0>
                <span class=infobox>Ticket date</span>
            </a>
        <input name="field_2" type="text" id="field_2" value="
    {$dateSet}">
        </label>
    </li>
    HEREDOC; 
    As the date will be set by the server, you won't need some trigger script to set the date. I would however advise that your code is set to the correct country so that you won't induce a London timing and also your date format is set as DD/MM/YYYY, so you would need to alter that string to return the date in the format you want.

    You might want to consider formatting your coding so that it is more readable instead of a glob of text that makes it difficult to see what is going on and when you post code in the forum, please use the forum [CODE] [/CODE] tags for things like JavaScript, CSS and the HTML and PHP tags in the same manner for their respective code sets, makes for easier reading.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  10. #10
    Join Date
    Aug 2014
    Posts
    5
    Hello \\.\
    I don't want to clog the thread with the code for the whole page, but it is nothing spectacular. Basically it is just an entry form that uses a php page to insert data on to a mysql database. I got most of the values to be selectable by a list, and there is the calendar applet for the date. I thought however to get that to fill in automatically (as well of course being able to manually enter it or select it from the applet). The code xelawho gave me works just fine and gives me what i need.
    I like learning new things though and i am trying to understand how your code works. So i am basically trying to substitute the code for DAY bellow with yours. I have included also the next option of the page which is a list:

    Code:
    </head>
    
    	<body onLoad="collapseAll()">
    
    	<div id="mainForm">
    
    
    
    
    		<div id="formHeader">
    				<h2 class="formInfo">SOS</h2>
    				<p class="formInfo">Ticket Follow-up</p>
    						</div>
    		<BR/><!-- begin form -->
    		<form method=post enctype=multipart/form-data action=processor.php onSubmit="return validatePage1();"><ul class=mainForm id="mainForm_1">
    
    				<li class="mainForm" id="fieldBox_1">
    					<label class="formFieldQuestion">TICKET&nbsp;*&nbsp;<a class=info href=#><img src=imgs/tip_small.png border=0><span class=infobox>Ticket Number</span></a>
    					  <input class=mainForm type=text name=field_1 id=field_1 size='20' value=''>
    					</label>
    				</li>
    
    				<li class="mainForm" id="fieldBox_2">
    		  <label class="formFieldQuestion">DAY&nbsp;*&nbsp;<a class=info href=#><img src=imgs/tip_small.png border=0><span class=infobox>Ticket date</span></a>
    		    <input  name=field_2 type=text id=field_2 autocomplete="off" value="">
    		  </label>
    		  <button type=reset class=calendarStyle id=fieldDateTrigger_2></button><SCRIPT type='text/javascript'> 
    		  Calendar.setup({
    								inputField     :    "field_2",   
    								ifFormat       :    "%m/%d/%Y",   
    								showsTime      :    false,          
    								button         :    "fieldDateTrigger_2",
    								singleClick    :    true,           
    								step           :    1                
    								});</SCRIPT></li>
    
    				<li class="mainForm" id="fieldBox_3">
    		  <label class="formFieldQuestion">PERSON&nbsp;*&nbsp;<a class=info href=#><img src=imgs/tip_small.png border=0><span class=infobox>Created by</span></a>
    		    <select class=mainForm name=field_3 id=field_3>
    		      <option value=''></option>
    		      <option value="SBS">SBS</option>
    		      <option value="JTU">JTU</option>
    		      		      	        </select>
    		  </label>
    Thanks for the help

Thread Information

Users Browsing this Thread

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

Posting Permissions

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



Recent Articles