Solved

simple php script for pop up box

Posted on 2014-04-23
11
21,629 Views
Last Modified: 2014-04-23
I want to allow a user to click on a link and for when they click the link a pop up box opens/pops up... (not a light box) just a basic image.
I want the box to be a png image that I have stored in mysite in my mysite/images folder called image1.png  I also would like to have white text on my png image with a button called continue and have the color of this button red with white text and when pressed turn green and the box to close.

any ideas?
My page is php
0
Comment
Question by:milkywaystudios
11 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
I think your best approach to this would be to learn how jQuery and CSS interact.  The thing you want to learn first is the jQuery UI Dialog. Then you want to learn enough CSS that you can set the color and position of  text and a link button.  These are good learning resources for CSS:
http://www.w3schools.com/css/DEFAULT.asp
http://www.codecademy.com/tracks/web
http://www.w3.org/Style/CSS/learning.en.html
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
Just to clarify - PHP is a server side language and is not used for dynamically displaying content in the browser - for that you need to use javascript.

Here is some code that demonstrates a way to do this
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
	$('body').on('click','.closePopup', function() {
		$('.action input').css({backgroundColor: 'green'}).fadeOut(300, function() {
			$('.popupElement').remove()
		});
	});
	$('a').click(function(e) {
		e.preventDefault();
		var overlay = $('<div/>').addClass('overlay').addClass('popupElement');
		$('body').append(overlay);
		var popup = $('<div/>').addClass('popup').addClass('popupElement').css({left: '-999px'});
		var html = '<img src="' + $(this).attr('href') + '" /><div class="action"><input type="button" value="Continue" class="closePopup"/></div>';
		popup.html(html);
		$('body').append(popup);
		var left = ($(window).width() - popup.width()) >> 1;
		var top = ($(window).height() - popup.height()) >> 1;
		popup.css({left: left + 'px', top: top + 'px'});
	});
});
</script>
<style type="text/css">
.overlay {
	background: #999;
	position: absolute;
	left: 0;
	right: 0;
	bottom: 0;
	top: 0;
	opacity: 0.95;
	filter: alpha(opacity=95);
	z-index: 1;
}
.popup {
	background: #fff;
	border: 2px solid #333;
	border-radius: 5px;
	padding: 10px;
	position: absolute;
	z-index: 1000;
}
.popup img {
	display: block;
	margin-bottom: 15px;
}
.popup div.action {
	text-align: right;
}
.popup div.action input {
	background: red;
	color: white;
	border: red;
}
</style>
</head>
<body>
<a href="images/a3.jpg">Click me</a>
</body>
</html>

Open in new window

Working sample here http://www.marcorpsa.com/ee/t643.html
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
JQueryUI is a bit of overkill for this particular scenario - very easy to do it with a few lines of jquery and some css.

Here is some updated code that includes a resize handler to ensure popup stays in centre of the screen when window size is changed. Also includes some comments on code
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
  // CODE TO CLOSE THE POPUP
  // USE THE .on METHOD IN CASE WE
  // WANT TO MODIFY THIS TO LOAD POPUP
  // CONTENT VIA AJAX
  $('body').on('click','.closePopup', function() {
    // CHANGE BACKGROUND TO GREEN 
  // FOLLOWED BY A FADEOUT TO GIVE
  // A DELAY TO SHOW CHANGE IN COLOUR
    $('.action input').css({backgroundColor: 'green'}).fadeOut(300, function() {
    // REMOVE ALL ELEMENTS WITH THE 
    // popupElement STYLE - INCLUDES OVERLAY
    // AND POUP
      $('.popupElement').remove()
    });
  });
  // HANDLE THE WINDOW RESIZE.
  // WHEN WINDO IS RESIZED - MAKE SURE
  // POPUP STAYS CENTERED.
  $(window).resize(function() {
    // FIND THE POPUP
    var popup = $('#popupWindow');
  // IF IT EXISTS CENTRE IT
    if (popup.length > 0) {
      centerPopup();
    }
  });
  // TRIGER DISPLAY OF POPUP
  $('a').click(function(e) {
    // DISABLE DEFAULT CLICK FUNCTIONALITY FOR <a>
    e.preventDefault();
  // CREATE OUR OVERLAY AND APPEND TO BODY
    var overlay = $('<div/>').addClass('overlay').addClass('popupElement');
    $('body').append(overlay);
  // CREATE OUR POPUP AND POSITION OFFSCREEN.
  // WE DO THIS SO WE CAN DISPLAY IT AND CALCULATE
  // ITS WIDTH AND HEIGHT SO WE CAN CENTRE IT
    var popup = $('<div/>').attr('id','popupWindow').addClass('popup').addClass('popupElement').css({left: '-999px'});
  // CREATE THE HTML FOR THE POPUP
    var html = '<img src="' + $(this).attr('href') + '" /><div class="action"><input type="button" value="Continue" class="closePopup"/></div>';
    popup.html(html);
  // APPEND THE POPUP TO THE BODY
    $('body').append(popup);
  // AND CENTER IT
    centerPopup();
  });
});
// FUNCTION TO CENTER THE POPUP
function centerPopup()
{
  var popup = $('#popupWindow');
  // LEFT AND TOP VALUES IS HALF THE DIFFERENCE 
  // BETWEEN THE WINDOW AND POPUP METRICS.
  // USE THE SHIFT RIGHT OPERATOR TO DO DIV BY 2
  var left = ($(window).width() - popup.width()) >> 1;
  var top = ($(window).height() - popup.height()) >> 1;
  // SET LEFT AND TOP STYLES TO CALCULATED VALUES
  popup.css({left: left + 'px', top: top + 'px'});
}
</script>
<style type="text/css">
.overlay {
  background: #999;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  opacity: 0.95;
  filter: alpha(opacity=95);
  z-index: 1;
}
.popup {
  background: #fff;
  border: 2px solid #333;
  border-radius: 5px;
  padding: 10px;
  position: absolute;
  z-index: 1000;
}
.popup img {
  display: block;
  margin-bottom: 15px;
}
.popup div.action {
  text-align: right;
}
.popup div.action input {
  background: red;
  color: white;
  border: red;
}
</style>
</head>
<body>
<a href="images/a3.jpg">Click me</a>
</body>
</html>

Open in new window

0
 

Author Comment

by:milkywaystudios
Comment Utility
Hmm thanks for the support.
I really want to keep this as basic as possible as my page is running all kinds of things.
I dont want the background to go dark, just when you click a link, show the image in the center and a chance to close the image.

thanks
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
The just make the overlay background whte or remove it entirely - as shown below.
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
  // CODE TO CLOSE THE POPUP
  // USE THE .on METHOD IN CASE WE
  // WANT TO MODIFY THIS TO LOAD POPUP
  // CONTENT VIA AJAX

  $('body').on('click','.closePopup', function() {

    // CHANGE BACKGROUND TO GREEN 
  // FOLLOWED BY A FADEOUT TO GIVE
  // A DELAY TO SHOW CHANGE IN COLOUR

    $('.action input').css({backgroundColor: 'green'}).fadeOut(300, function() {

    // REMOVE ALL ELEMENTS WITH THE 
    // popupElement STYLE - INCLUDES OVERLAY
    // AND POUP

      $('.popupElement').remove()
    });
  });

  // HANDLE THE WINDOW RESIZE.
  // WHEN WINDO IS RESIZED - MAKE SURE
  // POPUP STAYS CENTERED.

  $(window).resize(function() {

    // FIND THE POPUP

    var popup = $('#popupWindow');

  // IF IT EXISTS CENTRE IT

    if (popup.length > 0) {
      centerPopup();
    }
  });

  // TRIGER DISPLAY OF POPUP

  $('a').click(function(e) {

    // DISABLE DEFAULT CLICK FUNCTIONALITY FOR <a>

    e.preventDefault();

  // CREATE OUR POPUP AND POSITION OFFSCREEN.
  // WE DO THIS SO WE CAN DISPLAY IT AND CALCULATE
  // ITS WIDTH AND HEIGHT SO WE CAN CENTRE IT

    var popup = $('<div/>').attr('id','popupWindow').addClass('popup').addClass('popupElement').css({left: '-999px'});

  // CREATE THE HTML FOR THE POPUP

    var html = '<img src="' + $(this).attr('href') + '" /><div class="action"><input type="button" value="Continue" class="closePopup"/></div>';
    popup.html(html);

  // APPEND THE POPUP TO THE BODY

    $('body').append(popup);

  // AND CENTER IT

    centerPopup();
  });
});
// FUNCTION TO CENTER THE POPUP
function centerPopup()
{
  var popup = $('#popupWindow');

  // LEFT AND TOP VALUES IS HALF THE DIFFERENCE 
  // BETWEEN THE WINDOW AND POPUP METRICS.
  // USE THE SHIFT RIGHT OPERATOR TO DO DIV BY 2

  var left = ($(window).width() - popup.width()) >> 1;
  var top = ($(window).height() - popup.height()) >> 1;

  // SET LEFT AND TOP STYLES TO CALCULATED VALUES

  popup.css({left: left + 'px', top: top + 'px'});
}
</script>
<style type="text/css">
.popup {
  background: #fff;
  border: 2px solid #333;
  border-radius: 5px;
  padding: 10px;
  position: absolute;
  z-index: 1000;
}
.popup img {
  display: block;
  margin-bottom: 15px;
}
.popup div.action {
  text-align: right;
}
.popup div.action input {
  background: red;
  color: white;
  border: red;
}
</style>
</head>
<body>
<a href="images/a3.jpg">Click me</a>
</body>
</html>

Open in new window

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
If simple is what you want then I would not put the continue button - but rather would do something like this
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
  $('body').on('click','.closePopup', function() {
    $('.popupElement').remove()
  });

  $(window).resize(function() {
    var popup = $('#popupWindow');
    if (popup.length > 0) {
      centerPopup();
    }
  });

  $('a').click(function(e) {
    e.preventDefault();
    var popup = $('<div/>').attr('id','popupWindow').addClass('popup').addClass('popupElement').css({left: '-999px'});
    var html = '<a class="closePopup"></a><img src="' + $(this).attr('href') + '" /><div class="action"></div>';
    popup.html(html);
    $('body').append(popup);
    centerPopup();
  });
});

function centerPopup()
{
  var popup = $('#popupWindow');
  var left = ($(window).width() - popup.width()) >> 1;
  var top = ($(window).height() - popup.height()) >> 1;
  popup.css({left: left + 'px', top: top + 'px'});
}
</script>
<style type="text/css">
.popup {
  background: #fff;
  border: 2px solid #333;
  border-radius: 5px;
  padding: 10px;
  position: absolute;
  z-index: 1000;
}
.popup img {
  display: block;
  margin-bottom: 15px;
}
.popup div.action {
  text-align: right;
}
.popup div.action input {
  background: red;
  color: white;
  border: red;
}
.closePopup {
	position: absolute;
	display: block;
	width: 32px;
	height: 32px;
	background: url(images/close-button.png) no-repeat 0 0;
	right: -16px;
	top: -16px;
}
</style>
</head>
<body>
<a href="images/a3.jpg">Click me</a>
</body>
</html>

Open in new window

Sample here http://www.marcorpsa.com/ee/t643a.html
0
 
LVL 25

Expert Comment

by:Kyle Hamilton
Comment Utility
here's a super simple one:

<!doctype HTML>
<html>
	<head>
		<title></title>
		<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
		<style>
		*{
			box-sizing:border-box;
			font-family: sans-serif;
		}
			#popUp{
				display:none;
				position:relative;
				width:300px;
				height:150px;
				background:url(images/img.jpg) no-repeat;
				margin:50px auto;
				text-align: center;
				line-height: 150px;
			}

			#popUp .button{
				text-align: center;
				display:inline-block;
			}
			#popUp .button a{
				background-color: red;
				color:white;
				padding:10px 40px;
				height:30px;
				text-decoration: none;
			}
			#popUp .close-popUp{
				width:40px;
				height:40px;
				text-align: center;
				line-height: 40px;
				border:1px solid white;
				color:red;
				position: absolute;
				top:0;
				right:0;
				background-color: rgba(255,255,255,.5);
			}
		</style>
	</head>
	<body>
		<p> <a href="" class="open-popUp">open popup</a> </p>
		<div id="popUp">
			<div class="close-popUp">X</div>
			<div class="button"><a href="http://candpgen.com">Continue</a></div>
		</div>
	<script>
		$(".open-popUp").on('click', function(e){
			e.preventDefault();
			$("#popUp").show();
		})
		$(".close-popUp").on('click', function(e){
			e.preventDefault();
			$("#popUp").hide();
		})
	</script>		
	</body>
</html>

Open in new window

http://candpgeneration.com/EE/simple-popup.html
0
 

Author Comment

by:milkywaystudios
Comment Utility
hi thanks for replies but I worked with julianH and sorted it thanks to all
0
 

Author Comment

by:milkywaystudios
Comment Utility
Hi just an update it works fine, but.... it effects every link on my page which is not good, its only supposed to do the link I want :( any ideas.
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
Yes - I posted sample code and bound the event to <a> elements.

Just give your link a class and bind to elements of that class like this
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
  $('body').on('click','.closePopup', function() {
    $('.popupElement').remove()
  });

  $(window).resize(function() {
    var popup = $('#popupWindow');
    if (popup.length > 0) {
      centerPopup();
    }
  });
// CHANGE THIS TO ACT ON THE CLASS
  $('.myPopup').click(function(e) {
    e.preventDefault();
    var popup = $('<div/>').attr('id','popupWindow').addClass('popup').addClass('popupElement').css({left: '-999px'});
    var html = '<a class="closePopup"></a><img src="' + $(this).attr('href') + '" /><div class="action"></div>';
    popup.html(html);
    $('body').append(popup);
    centerPopup();
  });
});

function centerPopup()
{
  var popup = $('#popupWindow');
  var left = ($(window).width() - popup.width()) >> 1;
  var top = ($(window).height() - popup.height()) >> 1;
  popup.css({left: left + 'px', top: top + 'px'});
}
</script>
<style type="text/css">
.popup {
  background: #fff;
  border: 2px solid #333;
  border-radius: 5px;
  padding: 10px;
  position: absolute;
  z-index: 1000;
}
.popup img {
  display: block;
  margin-bottom: 15px;
}
.popup div.action {
  text-align: right;
}
.popup div.action input {
  background: red;
  color: white;
  border: red;
}
.closePopup {
	position: absolute;
	display: block;
	width: 32px;
	height: 32px;
	background: url(images/close-button.png) no-repeat 0 0;
	right: -16px;
	top: -16px;
}
</style>
</head>
<body>
<!-- GIVE YOUR ELEMENTS A CLASS THAT IS BOUND TO THE EVENT HANDLER -->
<a href="images/a3.jpg" class="myPopup">Click me</a>
</body>
</html> 

Open in new window

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now