sammy
sammy

Reputation: 717

use of dropdown list PHP

i have two questions. this is my code (Iranian calender), the problem is when i want see results. all values of year and month and day showns as '$year' and 'month' and 'day'.

enter image description here

 <?php

            echo'<select dir="rtl" name="year">';
            for($year=1388;$year<=1410;$year++)
            { 
            echo '<option value="$year">$year</option>';


            } 
echo'</select>';


echo'<select dir="rtl" name="month">';
for($month=1;$month<=12;$month++)
            {


            echo '<option value="$month">$month</option>';

            }
echo'</select>';


echo'<select dir="rtl" name="day">';
for($day=1;$day<=31;$day++)
            {


            echo '<option value="$day">$day</option>';

            }
echo'</select>';
            ?>

second question:

and i need another thing. in Iranian calendar month 1 to 6 have 31 days and 7 to 12 have 30 days so i need a conditional expression when user choose month 1 to 6 show 31 days to choose and when user chooses month 7 to 121 show 30 days.

Upvotes: 0

Views: 72

Answers (2)

Kostas Mitsarakis
Kostas Mitsarakis

Reputation: 4747

You use double quotes inside of single quotes so it will be rendered as string. Make the changes accordingly:

From:

echo '<option value="$year">$year</option>';

To:

echo '<option value="'.$year.'">'.$year.'</option>';

echo '<option value="'.$month.'">'.$month.'</option>';

echo '<option value="'.$day.'">'.$day.'</option>';

EDIT (answer to your second question):

JSFIDDLE

First you need to give an id attribute to your select fields.

<select dir="rtl" name="month" id="month">

<select dir="rtl" name="day" id="day">

Then you need to add some JQuery code that checks if month is greater than 6 so it will remove the last option (only if days are 31). If month is equal of 6 or less then will add a new option (only if days are 30).

$('#month').on('change', function() {
    var monthValue = $(this).val();
    var dayOptions = $('#day option').size();
    if (monthValue > 6 && monthValue <=12) {
        if (dayOptions == 31) {
            $("#day option:last").remove();
        }
    } else if (monthValue >= 1 && monthValue <= 6) {
        if (dayOptions == 30) {
            $('#day').append($('<option>', {
                    value: 31,
                    text: 31
                }));
        }
    }
});

Upvotes: 3

jmattheis
jmattheis

Reputation: 11125

you need to use " or use concatenation with . with ' so this should be:

echo '<option value="$day">$day</option>';

this:

echo '<option value="' . $day . '">' . $day . '</option>';

or this:

echo "<option value=\"$day\">$day</option>";

Upvotes: 2

Related Questions