1

I used some code that i found on the internet that was supposed to autoload a modal but its's not working. This is a live website and i am supposed to bring some changes to it. Not sure what's wrong here:

if (isset($_SESSION['verify'])) {

    echo '
        <script type="text/javascript">
            $(document).ready(function()
            {
                $("#exampleModal").modal('show');
            }
            );
        </script>

        <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog" role="document">
                <div class="modal-content">

                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel">Modaltitle</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>

                    <div class="modal-body">
                        <form action="#" method="post">
                            <input type="text" name="otp" placeholder="Type OTP">
                            <textarea rows="1" cols="25" name="message"placeholder="Type Message"></textarea>
                            <center><button id="button2" type="submit" name="verify_otp">Send Message</button></center>
                        </form>
                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <button type="button" class="btn btn-primary">Save changes</button>
                    </div>
                </div>
            </div>
        </div>';
}

I wanted this to produce a modal without any need for a button to be pressed but on auto load it's not doing that.

S.Alvi
  • 144
  • 1
  • 10
  • 2
    You need some basic debugging. It'll show you that you've not escaped your single quotes inside the single quoted text. `'show'`needs to be `\'show\'` – Robbie Jun 27 '19 at 05:19
  • You also have two `echos`. Turn on display errors while writing / debugging in you php.ini file. See https://stackoverflow.com/questions/1475297/phps-white-screen-of-death (the "18" answer is probably the best) – Robbie Jun 27 '19 at 05:20
  • Possible duplicate of [PHP's white screen of death](https://stackoverflow.com/questions/1475297/phps-white-screen-of-death) – Robbie Jun 27 '19 at 05:21
  • @Robbie sorry about that. It was a simple copy paste error. Nothing else – S.Alvi Jun 27 '19 at 05:26
  • No problems. Turn on the error checking (see "duplicate") and you'll be soon right! Typos/copyPaste errors are part of the game. – Robbie Jun 27 '19 at 05:30
  • @Robbie thanks man. Appreciate the help. I am new to the forum. So thanks again – S.Alvi Jun 27 '19 at 05:32
  • @Robbie the escape method kinda helped, the screen no longer is blank but the code isn't doing what its supposed to do. I edited the question to reflect that. – S.Alvi Jun 27 '19 at 05:40
  • That's another question: nothing we can see from what you've posted that helps. Create a new question with snipped of code, explanation of "what it's supposed to do" and "what does it actually do". Need to know what library you are using, for example. – Robbie Jun 27 '19 at 05:44
  • Instead of escaping quotes inside a massive `echo`, just close the PHP-block `?>`, write your JS code and then open the PHP-block ` – M. Eriksson Jun 27 '19 at 05:44
  • Also, you have some syntax errors in your HTML. The line: `data-dismiss="modal aria-label="Close">` for example is missing the closing `"` after `modal`. This is one of the things most IDE's would have picked up if it wasn't all in one giant echo. – M. Eriksson Jun 27 '19 at 05:49
  • @MagnusEriksson how can i write javascript outside the php tags/if statement when i want it to work only when the verify session is set? – S.Alvi Jun 27 '19 at 06:05
  • 1
    @S.Alvi : As MagnusEriksson pointed out you can write HTML code wrapped in a condition like so ` `, and if you don't like leaving opened `{`, you can use the [alternative syntax](https://www.php.net/manual/en/control-structures.alternative-syntax.php) which is somewhat cleaner for these kind of things – BJRINT Jun 27 '19 at 06:42

3 Answers3

1
    if(isset($_SESSION['verify'])){

    echo"
        <div class='modal fade id='exampleModal' tabindex='-1' role='dialog' aria-labelledby='exampleModalLabel' aria-hidden='true'>
            <div class='modal-dialog' role='document'>
                 <div class='modal-content'>
                      <div class='modal-header'>
                         <h5 class='modal-title' id='exampleModalLabel'>Modaltitle</h5>
                         <button type='button' class='close' data-dismiss='modal' aria-label='Close'>
                             <span aria-hidden='true'>&times;</span>
                         </button>
                      </div>
                      <div class='modal-body'>
                         <form action='#' method='post'>
                              <input type='text' name='otp' placeholder='Type OTP'>
                              <textarea rows='1' cols='25' name='message' placeholder='Type Message'></textarea>
                              <center><button id='button2' type='submit' name='verify_otp'>Send Message</button></center>
                         </form>
                     </div>
                     <div class='modal-footer'>
                       <button type='button' class='btn btn-secondary' data-dismiss='modal'>Close</button>
                       <button type='button' class='btn btn-primary'>Save changes</button>
                     </div>
                 </div>
             </div>
         </div>
         <script type='text/javascript'>
           $(document).ready(function()
           {
              $('#exampleModal').modal('show');
           });
        </script>";}

I have modified your code with changing quotes (single quote to double). This code should work for you. Before running please check following steps:

  1. Is there any JQuery conflict in your code?
  2. Use browser console to check whether there are any javascript errors.

I am assuming you have included required JS and CSS to use modal.

1

First issue: conflicting quotes

if (isset($_SESSION['verify'])) {

echo '
    <script type="text/javascript">
        $(document).ready(function()
        {
            $("#exampleModal").modal('show');
        }
        );

above you have an opening simple quote in theecho ' part and then you again use single quotes on your JS code in .modal('show') part.

Second issue: Absolutelly poorly engineered solution. You are echoing the modal server sides if you have a session variable called "verify" , with a static JS tiny code which just shows the modal. Why to charge the server with that work , if it's an UI concern? Why not to set a cookie server-sides and write a JS script which would show the modal if the cookie is not set? Like this

 <?php
    if(isset($_SESSION["verify"])){
     setcookie("verify","true");
    }
 ?>   
    <script type="text/javascript">
        $(document).ready(function()
        {   
            let cookie = getCookie("verify")
            if(cookie){
                $("#exampleModal").modal('show');
            }
        }
        );


        function getCookie(name) {
              var value = "; " + document.cookie;
              var parts = value.split("; " + name + "=");
              if (parts.length == 2) return parts.pop().split(";").shift();
            }
    </script>

    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">

                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Modaltitle</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>

                <div class="modal-body">
                    <form action="#" method="post">
                        <input type="text" name="otp" placeholder="Type OTP">
                        <textarea rows="1" cols="25" name="message"placeholder="Type Message"></textarea>
                        <center><button id="button2" type="submit" name="verify_otp">Send Message</button></center>
                    </form>
                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save changes</button>
                </div>
            </div>
        </div>
    </div>

I didn't tested that code though, it might work or need to be fine tuned but it's main concept it's clear : separate your concerns as much as you can between the -layers of your application.The view layer (HTML, JS) should communicate via some mechanism (ideally you'd have sent an ajax request rather than a cookie) with the business layer (PHP) , the later tell whether the verify variable exists , and the view layer should then show the modal if necessary.

Third issue:Not readable code Even if you actually wanted to go on with such an bad approach, you are writing it the worst possible way. What if coders or designers wanted to change the quoted HTML ? Do you know how hard it would be to change it without breaking something within those quotes? A better approach would be

<?php if(isset($_SESSION["verify"])){ ?>
<div class='modal fade id='exampleModal' tabindex='-1' role='dialog' aria-labelledby='exampleModalLabel' aria-hidden='true'>
        <div class='modal-dialog' role='document'>
             <div class='modal-content'>
                  <div class='modal-header'>
                     <h5 class='modal-title' id='exampleModalLabel'>Modaltitle</h5>
                     <button type='button' class='close' data-dismiss='modal' aria-label='Close'>
                         <span aria-hidden='true'>&times;</span>
                     </button>
                  </div>
                  <div class='modal-body'>
                     <form action='#' method='post'>
                          <input type='text' name='otp' placeholder='Type OTP'>
                          <textarea rows='1' cols='25' name='message' placeholder='Type Message'></textarea>
                          <center><button id='button2' type='submit' name='verify_otp'>Send Message</button></center>
                     </form>
                 </div>
                 <div class='modal-footer'>
                   <button type='button' class='btn btn-secondary' data-dismiss='modal'>Close</button>
                   <button type='button' class='btn btn-primary'>Save changes</button>
                 </div>
             </div>
         </div>
     </div>
     <script type='text/javascript'>
       $(document).ready(function()
       {
          $('#exampleModal').modal('show');
       });
    </script>
  <?php } ?>

Fourth (and most important) issue: An "alive" website in which you are supposed to make changes that you found on internet and don't know why/how they works ? Dude, please stop doing that !! If you don't know what is that code doing or why it isn't doing it, perhaps you shouldn't be working professionally yet and need more training first.

Gabo Alvarez
  • 139
  • 8
  • All well and good. I needed the reality check but about the last part... i am just an intern and i AM training right now so chill – S.Alvi Jun 27 '19 at 09:34
  • There are multiple issue with this answer. First off, if you set a cookie in PHP after you've already outputted something (headers have already been sent), the JS won't be able to read it until the next request. Also, clients can modify the contents of a cookie, so having that as the state for verification is very insecure. The third thing, I don't see any reason to involve cookies at all here. If the `if`-statement in PHP evaluates as true, you first set the cookie and then output the JS. That means that the JS will only exist if the cookie gets set, which makes me wonder, what's the point? – M. Eriksson Jun 27 '19 at 17:34
  • 1
    However, I 100% agree with the last (fourth) statement. Using code you don't know what it does or exactly how it works is dangerous. Especially since many guides/tutorials out there promotes bad (and often insecure) coding practices. – M. Eriksson Jun 27 '19 at 17:35
  • Well, at this instance I just tried to make a point , it's more like a pseudocode rather than actual production code (and I even made it clear that I didn't test it and that it probably wouldn't work). I would rather : 1) Make an endpoint, for instance /api/session_verifiy or just /api/session in which the Client JS app could get the relevant user's session information 2) Query that endpoint using ajax cliendside and , if the "verified" flag is true, show the modal (or whatever he needs to do) – Gabo Alvarez Jun 27 '19 at 20:14
  • About your security concerns, it's not dangerous at all because the server would just write that cookie as a boolean flag without reading it later , not using it for anything. The client would be the one reading that cookie, and if the user modifies it, then it's client would be the only affected one. And yes, client code tampering is unavoidable and it shouldn't be a security concern if your app is well designed and the only affected one is the user tampering with it and if he can't take advantage of it. – Gabo Alvarez Jun 27 '19 at 21:07
0

A simple approach would be to end the PHP block and write the JS/HTML code. Like below.

if (isset($_SESSION['verify'])) {

?>
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">

                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Modaltitle</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>

                <div class="modal-body">
                    <form action="#" method="post">
                        <input type="text" name="otp" placeholder="Type OTP">
                        <textarea rows="1" cols="25" name="message"placeholder="Type Message"></textarea>
                        <center><button id="button2" type="submit" name="verify_otp">Send Message</button></center>
                    </form>
                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save changes</button>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        $(document).ready(function()
        {
            $("#exampleModal").modal('show');
        }
        );
    </script>
<?php
}
Nisanth Sojan
  • 1,099
  • 8
  • 21
  • 1
    i am not sure if it's an error or not but there is a single quote followed by a semicolon at the end of your html code. Is its supposed to be there; – S.Alvi Jun 27 '19 at 08:27