How do I write a input statement like this one. Purchase date:MM/DD/YYYY <input type="text" id="purchasedate" name="invoice_date" />

I am using this function as a calender drop down menu. I am not sure how to write the php <inputtype ="text" ...

This is the php function to generate the drop down . How do i write the <inputtype="text" statement ?

Sorry I am new to PHP and I can not figure out how to do this ?

<?PHP 

    FUNCTION DateSelector($inName, $useDate=0) 
    { 
        /* create array so we can name months */ 
        $monthName = ARRAY(1=> "January", "February", "March", 
            "April", "May", "June", "July", "August", 
            "September", "October", "November", "December"); 

        /* if date invalid or not supplied, use current time */ 
        IF($useDate == 0) 
        { 
            $useDate = TIME(); 
        } 

        /* make month selector */ 
        ECHO "<SELECT NAME=" . $inName . "Month>\n"; 
        FOR($currentMonth = 1; $currentMonth <= 12; $currentMonth++) 
        { 
            ECHO "<OPTION VALUE=\""; 
            ECHO INTVAL($currentMonth); 
            ECHO "\""; 
            IF(INTVAL(DATE( "m", $useDate))==$currentMonth) 
            { 
                ECHO " SELECTED"; 
            } 
            ECHO ">" . $monthName[$currentMonth] . "\n"; 
        } 
        ECHO "</SELECT>"; 

        /* make day selector */ 
        ECHO "<SELECT NAME=" . $inName . "Day>\n"; 
        FOR($currentDay=1; $currentDay <= 31; $currentDay++) 
        { 
            ECHO "<OPTION VALUE=\"$currentDay\""; 
            IF(INTVAL(DATE( "d", $useDate))==$currentDay) 
            { 
                ECHO " SELECTED"; 
            } 
            ECHO ">$currentDay\n"; 
        } 
        ECHO "</SELECT>"; 

        /* make year selector */ 
        ECHO "<SELECT NAME=" . $inName . "Year>\n"; 
        $startYear = DATE( "Y", $useDate); 
        FOR($currentYear = $startYear - 5; $currentYear <= $startYear+5;$currentYear++) 
        { 
            ECHO "<OPTION VALUE=\"$currentYear\""; 
            IF(DATE( "Y", $useDate)==$currentYear) 
            { 
                ECHO " SELECTED"; 
            } 
            ECHO ">$currentYear\n"; 
        } 
        ECHO "</SELECT>"; 

    } 
?> 

<HTML> 
<BODY> 
<FORM> 
Choose a Date: <?PHP DateSelector( "Sample"); ?> 
</FORM> 
</BODY> 
</HTML>

The end of my code .

Thank you in advance . Sorry I am new to PHP

Answers


There is no need to add a text box for the date. You can just set them by concatenating the posted values

$selected_date = $_POST['SampleYear'] . $_POST['SampleMonth'] . $_POST['SampleDay']; 

--

If you feel it necessary to have a <input type="text" /> with the date, you can do this using jQuery

// in the head of your document
  //this checks for when elements of date are changed
$(function() {
$('#SampleMonth').change(function(){ setSelectedDate();});
$('#SampleDay').change(function(){ setSelectedDate();});
$('#SampleYear').change(function(){ setSelectedDate();});
}
  // this changes the text box to the values of the selects
Function setSelectedDate(){
var sel_month = $('#SampleMonth').val();
var sel_day = $('#SampleDay').val();
var sel_year = $('#SampleYear').val();
var new_sel_date = sel_year + "-" + sel_month + "-" + sel_day;
  // note that this puts the date as YYYY-MM-DD, which is the proper way to insert into mysql. It can be set as MM/DD/YYYY, but then you would have to change it again after posting.
$('#purchasedate').val(new_sel_date);
}

// inside the form tags
<input type="text" id="purchasedate" name="invoice_date" value="" readonly="true" />

Then you can input it in via the posted value

$selected_date = $_POST['purchasedate'];

assuming post the date selected (in mysql format) will be:

$mysql_date=$_POST['SampleYear'].'-'.$_POST['SampleMonth'].'-'.$_POST['SampleDay'];

'Sample' being the value of $inName in the example


Need Your Help

Rewrite rule problems in .htaccess

apache .htaccess mod-rewrite url-rewriting

I'm really having a headache with these Rewriterules

How prevalent is branch prediction on current CPUs?

arm cpu-architecture branch-prediction

Due to the huge impact on performance, I never wonder if my current day desktop CPU has branch prediction. Of course it does. But how about the various ARM offerings? Does iPhone or android phones ...