2017-09-22 91 views
0
<?php 

?> /*數組存儲天在一個星期*/ $ NoofDays =陣列(「星期一」,「星期二」,」週三」, 「Thurseday」, 「星期五」, 「星期六」, 「星期天」);保值已提交IIN PHP

?> 
<div class="form-wrapper"> 
<form action="" method="post"> 
    <select name="days"> 
     <?php foreach($NoofDays as $days){?> 

     <option <?php if($daysTitle== '$days')?> selected="selected"> <?php echo $days;?></option> 

     <?php } ?> 
    </select> 
    <button type='submit' class="btn">Submit me</button> 
</form>`enter code here` 
    </div> 
<?php 
/* Script to generate the Switch case */ 
    if (isset($_POST['days'])) { 
     $selected = $_POST['days']; 


    switch ($selected) { 
     case "Monday": 
      echo "Laugh on Monday, Laugh for danger"; 
      break; 
     case "Tuesday": 
      echo "Laugh on Tuesday, Laugh for stranger"; 
      break; 
     case "Wednesday": 
      echo "Laugh on Wednesday,Laugh for Letter"; 
      break; 
      case "Thurseday": 
      echo " Laugh on Thurseday,Something better"; 
      break; 
      case "Friday": 
      echo "Laugh on Friday,Laugh for sorrow"; 
      break; 
      case "Saturday": 
      echo "Laugh on Saturday,Joy Tommorow!"; 
      break; 

     default: 
      echo "Laugh on Every day for some reason:)"; 
    } 
     } 
?> 

回答

1
<option <?php if (isset($_POST['days']) && $_POST['days'] === $days): ?>selected="selected"<?php endif; ?>><?php echo $days; ?></option> 
+0

它像一個魅力工作謝謝camel_case –

1
<select name="days"> 
<?php 
foreach($NoofDays as $day) 
{ 
    $selected = (!empty($_POST['days']) && $_POST['days'] == $day) ? ' selected="selected"' : ''; 
    echo sprintf('<option value="%s"%s>%s</option>', $day, $selected, $day); 
} 
?> 
</select> 

甚至更​​好:使用任何第三方模板引擎 - 單獨PHP十分難看。