需要在PHP中动态更改日期变量和下拉菜单选项

时间:2017-06-07 14:57:31

标签: javascript php jquery drop-down-menu

我有一个带有标题栏的php文件(index.php),标题栏的日期为(MM YYYY)。日期是从另一个标题为latest_update.php的php文件中提取的。使用以下语法

在latest_update文件中列出日期
char

$ latest_run日期将从文件中删除,并使用以下内容传递给变量。

$latest_run="April 2017";
$latest_run2="May 2017";

标题和日期使用范围

保存在div中
include_once "scripts/latest_update.php";
$mainTitle = "Content Title";
$secondaryTitle = $latest_run;
$thirdTitle = $latest_run2;

我有一个选择菜单,当我更改选择菜单时,我想要更改secondaryTitle的选项。因此,如果我选择Run2作为选项,我希望标题更改为反映2017年5月,对应于$ thirdTitle。如果我选择Run1,我希望它更改为参考2017年4月的Secondaryititle。选择菜单保存在表单中。

   <div class="titleBar">
   <span class="mainTitle"><?php echo $mainTitle ?></span></br>            
   <span Class="secondaryTitle"><?php echo $secondaryTitle ?></span>
   </div>

我是php的新手,并且无法找到完成此操作的解决方案。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

尝试这种效果:(需要JQuery和Ajax)

JS -gets data from your form selection, sends it to your php file, then appends the response to the title 


    $(document).ready(function()
    var runChoice = $("#selectRun").val();
    $.ajax({
              url: 'latest_update.php',
              type: 'POST',
              data: {run:runChoice},
              success: function(response){
                  $(".secondaryTitle").append(response);
                }
    )};
)};

PHP Side - 从ajax获取数据

$choice = $_POST['runChoice'];
if($choice == "Run1"){
    echo $latest_run;
}else if($choice == "Run2"){
    echo $latest_run2;
}else{
    echo $latest_run3;
}

这可以完全没有PHP文件完成,但如果你不需要数据库相关的东西,你可以将运行日期的值存储到数组中,然后只需根据所选选项更改标题而不使用ajax而只是使用追加。

答案 1 :(得分:0)

要更改“secondaryTitle”的内容,您必须使用以下javascript:

function setTitle(select) {
        var value = select.options[select.selectedIndex].value;

        if (value == "Run2") {
            var secondTitle = document.getElementsByClassName('secondaryTitle')[0].innerHTML;
            document.getElementsByClassName('mainTitle')[0].innerHTML = secondTitle;
        }
    }

并将select标记更改为:

<select style="visibility:visible;position:relative;left:0px;top:1px;font-size:12px" name="selmenu" id="selmenu" onchange="setTitle(this);">
    <option value="Run1">Run1</option>
    <option value="Run2">Run2</option>
    <option value="Run3">Run3</option>
</select>