Jump to content

Open modal after php wont work


AzeS
Go to solution Solved by AzeS,

Recommended Posts

im trying to open a modal div after the triggering event from a form, and it doesnt work, 
soon as i click the button it Flashes briefly anfd then... nothing

 

 php

 if (isset($_POST['btn_exe_det_prf'])) {
 	$id = strip_tags($_POST['btn_exe_det_prf']);
 	$id = trim($id);
 	$reviews = $profile->coment_get($id);
 	echo $id;
echo "<script>
         $(window).Onload(Despair());
    </script>";
 }

js

  <script>
// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal 
function Despair() {
    modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}
</script>

 

Link to comment
Share on other sites

How are these two code samples connected?

 

PS - you might think about structuring your scripts to completely separate the html, the php and the js. Also you might build a proper web page rather than just echoing out some random js code in the middle of whatever is going on.

Link to comment
Share on other sites

  • Solution

Solved by my self, im sorry, i need to have more patience with my brain....
 
php

 if (isset($_POST['btn_exe_det_prf'])) {
 	$id = strip_tags($_POST['btn_exe_det_prf']);
 	$id = trim($id);
 	$reviews = $profile->coment_get($id);
 	echo $id;
echo "<script>
         $(window).Onload(Despair());
    </script>";
 }

Html

<div id='myModal'>Some content</div>

js

 <script>
// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal 
function Despair() {
    modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}
</script>

solution:
 
php
 

if (isset($_POST['btn_exe_det_prf'])) {
 	$id = strip_tags($_POST['btn_exe_det_prf']);
 	$id = trim($id);
 	$reviews = $Shop->coment_get($id);
 	echo $id;
echo "<script>
         var trigger = 1;
    </script>";
 }

html
 

<div id='myModal'>Some content<span>close</span></div>

js

 <script>
// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal 
function Despair() {
    modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}
</script>

solution 

at the end of the script:
 

<script>
        if (trigger == 1) {
           $(window).Onload(Despair());
        trigger = 0;
        }
      
</script>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.