duandong2562
2016-05-07 15:52
浏览 583
已采纳

$ _SESSION上的未定义变量

I initialize my $_SESSION var but I have these errors when I launch the index without submit the form:

if(!empty($_POST)){
    $_SESSION['idZone'] = 0;
    $_SESSION['idSalle'] = 0;
    $_SESSION['idUtilisateur'] = 0;
    $_SESSION['dateDebut'] = 0 ;
    $_SESSION['dateFin'] = 0;
}
?>

Screenshot of Errors

This is my full code :

<?php 
include('fonctionsBDD.php');
$bdd = connectionBDD(); 

session_start();

if(!empty($_POST)){
    $_SESSION['idZone'] = 0;
    $_SESSION['idSalle'] = 0;
    $_SESSION['idUtilisateur'] = 0;
    $_SESSION['dateDebut'] = 0 ;
    $_SESSION['dateFin'] = 0;
}
?>

<!-- Choix des filtres à afficher -->
<p>Quels filtres afficher ?</p>
<form action="" method="POST">
    <input type="checkbox" name="cbx-zones">Zones</input> 
    <input type="checkbox" mname="cbx-salles">Salles</input>
    <input type="checkbox" name="cbx-dates">Date</input> 
    <input type="checkbox" name="cbx-heures">Heures</input> 
    <p><input type="submit" /></p>
</form>

<!-- Zone filtres -->
<div class="FiltreSalle">
    <form action="" method="POST">
    <?php 
    if (isset($_POST['cbx-zones'])) {
        /* COntenu d'un filtre ici */
    }
    ?>
<!-- Filtre par Area Name-->
    <form action="" method="POST">
        <br /><br />
        <label>Choisir la zone </label><br />
        <select name="zone">
            <?php echo "<option unselected>- - - Choisissez  une zone - - - </option>
";
    $reponseSalle = $bdd->query('SELECT * FROM mrbs_area ORDER BY area_name');

        while ($donnees = $reponseSalle->fetch(PDO::FETCH_OBJ)) {
        ?>
            <option value="<?php echo $donnees->id; ?>"> <?php echo $donnees->area_name; ?></option>
        <?php
        }
        ?>
        </select>

        <!-- Filtre par salle -->
        <br /><br />

        <label>Choisir la salle</label><br />
        <select name="salle"> 
            <?php echo "<option unselected>- - - Choisissez  une salle - - - </option>
";
        $reponseSalle = $bdd->query('SELECT * FROM mrbs_room ORDER BY room_name');

        while ($donnees = $reponseSalle->fetch(PDO::FETCH_OBJ)) {
        ?>
            <option value="<?php echo $donnees->id; ?>"> <?php echo $donnees->room_name; ?></option>
        <?php
        } 
        ?>
        </select>

        <!-- Filtre par utilisateurs-->
        <br /><br />
        <label>Choisir l'utilisateur</label><br />
        <select name="utilisateur">
            <?php echo "<option unselected>- - - Choisissez  un utilisateur - - - </option>
";
        $reponseSalle = $bdd->query('SELECT * FROM mrbs_users ORDER BY name');

        while ($donnees = $reponseSalle->fetch(PDO::FETCH_OBJ)) {
        ?>
            <option value="<?php echo $donnees->id; ?>"> <?php echo $donnees->name; ?></option>
        <?php
        }
        ?>
        </select>

        <!-- Filtres par DatePicker -->
        <br /> <br />
<script>
$(function() {
    $("#from").datepicker({
        defaultDate: "+1d",
        changeMonth: true,
        numberOfMonths: 1,
        dateFormat : '@',
        onClose: function( selectedDate ) {
            $( "#to" ).datepicker( "option", "minDate", selectedDate );
        }
    });
    $("#to").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1,
        dateFormat : '@',
        onClose: function( selectedDate ) {
            $( "#from" ).datepicker( "option", "maxDate", selectedDate );
            var currentDate = $( ".selector" ).datepicker( "getDate" );
        }
    });
});
</script>
        <label for="from">Du</label>
        <input type="text" id="from" name="from">
        <label for="to">Au</label>
        <input type="text" id="to" name="to">
        <input type='submit' value='Submit'><br /><br />
    </form>
    <!-- Traitement des données -->
<?php
$_SESSION['idZone'] = $_POST['zone'];
$_SESSION['idSalle'] = $_POST['salle'];
$_SESSION['idUtilisateur'] = $_POST['utilisateur'];
$_SESSION['dateDebut'] = $_POST["from"];
$_SESSION['dateFin'] = $_POST["to"];

echo('La zone séléctionnée est :' . $_SESSION['idZone']  . '<br />');
echo('La salle séléctionnée est :' . $_SESSION['idSalle']  . '<br />');
echo('L utilisateur séléctionné est :' . $_SESSION['idUtilisateur']  . '<br />');
echo('La date de début séléctionnée est :' . $_SESSION['dateDebut']  . '<br />');
echo('La date de fin séléctionnée est :' . $_SESSION['dateFin'] . '<br />');      
?>

I think my initialization ins't good but I do not know why. If anyone can help me :)

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dqk77945 2016-05-07 16:52
    已采纳

    One best way of using sessions is The session should always start first in the file before any other code. like this

    <?php
    session_start() ;
    
    include('fonctionsBDD.php');
    $bdd = connectionBDD(); 
    .....
    
    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题