i have a code here which is intended to show a Javascript modal when a textbox is not filled (when send button is clicked it will check for unfilled textboxes and checkbox AND SHOW THE MODAL, if everything is fine, it will send an email...Im working on the textbox first). But I have some problem displaying the modal properly. It seems like when i use display: block it will be shown along with other objects in the form. but when i set it to display: none, nothing is being displayed even after the send button is clicked.
Here's the CSS for modal:
#overlay {
display: block;
position: absolute;
left: 0px;
top: 0px;
width:100%;
height:100%;
text-align:center;
z-index: 800;
}
#overlay div {
width:800px;
margin: 100px auto;
background-color: none;
border:none;
padding:15px;
text-align:center;
}
body {
height:100%;
width:100%;
margin:0;
padding:0;
overflow:auto;
}
HTML:
<div id="overlay">
<div>
<img src="images/NoUsername.png" alt="module" style="width:469px; height:345px;">
<p>[<a href='#' onclick='overlay()'>close</a>]</p>
</div>
I got these codes from http://raventools.com/blog/create-a-modal-dialog-using-css-and-javascript/
Here is the PHP code for checking the textbox:
if ($username == "") {
echo
"<script type=\"text/javascript\">
function overlay() {
el = document.getElementById(\"overlay\");
el.style.visibility = (el.style.visibility == \"visible\") ? \"hidden\" : \"visible\";
}</script>";
}
I tried using double and single quotations here but nothing happens. I think the problem is in my CSS. Please feel free to edit my code. Thanks in advance for your help guys!
As I commented, you should study a little more CSS and HTML before going straight through programming like PHP and JavaScript. Your question will probably be flagged as low quality before you can really learn something meaningful from it.
I think the visibility attribute isn't suitable for your needs, as it allocates space in the DOM for the element even though it's hidden. I also think you should choose between showing the modal by PHP (server-side) or by JavaScript (client-side) as what you're doing now is becoming very confuse, even more for a novice.
I would modify your CSS to:
#overlay {
display: none; /* hidden if not overwritten by style="" attribute or javascript */
position: fixed; /* fixed makes more sense here as it doesn't depend on the element's parents, search for it */
...
IF you choose to show or hide the modal by php, it would be something like
<div id="overlay" <?php if ($username == "") echo 'style="display: block;"' ?>>
<div>
<img src="images/NoUsername.png" alt="module" style="width:469px; height:345px;">
...
OR, in my opinion the best approach, making it entirely in JavaScript (client-side), without relying on php:
<script type="text/javascript">
function show_modal() {
el = document.getElementById("overlay");
el.style.display="block";
}
function hide_modal() { // You'll want to use this for the close button
el = document.getElementById("overlay");
el.style.display="none";
}
function verify_elements() {
el = document.getElementById("username");
if (el.value == '') {
show_modal(); // if the username is '', show'em the modal
return false; // returning false makes sure the form isn't submited
}
}
</script>
<form action="" method="post" onsubmit="verify_elements()">
<input name="username" id="username" value="" />
...
I hope it helps to put you in right path. But you should really go step-by-step into this if you really want to learn instead of copy-and-paste forever :D
Best Regards!
I think that you missed to call the function added to the page:
if ($username == "") {
echo
"<script type=\"text/javascript\">
function overlay() {
el = document.getElementById(\"overlay\");
el.style.visibility = (el.style.visibility == \"visible\") ? \"hidden\" : \"visible\";
}
overlay();
</script>";
}
Also, I believe that the overlay should be hidden by default:
#overlay {
display: block;
position: absolute;
left: 0px;
top: 0px;
width:100%;
height:100%;
text-align:center;
z-index: 800;
visiblity: hidden;
}
Related
I need to display most of a page's content before outputting the content from a function. The function takes a while to execute, and returns some content for the page. I need the rest of the page to display first, then run the function, and then 'insert' the function output.
It would seem that I'd need to use ob_start at the beginning of the function, then store that data in a variable with ob_get_content(). But I'd also need to run the function after the rest of the page displays.
Here's some code I have tried without success (this is the content area inside the body tag):
<div id='part1'>
<p>here is part 1 of the content</p>
</div>
<div id='part2'> // this is where the function output will be inserted after processing it
<p>part 2</p>
<?php echo long_function();?>
</div>
<div id='part3'>
<p>this is part 3 of the content
</div>
<?php
function long_function() {
// some process that takes a long time to get data
// and stores it in the $part2_content
return $part2_content;
}
The result should be that the part1 and part3 div content should be displayed. And then, when the long_function() finishes with gathering up it's data, that data should be output in the 'part2' section.
I've tried to put an ob_start() at the beginning of long_function(), then $part2_content = ob_get_contents() to store that output data (without an ob_flush).
I think that I may need to add some sort of DOM 'write' to the 'part2' ID, but not sure of the right combination to accomplish.
I believe you can't do the server side rendering.
php is back-end language, that run the hole script file, then last thin print the output
for example:
in a function:
you cant return then call code after
function sayHi(){
$word = 'hi';
return "bye";
echo $word; // this code will never work :)
}
sloution ?
so you need to use Clint side rendering
for example use ajax
AJAX stands for Asynchronous JavaScript and XML. AJAX is a new technique for creating better, faster, and more interactive web applications with the help of XML, HTML, CSS, and Java Script.
Based on this answer which is completely acceptable:
https://stackoverflow.com/a/46871956/17292588
PHP is a server-side programming language. It gets executed when you
request a page. So you cannot execute it after the page has been
loaded. If you need to check if something has loaded on the client
side, you'll have to use a client-side programming language like
JavaScript.
I suggest you to putting this long_function in a another route and request it with Ajax during (or after) loading page.
Based on the answers provided and links (as well as remembering the PHP is server-side so can't change things after the page is rendered), I came up with this code which worked for my purposes. Change the <some-page-on-your-site> to the page you want to get as needed.
I included a CSS-only spinner so you can watch something while the Ajax request is working.
<!DOCTYPE html>
<html>
<head>
<style>
/* for the loading spinner */
#spinner {
display:none;
position: fixed;
margin: auto;
width: 30%;
background-color: white;
top:40%;
left:30%;
border:thin solid darkblue;
}
.spinner {
border: 8px solid gray;
border-radius: 50%;
border-top: 8px solid green;
border-right: 8px solid lightgreen;
border-bottom: 8px solid green;
border-left: 8px solid lightgreen;
width: 30px;
height: 30px;
-webkit-animation: spin 2s linear infinite;
animation: spin 2s linear infinite;
}
.spinner_book {
transform: rotate(360deg);
-webkit-transform: rotate(360deg);
overflow: hidden;
transition-duration: 0.8s;
transition-property: transform;
}
#-webkit-keyframes spin {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
}
#keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</head>
<body onload='loadDoc()'>
<div>
<div id='ajax_results'>
<div align='center'>
<p align='center' class='spinner'></p>
<p align='center'>Please wait we get the data ...</p>
</div>
</div>
</div>
<script>
function loadDoc() {
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById("ajax_results").innerHTML = this.responseText;
}
xhttp.open("GET", "<some-page-on-your-site>");
xhttp.send();
}
</script>
</body>
</html>
You can add query parameters to the <some-page-on-your-site> .
Note that the initial text in the 'ajax_results' div (the spinner and message) will be replaced by the contents of the request. So nothing needed to remove the spinner after the request is completed.
Thanks for the answers which helped me get to my solution. I hope this helps others who wander here.
I am trying to figure out how to display an image while PHP runs and disappears after.
I grabbed this code from a site, but the image only shows very briefly at the very end of the PHP loading. It doesn't show when the page initially opens and it only seems to run once.
I have read many and many of websites and threads on here, but I can't figure out what is missing in this simple example. Is there a better way to do this? Or is this it and I just need to fix it?
THANK YOU in advance!
<html>
<head>
<title>Home</title>
<style>
/* This only works with JavaScript,
if it's not present, don't show loader */
.no-js #loader { display: none; }
.js #loader { display: block; position: absolute; left: 100px; top: 0; }
.se-pre-con {
position: fixed;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
z-index: 9999;
background: url(http://smallenvelop.com/wp-content/uploads/2014/08/Preloader_51.gif) center no-repeat #fff;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.2/modernizr.js"></script>
<script>
// Wait for window load
$(window).load(function() {
// Animate loader off screen
$(".se-pre-con").fadeOut("slow");;
});
</script>
</head>
<body>
<div id="loader" class="se-pre-con"></div>
<?php
include 'content/screen.php';
?>
</body>
</html>
SOLVED! I found and modified this AJAX code that worked for exactly what I was looking for (same page load with multiple options on what to load (by links). Thanks for all of the helpful messages directing me on the right path! This community is awesome!
<head>
<title>Demo</title>
<style>
#fade {
display: none;
position:absolute;
top: 0%;
left: 0%;
width: 100%;
height: 100%;
background-color: #ababab;
z-index: 1001;
-moz-opacity: 0.8;
opacity: .70;
filter: alpha(opacity=80);
}
#modal {
display: none;
position: absolute;
top: 45%;
left: 45%;
width: 64px;
height: 64px;
padding:30px 15px 0px;
border: 3px solid #ababab;
box-shadow:1px 1px 10px #ababab;
border-radius:20px;
background-color: white;
z-index: 1002;
text-align:center;
overflow: auto;
}
</style>
<script>
function openModal() {
document.getElementById('modal').style.display = 'block';
document.getElementById('fade').style.display = 'block';
}
function closeModal() {
document.getElementById('modal').style.display = 'none';
document.getElementById('fade').style.display = 'none';
}
function loadAjax(page) {
document.getElementById('results').innerHTML = '';
openModal();
var xhr = false;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
if (xhr) {
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
closeModal();
document.getElementById("results").innerHTML = xhr.responseText;
}
}
xhr.open("GET", "content/"+page+".php", true);
xhr.send(null);
}
}
</script>
</head>
<body>
<div id="content">
Click to load page 1<br/><br/>
Click to load page 2<br/><br/>
<div id="results"><!-- Results are displayed here --></div>
<div id="fade"></div>
<div id="modal">
<img id="loader" src="loading.gif" />
</div>
</div>
</body>
</html>
It has all to do with the output buffering PHP applies.
This Stack Overflow link explains why it doesn't work as expected, a possible way to make it work and why you shouldn't make it work that way.
PHP always (unless specifically told not to) buffers the output before printing it. That means that when you actually print, PHP just stores the output text in the memory. After everything is printed, the contents stored in the memory gets printed and the memory gets flushed. It is not only PHP that does that. Almost all the I/O libraries across many languages and platforms has this feature, which is generally enabled by default.
Here is a relevant link that shows all the possible options to bypass or disable this feature. I personally think that you shouldn't disable it because the image will still need to be loaded and you won't be able to control the latency between PHP loading and image loading. I think in this situation maybe a solution that involved Ajax is more suitable for your needs.
Are you trying to show a loading animation/image for the PHP operation? If yes, then you should definitely do it with Ajax on a separate action.
Edit: sorry about not pasting the link: How to disable output buffering in PHP
Here's how to apply Show image while page is loading to your situation.
Replace your php tag with a div like this:
<div id="main"></div>
Then change your fadeout script like this:
<script>
$(document).ready(function() {
$("#main").load("content/screen.php", function () {
// Animate loader off screen
$(".se-pre-con").fadeOut("slow");
});
});
</script>
I found what I was looking for on this link How to show ajax loading gif animation while the page is loading? but I dont know how to implement this on my website.
I want a loading GIF to appear while the page loads and disappears by itself once the page is completely loaded with PHP results.
On the website I use one page template (index.php) and I have all data loaded in that page dynamically according to the user's query.
Thank you in advance.
You will find everything you need here
http://mycodeprograms.blogspot.ca/2012/10/how-to-add-loader-while-page-load.html
When the page is completely loaded, the first thing that happens is the <body>'s onload event fires. This is where you make the image disappear.
For example, make the <body> tag look like this:
<body onload="makeLoadingGifDisappear()">
Somewhere in the page body give your GIF an ID:
<img src="loadinggif.gif" id="myLoadingGif">
Then in the page's JavaScript, make the makeLoadingGifDisappear function hide the GIF:
function makeLoadingGifDisappear() {
document.getElementById('myLoadingGif').style.display = 'none';
}
Well, this is my solution.
1) CSS part
#loadgif {
padding-top:2px;
font: 10px #000;
text-align:center;
vertical-align:text-top;
height: 80px;
width: 130px;
/* Centering the div to fit any screen resolution */
top: 50%;
left: 50%;
margin-top: -41px; /* Div height divided by 2 including top padding */
margin-left: -65px; /* Div width divided by 2 */
position: absolute;
display: none; /* JS will change it to block display */
position: fixed;
z-index: 1000;
/* Loading GIF set as background of the div */
background: #FFF url('../img/loader.gif') 50% 75% no-repeat;
/* Misc decoration */
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
border:#7580a8 solid 1px;
}
2) HTML part
Place div tag within body tag anywhere.
<!-- With or without text -->
<div id="loadgif">Loading...</div>
3) Javascript part
I made two functions, one to show and another to hide the div.
<script type="text/javascript">
// Hide function by changing <div> style display attribute back to none
function hideloadgif() {
document.getElementById('loadgif').style.display = 'none';
}
// Show function by changing <div> style display attribute from none to block.
function showloadgif() {
document.getElementById('loadgif').style.display = 'block';
}
// Making sure that any other event running in the background isn't affected
if (window.addEventListener) { // Mozilla, Netscape, Firefox
window.addEventListener('load', WindowLoad, false);
} else if (window.attachEvent) { // IE
window.attachEvent('onload', WindowLoad);
}
// Call the hideloadgif() function on click event,
// with interval time set to 3 seconds to hide the <div>
function WindowLoad(click) {
setInterval("hideloadgif()",3000)
}
</script>
4) Showing the div.
Call function showloadgif() using onlick="" event anywhere.
For example
<img src="abc/def.jpg" onlick="showloadgif()">
Once the image is clicked, the div will appear and at the same time, hideloadgif() will trigger and hides the div within 3 seconds.
function WindowLoad(click) {
setInterval("hideloadgif()",3000)
}
Sorry if this question has been asked a lot. I am currently working on a site that will have a custom gallery. Of all things to get stuck on right now, its the gallery page indicator.
On this page, the left portion will be made of a gallery of "galleries", displaying 6 galleries per "page". http://www.ct-social.com/ctsdev/aff/news-and-events/
Above the gallery are small blue dots that will serve as gallery indicators. The code to create the gallery indicators is as follows:
<?php for ($i=0; $i < $n2; $i++): ?>
<div class="event-gallery-unselected"></div>
<?php endfor; ?>
Upon loading I would like the left most dot to be given a different style that is attributed to class="event-gallery-selected". Upon clicking any other dot (except the current selection) the currently selected dot needs to revert back to "event-gallery-unselected" and the clicked dot takes on "event-gallery-selected"
I am kinda new to PHP, very new to JavaScript and JQuery. If using either of those languages as an example could you please breakdown your explanation? Thank you very much for all of your help.
Updated code:
CSS
.event-gallery.selected {
position: relative;
top: -0.7em;
background: white;
background-color: #1e93bb;
width: 20px;
height: 20px;
margin: 7px;
border-radius: 50%;
}
.event-gallery {
position: relative;
top: -1.1em;
background: white;
background-color: #63c5e7;
width: 15px;
height: 15px;
border-radius: 50%;
margin: 7px;
float: right;
cursor: pointer;
}
Updated Code JS
<script type="text/javascript">
jQuery(document).ready(function($){
$(".event-gallery").click(function() {
$(this).addClass('selected').siblings().removeClass('selected');
});
});
</script>
Just got it working now.
I would suggest having a class which is present on all of your gallery div elements. This will allow the common styles to be maintained and also allow you to have only 1 click handler. You can then have a separate selected class which you toggle as needed. Try this:
<?php for ($i = 0; $i < $n2; $i++): ?>
<div class="event-gallery"></div>
<?php endfor; ?>
.event-gallery {
color: #000; /* default styling ... */
padding: 5px;
}
.event-gallery.selected {
color: #FFF; /* selected item styling ... */
background-color: #C00;
}
$(".event-gallery").click(function() {
$(this).addClass('selected').siblings().removeClass('selected');
});
Example fiddle
use jQuery like this:
$('divid').click(function(){
$('divid').css('background-image', 'pic2.jpeg');
});
for example
If you have a set of elements:
<div class="wrapper">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
I typically handle it as such:
var $items = $('#wrapper .item');
$('.item').click(function(){
$items.removeClass('active'); // 'reset' the active links
$(this).addClass('active'); // apply the active class to the clicked item
})
This can easily be done with a little bit of help from jQuery.
$(function() {
$(".even-gallery-unselected").on("click", function() {
this.removeClass("event-gallery-unselected")
.addClass("event-gallery-selected");
});
});
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Javascript Confirm popup Yes, No button instead of OK and Cancel
Please someone help me to make a confirm box with button "Yes" and "No" without using jquery or VB.Script. I have searched a lot but I got all of the answers with jquery but that is not my requirement.
I have to call another function from confirmbox function. The code I am using below
HTML
Delete
And Javascript
<script type="text/javascript">
function show_confirm(cat_id)
{
var conf = confirm("Are you sure you want to delete the file");
if (conf ==true)
{
deleteCatagory(cat_id);
}
}
function deleteCatagory(cat_id)
{
var obj = document.getElementById("file_"+cat_id);
callAjax(cat_id,obj);
}
</script>
Very simple. You'll have to custom code an HTML box that has a Yes and No button. The Yes button executes the code and hides the box, the No button just hides the box. Something as simple as this would do:
HTML
<div id="confirmation">
Are you sure you want to delete the category?<br>
<input type="button" onclick="deleteCategory(document.getElementById('catID').value);this.parentNode.style.display='none';" value="Yes">
<input type="button" onclick="this.parentNode.style.display='none';" value="No">
<input type="hidden" id="catID" value="0">
</div>
CSS
<style type="text/css">
<!--
#confirmation {
display: none;
position: absolute;
top: 50%;
left: 50%;
background-color: white;
border: 2px solid white;
padding: 3px;
text-align: center;
width: 200px;
height: 50px;
}
</style>
Updated show_confirm():
function show_confirm(catID) {
document.getElementById('catID').value=catID;
document.getElementById('confirmation').style.display='block';
}
You need to create it yourself, this: http://dev.sencha.com/deploy/ext-4.0.0/examples/message-box/msg-box.html seems to have a way to do it but this is from 5 minutes of googling. Others suggest using a div to do it, which is my personal preference.
If you're fine with the "Ok" or "Cancel" options the confirm box gives you, you only need to fix the typo in your call. I'd do it like this
Delete
If you however want to change the default text then you're out of luck with the default confirm popup. You'll have to come up with a html based "popup".