简体   繁体   English

需要在PHP中通过下拉菜单选择动态更改日期变量

[英]Need to change date variable with drop down menu selection dynamically in PHP

I have a php file (index.php) which has a titlebar, the titlebar holds date as (MM YYYY). 我有一个带标题栏的php文件(index.php),标题栏保存的日期为(MM YYYY)。 The dates are pulled from another php file titled latest_update.php. 日期从另一个名为latest_update.php的php文件中提取。 The dates are listed in the latest_update file using the following syntax 使用以下语法在latest_update文件中列出了日期

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

The $latest_run date is stripped from the file and passed to a variable using the following. $ latest_run日期从文件中剥离,并使用以下命令传递给变量。

include_once "scripts/latest_update.php";
$mainTitle = "Content Title";
$secondaryTitle = $latest_run;
$thirdTitle = $latest_run2;

The title and date are held in a div using span 标题和日期使用跨度保存在div中

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

I have a selection menu and when I change the selection menu I want the option of the secondaryTitle to change. 我有一个选择菜单,当我更改选择菜单时,我要更改secondaryTitle的选项。 So if I select Run2 as the option I want the title to change to reflect May 2017 which corresponds to $thirdTitle. 因此,如果我选择Run2作为选项,我希望标题更改以反映与$ thirdTitle相对应的2017年5月。 If I select Run1 I want it to change to the Secondarytitle referring to April 2017. The select menu is held within a form. 如果选择Run1,请参考2017年4月将其更改为Secondarytitle。选择菜单保留在表单中。

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

I am new to php and have not been able to find a solution to complete this action. 我是php的新手,无法找到解决方案来完成此操作。 Any help would be appreciated. 任何帮助,将不胜感激。

Try something to this effect: (requires JQuery and Ajax) 尝试以下操作以达到此效果:(需要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 - gets data from ajax PHP Side-从ajax获取数据

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

This could be completed entirely without a PHP file though if you don't need database related things, you could store the values for run dates into an array, then just change the title based on the selected option without using ajax and just using appends. 这可以完全不用PHP文件来完成,但是如果不需要数据库相关的东西,可以将运行日期的值存储到数组中,然后仅根据所选选项更改标题,而无需使用ajax和附加。

To change the content of the "secondaryTitle" you have to use javascript below: 要更改“ 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;
        }
    }

and change the select tag to: 并将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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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