简体   繁体   中英

PHP Display label and selected value

:::EDIT:::

Ok so apparently I should have put in the larger picture here, and that is my fault. Ok so let's say there are 2 sets of dropdowns,

Consider the following HTML:

<form name="myform" action="process.php" method="POST">
<input type="hidden" name="check_submit" value="1" />
<select name="month">
<option selected="--">--</option>
<option value="January">01</option>
<option value="February">02</option>
<option value="March">03</option>
<option value="April">04</option>
<option value="May">05</option>
<option value="June">06</option>
<option value="July">07</option>
<option value="August">08</option>
<option value="September">09</option>
<option value="October">10</option>
<option value="November">11</option>
<option value="December">12</option>
</select>
<select name="year">
<option selected="--">--</option>
<option value="1965">65</option>
<option value="1966">66</option>
<option value="1967">67</option>
<option value="1968">68</option>
</select>
<input type="submit" />
</form>

I am using $_POST in 'process.php' to display the year selected by the user like this:

<?php
 echo "{$_POST['month']} ";
 echo "{$_POST['year']} ";
 ?>

If "67" is selected "1967" displays AND when "09" is selected, "September" is displayed so the question still stands...

Is there a way that I can display the option and the label on the same page? This is simple HTML with a PHP processor, nothing more.

Thanks!

you can't achieve it by php alone you can use jquery with this like follow

   <select name="month" onchange="document.getElementById('month_text').value=this.options[this.selectedIndex].text">
     <option selected="--">--</option>
     <option value="January">01</option>
     <option value="February">02</option>
     <option value="March">03</option>
      ............
     <option value="December">12</option>
   </select>

   <select name="year" onchange="document.getElementById('year_text').value=this.options[this.selectedIndex].text">
      <option selected="--">--</option>
      <option value="1965">65</option>
      <option value="1966">66</option>
      <option value="1967">67</option>
      <option value="1968">68</option>
  </select>

    <input type="hidden" name="year_text" id="year_text" value="" />
    <input type="hidden" name="month_text" id="month_text" value=""/>

in php

  echo $_POST['year']; // will print 1967
  echo $_POST['year_text']; // will print 67

  echo $_POST['month']; // will print January
  echo $_POST['month_text']; // will print 01
<?php
 $monthArray = array('01'=>'January','02'=>'February','03'=>'March','04'=>'April',
                     '05'=>'May','06'=>'June','07'=>'July','08'=>'August',
                     '09'=>'September','10'=>'October','11'=>'November',
                     '12'=>'December');
 $month = $_POST['month'];
 $year = $_POST['year'];
 echo $month;
 echo $year;
 echo "You have selected ".substr($year, 2)." Year.";
 echo "You have selected ".$monthArray[$month]." month.";
?>

I have updated the code for month also.

You can do like this: Modify html like:

<option value="1965-65">65</option>

In PHP:

$explode = explode("-",$_POST['year']);
echo $explode[0]; //will be 1965
echo $explode[1]; //will be 65

OR your html

<option value="1965">65</option>

PHP

echo substr($_POST['year'], 2, 2);  // 65

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM