methuselah
methuselah

Reputation: 13206

Setting up a live refresh on select drop down

I have currently set up a select drop down menu that does three things:

But now I have a problem! Let me use this example to explain my difficulty

The problem now arises when the user tries to select a date from a different month. The date limitation still exists! So if I switch to October I can only select dates from 23 October to 31 October. Which is wrong cos I want to select dates from 1 October to 31 October.

How can I refresh the drop down in real-time to update the date entry when the user switches to another month?

Here is the code I'm using right now:

<label for="date"><?php echo __('Pickup Date') ?> <span>*</span></label>
<?php
$curr_day = date('j', strtotime('+ 48 hours'));
$day = range (1, 31);
$day = array_slice($day, $curr_day-1);
$select = "<select name=\"day\">\n";
foreach ($day as $key => $val) {
    $select .= "\t<option val=\"".$key."\"";
    if ($key == $curr_day) {
        $select .= " selected=\"selected\">".$val."</option>\n";
    } else {
        $select .= ">".$val."</option>\n";
    }
}
$select .= "</select>";
echo $select;
?>
    &nbsp;:&nbsp;
<?php 
$curr_month = date("m");
$month = array (1=>"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
$month = array_slice($month, $curr_month-1);
$select = "<select name=\"month\">\n";
foreach ($month as $key => $val) {
    $select .= "\t<option val=\"".$key."\"";
    if ($key == $curr_month) {
        $select .= " selected=\"selected\">".$val."</option>\n";
    } else {
        $select .= ">".$val."</option>\n";
    }
}
$select .= "</select>";
echo $select;
?>

Many thanks in advance!

Upvotes: 1

Views: 846

Answers (1)

Korvin Szanto
Korvin Szanto

Reputation: 4501

You're going to use JavaScript to do it effectively, jQuery if you're nimble.

$('select').change(function(){
    updatetheselect;
});

To do it, make a PHP page that outputs the values in a json array using json_encode():

{"option1":"value"}

Then use AJAX to retrieve the info and parse it:

$.getJSON('jsonstuff.php',{month:'august'},function(json) {
    json.each(function(key,value){
        $('select').append('<option value="'+key+'">'+value+'</option>');
    });
});

This function is untested.

http://jsfiddle.net/tBrXt/2/

Upvotes: 2

Related Questions