Hello,
I've been trying to figure out how to combine a form submission and displaying results, and I've come up with the following way:
When you submit the form, it will set a session called SummonerSearch and give it a value of what the user has entered. I do some preliminary validation to prevent cross site scripting. Then the page is redirected back to itself. In the template I call a method that loops through results and prints them out.
My problem is that if I enter a user that exists and search for it, the form will return me the user. Then if I hit refresh page, the results portion will still print and no users will be shown. It is almost as if the session still exists but it is empty.
You can test this at vvv.azularis.com/adl-look-up
Any help is most appreciated!
oh and, I do not want to use URL parameters to search for summoners.
<?php
class ADLLookUp extends Page {
}
class ADLLookUp_Controller extends Page_Controller {
private static $allowed_actions = array('ADLResultForm');
public function ADLResultForm() {
$fields = new FieldList (
new TextField('Summoner')
);
$actions = new FieldList(
new FormAction('setPlayer', 'Search')
);
$form = new Form($this, 'ADLResultForm', $fields, $actions, $requiredFields);
$requiredFields = new RequiredFields(array('Summoner'));
$form->Fields()->dataFieldByName('Summoner')->addExtraClass('Summoner');
return $form;
}
function setPlayer($data, $form) {
if($this->checkBadCharacters($data)){
if(trim($data['Summoner']) == true){
Session::set("SummonerSearch", $data['Summoner']);
}
}
return $this->redirectBack();
}
function seekPlayer(){
if($Name = Session::get("SummonerSearch")){
$player = ADLPlayer::get()->filter('SummonerTag', $Name)->Limit(1);
Session::destroy("SummonerSearch");
return $player;
}
else{
return NULL;
}
}
function seekTopPlayers(){
return ADLPlayer::get()->sort('Rating', 'DESC')->Limit(20);
}
public function checkBadCharacters($data){
foreach($data as $inputValue){
if(strpos($inputValue, '&') || strpos($inputValue, '<') || strpos($inputValue, '>') || strpos($inputValue, '/') || strpos($inputValue, '\'') || strpos($inputValue, '\"')){
return FALSE;
}
}
return TRUE;
}
}
Template:
<% require themedCSS('ADLLookUp') %>
<div id="ADLLookUp">
<div id="ADLLookUp-Form">$ADLResultForm</div>
<% loop seekPlayer() %>
<h2>Search Results:</h2>
<table>
<tr>
<th>Summoner</th>
<th>Rating</th>
<th>MVP Count</th>
<th>Games Won</th>
<th>Games Lost</th>
<th>Last Played</th>
</tr>
<tr>
<td>$SummonerTag</td>
<td>$Rating</td>
<td>$MVPCount</td>
<td>$GamesWon</td>
<td>$GamesLost</td>
<td>$LastPlayed</td>
</tr>
</table>
<% end_loop %>
<h2>Top Players:</h2>
<table>
<tr>
<th>Summoner</th>
<th>Rating</th>
<th>MVP Count</th>
<th>Games Won</th>
<th>Games Lost</th>
<th>Last Played</th>
</tr>
<% loop seekTopPlayers() %>
<tr>
<td>$SummonerTag</td>
<td>$Rating</td>
<td>$MVPCount</td>
<td>$GamesWon</td>
<td>$GamesLost</td>
<td>$LastPlayed</td>
</tr>
<% end_loop %>
</table>
</div>