#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Posts
    52
    Rep Power
    13

    problem while controlling PHP array created data with JavaScript


    date select lists are created using PHP like this
    //DATE ARRAYS
    $day=array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31);
    $mo=array(0,Jan,Feb,Mar,Apr,Mai,Jun,Jul,Aug,Sep,Oct,Nov,Dec);
    $thisyear=date("Y");
    for($i=($thisyear-2);$i<($thisyear+5);$i++)
    {
    $year[]=$i;
    }
    ...
    From <select size="1" name="valid_fromday">
    <?
    for($i=1;$i<(count($day));$i++)
    {
    echo("<option value=\"$i\">$day[$i]</option>");
    }
    ?>
    </select> .... month year ext.

    I have a javascript control function like this
    function control()
    {
    if(document.getElementById('valid_fromday').value==document.getElementById('valid_today').value && document.getElementById('valid_frommo').value==document.getElementById('valid_tomo').value && document.getElementById('valid_fromyear').value==document.getElementById('valid_toyear').value)
    {
    alert('ERROR! beginning and ending dates of a period must be different');
    return false;
    }
    else if(document.getElementById('valid_fromday').value>= document.getElementById('valid_today').value && document.getElementById('valid_frommo').value>= document.getElementById('valid_tomo').value && document.getElementById('valid_fromyear').value>= document.getElementById('valid_toyear').value)
    {
    alert('ERROR! beginning date of the period can not be later then ending date');
    return false;
    } ...



    When the days or months between 1-9 selected as from date and days or months greater 10 as to date, function alerts first date is later then second.
    And when dates in the same month selected then it alerts dates must be different.
    Why? and how can I solve the problem?

    Note1: This PHP script is able to save the date data in db correct and problem free
    Note2: When I create the date select lists with HTML codes, the control function runs as expected.
  2. #2
  3. film at 11
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Portland, OR
    Posts
    413
    Rep Power
    15
    teknoart,

    to get the value of the selected option from the select box, dont you have to go further than getting the .value attribute of the select box itself. the select box's value is not the same as the value of the selected option, because each option is also an object. they're stored in the .options array, and you can get the selected one by the .selectedIndex attribute...

    mySelect.options[mySelect.selectedIndex].value

    also I think you can shorten that to:

    mySelect[mySelect.selectedIndex].value

    but don't quote me on that one. anyway the point is that you might have to rewrite the conditionals to something like this:

    var fromDay = document.getElementById('valid_fromday');
    var toDay = document.getElementById('valid_today');

    if (fromDay.options[fromDay.selectedIndex].value == toDay.options[toDay.selectedIndex].value && etc etc)

    hth

    /nick
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Posts
    52
    Rep Power
    13
    thank you merkinmuffley it's well done

IMN logo majestic logo threadwatch logo seochat tools logo