即使在表单已经提交到我的PHP后,仍然保留在下拉列表中选择的值

时间:2017-09-22 04:56:21

标签: php arrays

<?php

?>

/* Array to store days in a week*/
 $NoofDays=array("Monday","Tuesday","Wednesday","Thurseday","Friday","Saturday","Sunday");

?>
<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:)";
    }
        }
?>

感谢任何人回答这个问题。格式化代码将使更容易看到错误。这是PHP代码(PHP是一种服务器端脚本语言,专为Web开发而设计,但也用作通用编程语言)

2 个答案:

答案 0 :(得分:1)

InMemoryUploadedFile

答案 1 :(得分: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是一个丑陋的。