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

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

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证