Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
862 views
in Technique[技术] by (71.8m points)

php - Setting up a live refresh on select drop down

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

  • By default it picks a date 48 hours in the future and the current month
  • To prevent a date selected in the past it only shows the selected date until the end of the current month
  • Another limitation the system has is that it shows all the months until the end of the calendar year

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

  • At the time of writing a date of 23 September is available by default. No other dates are available before 23 September
  • The user has the option selecting dates from 23 September to 31 September
  • The user also has the option of selecting months from September to December

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">
";
foreach ($day as $key => $val) {
    $select .= "<option val="".$key.""";
    if ($key == $curr_day) {
        $select .= " selected="selected">".$val."</option>
";
    } else {
        $select .= ">".$val."</option>
";
    }
}
$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">
";
foreach ($month as $key => $val) {
    $select .= "<option val="".$key.""";
    if ($key == $curr_month) {
        $select .= " selected="selected">".$val."</option>
";
    } else {
        $select .= ">".$val."</option>
";
    }
}
$select .= "</select>";
echo $select;
?>

Many thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...