1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| <?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
function get_value($index, $default = "") {
return (isset($_GET[$index])) ? filter_input(INPUT_GET, $index) : $default;
}
function get_value_array($index, $default = "") {
return (isset($_GET[$index])) ? filter_input(INPUT_GET, $index, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY) : $default;
}
$types = [
'patrimoine' => "Patrimoine",
'historique' => "Historiques",
'numerique' => "Numériques",
];
$sites = [
'mediatheque-victor-hugo' => 'Médiathèque Victor Hugo',
'sorbonne' => "La Sorbonne",
'puy-du-fou' => "Puy dy Fou",
];
?><form method="get" action="">
<p>
<label for="type_doc">Type de document</label>
<select name="type_doc" id="type_doc">
<option value=""></option>
<?php foreach ($types AS $k => $v) : ?>
<?php $selected = (get_value('type_doc') == $k || (empty(get_value('type_doc')) && $k == 'patrimoine')) ? ' selected="selected"' : ''; ?>
<option value="<?= $k; ?>"<?= $selected; ?>><?= $v; ?></option>
<?php endforeach; ?>
</select>
</p>
<p>
<label for="sites">Un select multiple pour l'exemple</label>
<select name="sites[]" id="sites" multiple>
<?php foreach ($sites AS $k => $v) : ?>
<?php $selected = (is_array(get_value_array('sites')) && in_array($k, get_value_array('sites')) || (empty(get_value('sites')) && $k == 'mediatheque-victor-hugo')) ? ' selected="selected"' : ''; ?>
<option value="<?= $k; ?>"<?= $selected; ?>><?= $v; ?></option>
<?php endforeach; ?>
</select>
</p>
<p>
<label for="search">Texte à trouver :</label>
<input type="text" name="search" id="search" value="<?= get_value('search'); ?>" required="required" />
</p>
<button type="submit">Chercher</button>
</form>
<?php
if (!empty($_GET)) {
echo '<hr />';
echo PHP_EOL . '<pre>type_doc: ' . print_r(get_value('type_doc'), true) . '</pre>' . PHP_EOL;
echo PHP_EOL . '<pre>sites: ' . print_r(get_value_array('sites'), true) . '</pre>' . PHP_EOL;
echo PHP_EOL . '<pre>search: ' . print_r(get_value('search'), true) . '</pre>' . PHP_EOL;
echo '<hr />';
echo PHP_EOL . '<pre>$_GET raw data: ' . print_r($_GET, true) . '</pre>' . PHP_EOL;
} |
Partager