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

$ _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条)

报告相同问题?

悬赏问题

  • ¥33 集成BiLSTM模型和GCN模型时,前模型的输出不匹配后模型的输入
  • ¥15 关于液体厚度提取,和铺展轨迹数据化处理
  • ¥20 对方微信是wxid如何添加成好友?或者转换成微信号?
  • ¥15 yolov5实验数据好,但是自己拍摄的照片检测结果差
  • ¥15 做过dali2.0认证的技术,
  • ¥15 一道python难题
  • ¥15 matlab 免疫算法
  • ¥50 可视化界面构建,实现信息模块功能
  • ¥15 基于bert-base-chinese的中文文本处理
  • ¥15 高价求抖音抓包技术,抖音抓包无网络问题