我有应该在提交后管理表单的这段代码,并将$_SESSION
变量设置为用户从选择框中选择的新值。问题是默认会话变量将不会更改,并且如果用户尝试搜索它,即使更改了所需联赛,它也将始终获得相同的结果。我该如何解决?注意:session_start()
存在于保存代码的索引页中,也存在于管理数据库操作的类文件中。
<?php
session_start();
require_once 'app/config.php';
require_once 'app/Autoloader.php';
$prediction = new Prediction(new DataManager($db));
$template = new TemplateLoader('app/templates/');
if(isset($_POST['search_matchday'])){
$matchday = filter_var($_POST['matchday'], FILTER_SANITIZE_NUMBER_INT);
$data = array(
'data'=> $prediction->searchMatchday($matchday, $_SESSION['competition']),
'date'=> new DateTime(),
'results'=> $prediction->randomResult(),
'logo'=> $prediction->competitionLogo($_SESSION['competition'])
);
echo $template->renderTemplate('home', $data);
}
if(isset($_POST['switch_competition'])){
$competitionName = $_POST['competition'];
$_SESSION['competition'] = $_POST['competition'];
$data = array(
'data'=> $prediction->switchCompetition($competitionName),
'date'=> new DateTime(),
'results'=> $prediction->randomResult(),
'logo'=> $prediction->competitionLogo($competitionName)
);
echo $template->renderTemplate('home', $data);
}
$data = array(
'data'=> $prediction->defaultCompetition(),
'results'=> $prediction->randomResult(),
'date'=> new DateTime(),
'logo'=> $prediction->competitionLogo('Serie A')
);
$_SESSION['competition'] = 'Serie A';
echo $template->renderTemplate('home', $data);
?>
HTML表单
<form class="form-inline" method="POST" action="">
<div class="input-group">
<input type="text" class="form-control" name="matchday" id="input-matchday" placeholder="Cerca Giornata">
<div class="input-group-append">
<button type="submit" name="search_matchday" class="btn btn-link"><i class="fas fa-search"></i></button>
</div>
</div>
</form>