簡體   English   中英

需要在PHP中通過下拉菜單選擇動態更改日期變量

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

我有一個帶標題欄的php文件(index.php),標題欄保存的日期為(MM YYYY)。 日期從另一個名為latest_update.php的php文件中提取。 使用以下語法在latest_update文件中列出了日期

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

$ latest_run日期從文件中剝離,並使用以下命令傳遞給變量。

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

標題和日期使用跨度保存在div中

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

我有一個選擇菜單,當我更改選擇菜單時,我要更改secondaryTitle的選項。 因此,如果我選擇Run2作為選項,我希望標題更改以反映與$ thirdTitle相對應的2017年5月。 如果選擇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>

我是php的新手,無法找到解決方案來完成此操作。 任何幫助,將不勝感激。

嘗試以下操作以達到此效果:(需要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和附加。

要更改“ 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM