Search code examples
javascriptphpbootstrap-modalsession-variables

Calling a modal form after setting a session variable on a page


I want to call a modal form after setting a session variable on a page.

I need some advice. I am stuck on calling the modal after setting a session variable on a page. Please note that I want to call the modal on an if statement behavior, not on a button click.

I've tried testing my modal forms if there was an error on them on a button click and it was working fine and test the session variable if it is really set using an alert command and session variable is really passing a value but the calling of modal is not triggering. I dunno if I'm missing something, sorry in advance.

Here are my code samples:

index.php:-

<?php require('includes/header.php');?>
<?php require('includes/sidebar.php');?>

      //content or body page
     <?php include('Incoming_Home.php');?>

<?php require('includes/modals.php')?>
<?php require('includes/Footer.php')?>

Incoming_Home.php

      //some long codes and inside a table is here
    <td><a class="btn btn-warning btn-sm" href="processUpdate.php?id=<?php echo $myrow["inc_id"];?>">Update</a>
    </td>

<?php 
    if(isset($_SESSION["Update_ud"])){
    echo "<script>
    $(document).ready(function(){
        $('#modal-update-incoming').modal('show');
    });
</script>";

    //test if session is set then alert
    //echo "<script>alert('".$_SESSION["Update_ud"]."');</script>";
  

  //test if session is set then alert
  
    unset($_SESSION["Update_ud"]);
    
  }

?>

processUpdate.php

   if (session_status() == PHP_SESSION_NONE) {
      session_start();
    }

//echo $_GET["id"];

    $_SESSION["Update_ud"] = $_GET["id"];


?>

<script type="text/javascript">
        window.location='index.php';
</script>

Modals Code

 <div class="modal fade" id="modal-update-incoming">
   <div class="modal-dialog modal-lg">
     //some contents
   </div>
 </div>

Solution

  • You could do something like that:-

    <?php if(isset($_SESSION['Update_ud'])){ ?>
    
    <div class="modal hide fade" id="myModal">
        <div class="modal-header">
            <a class="close" data-dismiss="modal">×</a>
            <h3>Success</h3>
        </div>
        <div class="modal-body">
            <p><?php echo $_SESSION['Update_ud']; ?></p>
        </div>
        <div class="modal-footer">
            <a class="close btn" data-dismiss="modal">Close</a>
        </div>
    </div>
    
    <script type="text/javascript">
        $(window).load(function () {
            $('#myModal').modal('show');
        });
    </script>
    <?php } ?>