Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to get the selected value in the drop down; but the items inside it are in an array. How can I get the value of the selected option?

For now, I have these lines of code:

<?php
// lowest year wanted
$cutoff = 2013;

// current year
$now = date('Y');

// build months menu
echo '<select name="month">' . PHP_EOL;
for ($m=1; $m<=12; $m++) {
    echo '  <option value="' . $m . '">' . date('M', mktime(0,0,0,$m)) . '</option>' . PHP_EOL;
}
echo '</select>' . PHP_EOL;

// build days menu
echo '<select name="day">' . PHP_EOL;
for ($d=1; $d<=31; $d++) {
    echo '  <option value="' . $d . '">' . $d . '</option>' . PHP_EOL;
}
echo '</select>' . PHP_EOL;

// build years menu
echo '<select name="year">' . PHP_EOL;
for ($y=$now; $y>=$cutoff; $y--) {
    echo '  <option value="' . $y . '">' . $y . '</option>' . PHP_EOL;
}
echo '</select>' . PHP_EOL;
?>

Let's say, I picked July for the month. How can I tell the browser that I have chosen July. Any idea?

share|improve this question
you want to get value of selected one using javascirpt – webGautam May 28 at 7:09
add comment (requires an account with 50 reputation)

2 Answers

up vote 1 down vote accepted

Try like this

echo '<select name="month">' . PHP_EOL;
for ($m=1; $m<=12; $m++) {
    if($sel_mnth == $m)
        echo '  <option value="' . $m . '" selected="selected">' . date('M', mktime(0,0,0,$m)) . '</option>' . PHP_EOL;
    else   
        echo '  <option value="' . $m . '">' . date('M', mktime(0,0,0,$m)) . '</option>' . PHP_EOL;
}
echo '</select>' . PHP_EOL;

or directly

for ($m=1; $m<=12; $m++) {                   
        echo '  <option value="' . $m . '" if($sel_mnth == $m) echo "selected=\'selected\'";>' . date('M', mktime(0,0,0,$m)) . '</option>' . PHP_EOL;
}
share|improve this answer
Hi! Thank you for a quick response. Just to clarify, I want to display the value of the Month that was selected. Is that how your code works? For example, I set month to December, how can I tell the code that the selected value is December? Does that make any sense? I'm sorry for this. – ninpot18 May 28 at 7:23
It will check with the month count,for example For december it will be 12 – Gautam3164 May 28 at 7:24
I see. Well then, I'll give it a go and see how it works. If not I'll go back to you, if yes I'll give you a check mark. Thanks! :) – ninpot18 May 28 at 7:28
It's working now! Thanks again! :) – ninpot18 May 28 at 7:38
add comment (requires an account with 50 reputation)

Put your select within a form tag and after submit the form use $_POST['month'] or $_POST['year'] for getting month and year user selected value.

like this

if (isset($_POST['submit'])){

$year=$_POST['year']; //user selected year value is here
...
}

<form method="POST" action="">
... //here is your select tag codes
...
<input type="submit" name="submit" />
</form>
share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.