如果未登录则显示模式

时间:2019-03-21 23:49:08

标签: php modal-dialog

我正在尝试显示模式弹出窗口,使您可以登录,但前提是您尚未登录。

我已经尝试了许多解决方案,但到目前为止只能使登录名显示在新页面中。

请在下面查看我的代码

index.php

<?php 

require 'database_connection.php';

if (!isset($_SESSION["type"])) {
    echo "<script>$('#loginModal').modal('show');</script>";
}

require 'includes/header.php';
?> 

***
***
if session type is 
set code goes here
***
***

<div id="loginModal" class="modal fade">
            <div class="modal-dialog">
                <form method="post" id="product_form">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h4 class="modal-title"><i class="fa fa-plus"></i> Add Product</h4>
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                        </div>
                        <div class="modal-body">
                            <div class="form-group">
                                <label>Stock Code</label>
                                <input type="text" name="product_name" id="product_name" class="form-control" required disabled />
                            </div>
                            <div class="form-group">
                                <label name="category_label" id="category_label">Category</label>
                                <select name="category_id" id="category_id" class="form-control" required> 
                                    <option value="">Select Category</option>
                                    <?php echo fill_category_list($connect);?>
                                </select>
                            </div>
                            <div class="form-group">
                                <label>Enter Product Quantity</label>
                                <div class="input-group">
                                    <input type="text" name="product_quantity" id="product_quantity" class="form-control" required pattern="[+-]?([0-9]*[.])?[0-9]+" placeholder="Enter Qty" /> 
                                    <span class="input-group-addon">
                                        <select class="form-control" name="product_unit" id="product_unit" required disabled>
                                            <option value="Box">Boxes</option>
                                        </select>
                                    </span>
                                </div>
                            </div>
                            <div class="form-group">
                                <label>Location</label>
                                <input type="text" name="product_location" id="product_location" class="form-control" required disabled />
                            </div>
                        </div>
                        <div class="modal-footer">
                            <input type="hidden" name="product_id" id="product_id" />
                            <input type="hidden" name="btn_action" id="btn_action" />
                            <input type="submit" name="action" id="action" class="btn btn-success" value="Add" />
                        </div>
                    </div>
                </form>
            </div>
        </div>

database_connection.php

<?php

$connect = new PDO('mysql:host=localhost;dbname=continental3', 'root', 'password');
session_start();

?>

我想念什么?

顺便说一句,如果有更简便或更好的方法,我很乐意更改我的方法

任何帮助将不胜感激

**更新**

<?php

require 'database_connection.php'; //checks session type is set or not
require 'includes/header.php'; // Loads css and js files

if (!isset($_SESSION["type"])) { //session check 


    echo '<script>$(document).ready(function(){$("#loginModal").modal("show");});</script>'; 
} //open modal

require 'function.php'; 
require 'includes/navbar.php';

?>

上面的代码按预期显示模式,但仅在页面加载后显示。删除文档就绪功能后,模式将不会加载,但是页面的其余部分会加载并显示(大概是因为我没有使用else条件。当使用如下所示的else条件时,不会加载任何东西。

<?php

require 'database_connection.php';
require 'includes/header.php';

if (!isset($_SESSION["type"])) { ?>


    echo '<script>$(document).ready(function(){$("#loginModal").modal("show");});</script>';
 <?php } else {

require 'function.php'; 
require 'includes/navbar.php';

?>

***
***
if session type is 
set code goes here
***
***

<div id="loginModal" class="modal fade">
            <div class="modal-dialog">
                <form method="post" id="product_form">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h4 class="modal-title"><i class="fa fa-plus"></i> Add Product</h4>
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                        </div>
                        <div class="modal-body">
                            <div class="form-group">
                                <label>Stock Code</label>
                                <input type="text" name="product_name" id="product_name" class="form-control" required disabled />
                            </div>
                            <div class="form-group">
                                <label name="category_label" id="category_label">Category</label>
                                <select name="category_id" id="category_id" class="form-control" required> 
                                    <option value="">Select Category</option>
                                    <?php echo fill_category_list($connect);?>
                                </select>
                            </div>
                            <div class="form-group">
                                <label>Enter Product Quantity</label>
                                <div class="input-group">
                                    <input type="text" name="product_quantity" id="product_quantity" class="form-control" required pattern="[+-]?([0-9]*[.])?[0-9]+" placeholder="Enter Qty" /> 
                                    <span class="input-group-addon">
                                        <select class="form-control" name="product_unit" id="product_unit" required disabled>
                                            <option value="Box">Boxes</option>
                                        </select>
                                    </span>
                                </div>
                            </div>
                            <div class="form-group">
                                <label>Location</label>
                                <input type="text" name="product_location" id="product_location" class="form-control" required disabled />
                            </div>
                        </div>
                        <div class="modal-footer">
                            <input type="hidden" name="product_id" id="product_id" />
                            <input type="hidden" name="btn_action" id="btn_action" />
                            <input type="submit" name="action" id="action" class="btn btn-success" value="Add" />
                        </div>
                    </div>
                </form>
            </div>
        </div>
<?php } #close else ?>

1 个答案:

答案 0 :(得分:0)

您在哪里设置了$ _SESSION ['type'] ???

请尝试这个 index.php

<?php 

require 'database_connection.php';

if (!isset($_SESSION["type"])) { ?>

   <script> $('#loginModal').modal('show'); </script>";

<?php } else {

require 'includes/header.php';
?> 

***
***
if session type is 
set code goes here
***
***

<div id="loginModal" class="modal fade">
            <div class="modal-dialog">
                <form method="post" id="product_form">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h4 class="modal-title"><i class="fa fa-plus"></i> Add Product</h4>
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                        </div>
                        <div class="modal-body">
                            <div class="form-group">
                                <label>Stock Code</label>
                                <input type="text" name="product_name" id="product_name" class="form-control" required disabled />
                            </div>
                            <div class="form-group">
                                <label name="category_label" id="category_label">Category</label>
                                <select name="category_id" id="category_id" class="form-control" required> 
                                    <option value="">Select Category</option>
                                    <?php echo fill_category_list($connect);?>
                                </select>
                            </div>
                            <div class="form-group">
                                <label>Enter Product Quantity</label>
                                <div class="input-group">
                                    <input type="text" name="product_quantity" id="product_quantity" class="form-control" required pattern="[+-]?([0-9]*[.])?[0-9]+" placeholder="Enter Qty" /> 
                                    <span class="input-group-addon">
                                        <select class="form-control" name="product_unit" id="product_unit" required disabled>
                                            <option value="Box">Boxes</option>
                                        </select>
                                    </span>
                                </div>
                            </div>
                            <div class="form-group">
                                <label>Location</label>
                                <input type="text" name="product_location" id="product_location" class="form-control" required disabled />
                            </div>
                        </div>
                        <div class="modal-footer">
                            <input type="hidden" name="product_id" id="product_id" />
                            <input type="hidden" name="btn_action" id="btn_action" />
                            <input type="submit" name="action" id="action" class="btn btn-success" value="Add" />
                        </div>
                    </div>
                </form>
            </div>
        </div>
<?php } #close else ?>

database_connection.php

<?php

$connect = new PDO('mysql:host=localhost;dbname=continental3', 'root', 'password');
session_start();

?>