dqybjj3497 2019-03-21 23:49
浏览 33

如果未登录则显示模态

I'm trying to make a modal popup display allowing you to log in but only if you are not logged in.

I have tried many solutions but so far can only get the login to display in a new page.

Please see my code below

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();

?>

What am I missing?

On a side note if there is an easier or better way of doing this I'm happy to change my approach

Any help would be greatly appreciated

** UPDATE**

<?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';

?>

The above code shows the modal as expected but only after the page has loaded. removing the document ready function the modal does not load but the rest of the page loads and displays (presumably because I do not use the else condition. When using the else condition as seen below nothing loads.

<?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条回答 默认 最新

  • dqstti8945 2019-03-22 03:30
    关注

    Where did you set $_SESSION['type'] ???

    Please try this 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();
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100