Comment je peux faire en sorte que lorsque je rafraichi la page le titre du contenu puisque correspondre à la sélection du menu déroulant au moment que je rafraichi ma page avec le bouton du navigateur?

Admettons que je sélection au menu déroulant JAVASCRIPT, mon fichier getuser va afficher les sujets concernant Javascript. Mais, si je rafraichi
la page, Javascript au menu va resté mais le contenu Ajax va disparaitre puisque je n'aurai plus sélectionner le sujet au menu.

Je pourrait mettre showUser(1) pour afficher par défaut le sujet 1 mais ce n'est pas ce que je veux. Je veux afficher le sujet qui corresponds au
sujet choisi du menu. Je pensais placer en session dans showUser() mais ce que j'ai testé, ça ne fonctionne pas.

Une idée?


Fichier index.php:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Triage par catégories</title>
  <script src="http://code.jquery.com/jquery-2.1.1.js"></script>
 
    <style>
            table                          {border-collapse:collapse;width:50%}
            th                              {height:66px;background-color:darkblue;color:white;font-size:1.75em}
            td                           {width:50%;font-size:1.5em}
            tr:nth-of-type(odd) {background-color:#eee}
            form                       {width:50%;margin-left:auto;margin-right:auto}
            select,option           {width:25%}
    </style>
 
  <script>
            function showUser(str) {
                    if (str == "") {
                            document.getElementById("txtHint").innerHTML = "";
                            return;
                    } else {
                            if (window.XMLHttpRequest) {
                                    // code for IE7+, Firefox, Chrome, Opera, Safari
                                    xmlhttp = new XMLHttpRequest();
                            } else {
                                    // code for IE6, IE5
                                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                            }
                            xmlhttp.onreadystatechange = function() {
                                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                                            document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                                    }
                            };
                            xmlhttp.open("GET","getuser.php?q="+str,true);
                            xmlhttp.send();
                    }
            }
            showUser(1);
  </script>
 
</head>
<body>
 
<?php
$VALEUR_hote='localhost';
$VALEUR_port='3306';
$VALEUR_nom_bd='bdd';
$VALEUR_user='root';
$VALEUR_mot_de_passe='';
 
$connexion = new PDO('mysql:host='.$VALEUR_hote.';port='.$VALEUR_port.';dbname='.$VALEUR_nom_bd, $VALEUR_user, $VALEUR_mot_de_passe);
 
?>
<form method="post">
        <fieldset>
                <legend>Les catégories</legend>
                                        <select name="categoryid" onchange="showUser(this.value)">
                                                                    <?php
                                                                    $ResultCategory = $connexion->query('SELECT * FROM    category');
                                                                    while (list($category_id, $category_name) = $ResultCategory->fetch(PDO::FETCH_NUM)) {
                                  ?>
                                  <option value="<?=$category_id?>"><?=$category_name?></option>
                                                                    <?php
                                                                    }
                                                                    ?>
                                        </select>
        </fieldset>
</form>
 
<div id="txtHint"></div>
 
</body>
</html>
Le fichier de requête (getuser.php) :

Code php : Sélectionner tout - Visualiser dans une fenêtre à part
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
<?php
$id = isset($_GET['q']) ? intval($_GET['q']) : '';
 
$VALEUR_hote='localhost';
$VALEUR_port='3306';
$VALEUR_nom_bd='bdd';
$VALEUR_user='root';
$VALEUR_mot_de_passe='';
 
$connexion = new PDO('mysql:host='.$VALEUR_hote.';port='.$VALEUR_port.';dbname='.$VALEUR_nom_bd, $VALEUR_user, $VALEUR_mot_de_passe);
 
$ResultUsers = $connexion->prepare('
            SELECT *
            FROM    post AS p
            RIGHT JOIN category AS c ON p.category_id = c.category_id
            WHERE c.category_id = :category_id
');
 
$ResultUsers->bindParam(':category_id', $id, PDO::PARAM_INT);
$ResultUsers->execute();
 
$category = '';
 
echo '<table align="center">';
while( $ResultUserWhile = $ResultUsers->fetch(PDO::FETCH_OBJ) ) {
 
        if ( $category != $ResultUserWhile->category_id ) {
                            echo '<tr><th align="center" colspan=2>' . $ResultUserWhile->category_name . '</td></tr>';
                            $category = $ResultUserWhile->category_id;
        }
                echo '<tr><td><img src="image.jpeg" height="50" align="middle"> Titre: <strong>' . $ResultUserWhile->post_title . '</strong> par utilisateur ' . $ResultUserWhile->post_id . '</td></td></tr>';
}
echo '</table>';
 
$ResultUsers->closeCursor();
?>