Javascript Popup Windows - php

I have a popup window code that I have used before in login forms. The code displays an in-page popup.
This is the code:
<?php
//In Page Popup Box with Faded Background by Jerry Low #crankberryblog.com
//Find other useful scripts at the Crankberry Blog
//SETTINGS
$fade_amount = 60; //In Percentage
$box_width = 400;
$box_background = 'FFFFFF'; //Hex Color
$box_border_width = 2;
$box_border_color = '999999'; //Hex Color
$close_box = 1; //Do You Want The Close Bar on Top 1 = Yes, 0 = No
$extension = ""; // Other Variables that maybe needed, page number etc.
//Begin Popup Box
$left_margin = ( 0 - ($box_width*0.5) );
$page_url = basename($_SERVER['PHP_SELF']);
if ($extension!="") $page_url .= '?' . $extension;
if (isset($_GET['popup'])) {
echo '<div class="popup" style="width:'.$box_width.'px; background: #'.$box_background.'; margin-left:'.$left_margin.'px;';
if ($box_border_width>1) echo ' border: '.$box_border_width.'px solid #'.$box_border_color.';';
echo '">';
//Close Box
if ($close_box===1) echo '<div class="popup_close">Close (x)</div>';
?>
<!–- START YOUR POPUP CONTENT HERE -–>
Popup content goes in here!
<!–- END OF YOUR POPUP CONTENT HERE -–>
<?php
echo '</div>
<div class="fade" onclick="location.replace(\''.$page_url.'\');" style="opacity: 0.'.$fade_amount.'; -moz-opacity: 0.'.$fade_amount.';filter: alpha(opacity: '.$fade_amount.');"></div>
<div class="fade_container">';
}
?>
Activated Box
This code contains a link that reloads the page with parameters/arguments to show the popup.
I want to update this code to make the popup appear/hide without
This is what I have done so far, yet the popup doesn`t show.
Now I want to update the code to work as follows.
<link rel=StyleSheet href="css/popup.css" type="text/css" media=screen></link>
<?php
//In Page Popup Box with Faded Background by Jerry Low #crankberryblog.com
//Find other useful scripts at the Crankberry Blog
//SETTINGS
$fade_amount = 60; //In Percentage
$box_width = 400;
$box_background = 'FFFFFF'; //Hex Color
$box_border_width = 2;
$box_border_color = '999999'; //Hex Color
$close_box = 1; //Do You Want The Close Bar on Top 1 = Yes, 0 = No
$extension = ""; // Other Variables that maybe needed, page number etc.
//Begin Popup Box
$left_margin = ( 0 - ($box_width*0.5) );
$page_url = basename($_SERVER['PHP_SELF']);
if ($extension!="") $page_url .= '?' . $extension;
{
echo '<div id="pop_up" class="popup" style="visibility:hidden; width:'.$box_width.'px; background: #'.$box_background.'; margin-left:'.$left_margin.'px;';
if ($box_border_width>1) echo ' border: '.$box_border_width.'px solid #'.$box_border_color.';';
echo '">';
//Close Box
if ($close_box===1) echo '<div class="popup_close"><a href="#" ChangeStatus()>Close (x)</a></div>';
?>
<!–- START YOUR POPUP CONTENT HERE -–>
Popup content goes in here!
<!–- END OF YOUR POPUP CONTENT HERE -–>
<?php
echo '</div>
<div id="fade_div" class="fade" onclick="location.replace(\''.$page_url.'\');" style="visibility:hidden; opacity: 0.'.$fade_amount.'; -moz-opacity: 0.'.$fade_amount.';filter: alpha(opacity: '.$fade_amount.');"></div>
<div class="fade_container">';
}
?>
Activated Box
<script>
function ChangeStatus()
{
div = document.getElementById('fade_div').style.visibility;
popup = document.getElementById('pop_up').style.visibility;
alert(popup);
if(popup == "hidden")
{
div = "visible";
popup = "visible";
}
else
{
div = "hidden";
popup = "hidden";
}
}
</script>
ignore the CSS files as it is working fine.
I guess the problem is with the JS. Can anyone help me?

change your javascript to this:
if(popup == "hidden")
{
document.getElementById('fade_div').style.visibility = "visible";
document.getElementById('pop_up').style.visibility = "visible";
}
and
else
{
document.getElementById('fade_div').style.visibility = "hidden"
document.getElementById('pop_up').style.visibility = "hidden";
}

Related

How to apply css in jQuery on variable that is in loop?

Hello guys I search alot but find nothing regarding my problem.
I have one foreach loop in that i have one anchor tag and that will be changed according to my ifelse condition.
Now I want to change color of each if else condition in that. Please help me to do.My code is within jQuery script.
for(var j=0;j<arrayData.length;j++){
var country = "";
if(arrayData[j].key=="40"){
country = "UK";
}else if(arrayData[j].key=="41"){
country = "INDIA";
}else if(arrayData[j].key=="47"){
country = "POLAND";
}else{
country = "GERMANY";
}
check+='<a class="btn btn-success" href="<?php echo base_url()?>uploads/'+arrayData[j].value+'">'+country+
'</a>|';
}
check +='</td></tr>';
You can just append it on the html string.
for(var j=0;j<arrayData.length;j++){
var country = "";
var color = "";
if(arrayData[j].key=="40"){
country = "UK";
color = "red";
}else if(arrayData[j].key=="41"){
country = "INDIA";
color = "orange";
}else if(arrayData[j].key=="47"){
country = "POLAND";
color = "yellow";
}else{
country = "GERMANY";
color = "green";
}
check+='<a class="btn btn-success" style="background-color:' + color + ';" href="<?php echo base_url()?>uploads/'+arrayData[j].value+'">'+country+
'</a>|';
}
check +='</td></tr>';
Other option: You can also add class to <a> tag.
Like <a class="color-red">
CSS:
.color-red {
background-color: red;
}

Check if a string contains a url and get contents of url php

Suppose someone enters a string in a textarea like this
"The best search engine is www.google.com."
or maybe
"The best search engine is https://www.google.co.in/?gfe_rd=cr&ei=FLB1U4HHG6aJ8Qfc1YHIBA."
Then i want to highlight the link as stackoverflow does.
And also i want to file_get_contents to get one image , a short description and title of the page.
Most probably i wanna check if the string contains a url or not -> two times.
On keyup of textarea using jQuery and therefore using the
get_file_contents
When the string is recieved by php.
Possibly how can i do this?
UPDATE
function parseHyperlinks($text) {
// The Regular Expression filter
$reg_exUrl1 = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$reg_exUrl2 = "/[\w\d\.]+\.(com|org|ca|net|uk)/";
// The Text you want to filter for urls
// Check if there is a url in the text
if(preg_match($reg_exUrl1, $text, $url)) {
// make the urls hyper links
return preg_replace($reg_exUrl1, "<a class=\"content-link link\" href=\"{$url[0]}\">{$url[0]}</a> ", $text);
} else if(preg_match($reg_exUrl2, $text, $url)){
return preg_replace($reg_exUrl2, "<a class=\"content-link link\" href=\"{$url[0]}\">{$url[0]}</a> ", $text);
}else{
// if no urls in the text just return the text
return $text;
}
}
This works only if $str='www.google.com is the best' or $str='http://www.google.com is best' but not if $str='http://stackoverflow.com/ and www.google.com is the best'
First off you create the html then you need to an AJAX to request to the server. Consider this sample codes:
HTML/jQuery:
<!-- instead of textarea, you could use an editable div for styling highlights, or if you want, just use a plugin -->
<div id="textarea"
style="
font-family: monospace;
white-space: pre;
width: 300px;
height: 200px;
border: 1px solid #ccc;
padding: 5px;">For more tech stuff, check out http://www.tomshardware.com/ for news and updates.</div><br/>
<button type="button" id="scrape_site">Scrape</button><br/><br/>
<!-- i just used a button to hook up the scraping, you can just bind it on a keyup/keydown. -->
<div id="site_output" style="width: 500px;">
<label>Site: <p id="site" style="background-color: gray;"></p></label>
<label>Title: <p id="title" style="background-color: gray;"></p></label>
<label>Description: <p id="description" style="background-color: gray;"></p></label>
<label>Image: <div id="site_image"></div></label>
</div>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#scrape_site').on('click', function(){
var value = $.trim($('#textarea').text());
$('#site, #title, #description').text('');
$('#site_image').empty();
$.ajax({
url: 'index.php', // or you php that will process the text
type: 'POST',
data: {scrape: true, text: value},
dataType: 'JSON',
success: function(response) {
$('#site').text(response.url);
$('#title').text(response.title);
$('#description').text(response.description);
$('#site_image').html('<img src="'+response.src+'" id="site_image" />');
}
});
});
// you can use an editable div so that it can be styled,
// theres to much code already in the answer, you can just get a highlighter plugin to ease your pain
$('#textarea').each(function(){
this.contentEditable = true;
});
});
</script>
And on your php that will process, in this case (index.php):
if(isset($_POST['scrape'])) {
$text = $_POST['text'];
// EXTRACT URL
$reg_exurl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
preg_match_all($reg_exurl, $text, $matches);
$usedPatterns = array();
$url = '';
foreach($matches[0] as $pattern){
if(!array_key_exists($pattern, $usedPatterns)){
$usedPatterns[$pattern] = true;
$url = $pattern;
}
}
// EXTRACT VALUES (scraping of title and descriptions)
$doc = new DOMDocument();
$doc->loadHTMLFile($url);
$xpath = new DOMXPath($doc);
$title = $xpath->query('//title')->item(0)->nodeValue;
$description = $xpath->query('/html/head/meta[#name="description"]/#content');
if ($description->length == 0) {
$description = "No description meta tag :(";
// Found one or more descriptions, loop over them
} else {
foreach ($description as $info) {
$description = $info->value . PHP_EOL;
}
}
$data['description'] = $description;
$data['title'] = $title;
$data['url'] = $url;
// SCRAPING OF IMAGE (the weirdest part)
$image_found = false;
$data['src'] = '';
$images = array();
// get all possible images and this is a little BIT TOUGH
// check for og:image (facebook), some sites have this, so first lets take a look on this meta
$facebook_ogimage = $xpath->query("/html/head/meta[#property='og:image']/#content");
foreach($facebook_ogimage as $ogimage) {
$data['src'] = $ogimage->nodeValue;
$image_found = true;
}
// desperation search (get images)
if(!$image_found) {
$image_list = $xpath->query("//img[#src]");
for($i=0;$i<$image_list->length; $i++){
if(strpos($image_list->item($i)->getAttribute("src"), 'ad') === false) {
$images[] = $image_list->item($i)->getAttribute("src");
}
}
if(count($images) > 0) {
// if at least one, get it
$data['src'] = $images[0];
}
}
echo json_encode($data);
exit;
}
?>
Note: Although this is not perfect, you can just use this as a reference to just improved on it and make it more dynamic as you could.

Expanded navigation by default

I use the Sidebar Navigation Menu Professional for Magento by CODNITIVE and I am trying to make it expanded by default. Particularly I need a solution to make just the first list item expanded by default. I try to edit Navigation.php in app/code/community/codnitive/sidenav/block/:
$collapsibleClass = '';
if ($config->isCollapsible()) {
$collapsibleClass = ' collapsible';
}
// add collapsible arrow and wrraper
$arrow = '';
$extraStyle = '';
$collapsibleIconPosition = $config->getCollapsibleIconPosition();
if ($config->isCollapsible()) {
$width = $config->getCollapsibleIconType() === 'arrow' ? 8 : 16;
$height = 0;
$expanded = 0;
if ($hasActiveChildren) {
$width = $config->getCollapsibleIconType() === 'arrow' ? 8 : 16;
$height = 16;
}
if ($height == 0) {
$extraStyle = ' display:none;';
}
if ($height == 0 && $collapsibleIconPosition === 'left') {
$liMarginLeft += $width;
}
if ($this->isCategoryActive($category)) {
$expanded = 1;
}
$expanded = ' expanded="' . $expanded .'"';
$spanOnclick = 'onclick="Codnitive.expandMenu(this.parentNode)';
$spanClass = $config->getCollapsibleIconType() . '-' . $collapsibleIconPosition;
$arrow = '<span class="' . $spanClass . ' " ' . $spanOnclick . '" style="width: ' . $width . 'px; height: ' . $height . 'px;' . $extraStyle . '"></span>';
}
If I add this code to make the desired category expanded
if ($category->getId() == '35') {
$expanded = 1;
}
there appear two problems:
The category remains expanded even if another category is active.
The "plus" sign (showing that the category can be expanded) remains but it should be "minus". I guess $collapsibleIconPosition should be 'right'?
if ($height == 0 && $collapsibleIconPosition === 'left') {
$liMarginLeft += $width;
}
Try this, though i'm not sure which parent it's setting to block. I assume it's the first <li> tag above the a tag since the span tag isn't a parent
$( document ).ready(function() {
$('.nav-1').css( "display", "block" );
});
See if this is how you want it http://jsfiddle.net/6VSUm/
You want to execute that javascript anytime after (enough of) your HTML has loaded.
If your site uses jQuery, you can put it in a ready callback:
$(function(){
Codnitive.expand(document.getElementById('the-menu-parent-element-id'))
})
You can put it at the very end of your html document, or at least after all the navigation HTML code.
...
<script type="text/javascript" language="javascript">
Codnitive.expand(document.getElementById('the-menu-parent-element-id'))
</script>
</body>
Or you can put it in a onload callback. (warning: this might interfere with other javascript on your site) If there is an existing window.onload, it is safe to add it at the beginning or end of that function.
window.onload = function(){
Codnitive.expand(document.getElementById('the-menu-parent-element-id'))
}
The element you want to target is: <ul class="level0 collapsible" ...
It doesn't have a id currently, so you can't directly use getElementById().
One solution (not my favorite approach) would be to add an id
<ul id="start-open" class="level0 collapsible" ...
And then expand it:
Codnitive.expand(document.getElementById('start-open'))
I think the best solution would be to modify the HTML to do what expand would do to it:
<li class="level0 nav-1 first parent collapsible" style="margin-left: 0px;">
<span class="plus-right " onclick="Codnitive.expandMenu(this.parentNode)" style="width: 16px; height: 16px;background-position: right center"></span>
<a class="collapsible-wrapper collapse-name" onclick="Codnitive.expandMenu(this.parentNode);return false;" href="#"><span class="category_name">WANT TO BE EXPANDED BY DEFAULT</span></a>
<ul class="level0 collapsible" style="margin-left: 5px; padding-left: 10px;display: block" expanded="1">
<span class="plus-right"... gets the style="...;background-position: right center".
<ul class="level0"... gets expanded="1" and style="...;display:block"
If this doesn't work for you, it would be really helpful to see a live version of your code. (The fiddle, lacking JS and CSS isn't enough)

Showing multiple jquery image sliders on same page using php

Hi I currently have a jquery image slider script that pulls images from a folder and shows them on a page.
Here is the webpage with the working image slider: http://iseeit.no/slidertest/
What I want and have been trying to figure out is to have 9 individual sliders on the same page, here is an example picture that shows how it should look like:
each of the grey boxes represents the sliders
Now I've been trying to make a new div for each slides, but I can't figure out how to do it...
Here is the index.php script:
Slideshow
<script src="scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="scripts/jquery.cycle.all.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#myslides').cycle({
fit: 1, pause: 2, timeout: 100
});
});
</script>
<link rel="stylesheet" href="styles/dynamicslides.css" type="text/css" media="screen" />
</head>
<body>
<?php
$directory = 'images/slideshow';
try {
// Styling for images
echo "<div id=\"myslides\">";
foreach ( new DirectoryIterator($directory) as $item ) {
if ($item->isFile()) {
$path = $directory . "/" . $item;
echo "<img src=\"" . $path . "\" />";
}
}
echo "</div>";
}
catch(Exception $e) {
echo 'No images found for this slideshow.<br />';
}
?>
</body>
</html>
Here is the CSS coding:
#myslides {
padding: -8px;
margin: -8px;
}
#myslides img {
}
So I was wondering how I would do this? I've tried to make a new div but I don't hvae so much experience with php, so I don't think I'm doing it right...
Would love to get some feedback and some help with this.
Here is all the files that has to do with the script:
http://iseeit.no/files/slidertest.rar
If I get your question right, then you have one working slider and want more of them on one page next to each other. If so then you can do it like so:
PHP:
// set the directories of the sliders you want
$directories = array('images/slideshow',
'images/slideshow2',
'images/slideshow3',
'images/slideshow4',
'images/slideshow5');
//create all the image sliders
foreach($directories as $dir) {
try {
echo '<div class="myslides">';
//add slides
foreach( new DirectoryIterator($dir) as $item ) {
if ($item->isFile()) {
echo '<img src="' . $dir . '/' . $item. '" />';
}
}
echo '</div>';
} catch(Exception $e) {
//do nothing, just skip the slider
}
}
And set the jQuery plugin for the slider on the class myslides like so:
<script>
$(document).ready(function() {
$('.myslides').cycle({
fit: 1, pause: 2, timeout: 100
});
});
</script>
You can use floats to place the sliders next to each other. Best practice would be an ul with the sliders as an li. But i left it as an div for now. You can get them next to each other with this CSS:
.myslides {
float: left;
margin: 0 10px 10px 0; //top, right, bottom, left
width: 200px; //set the width and height of the sliders (or maybe the slider is doing it?)
height: 400px;
}
Hope this helps!
update: example code to create 3x3
$directories = array(0, 1, 2, 3, 3, 4, 5, 5, 6, 6, 7, 7);
$i = 0;
echo '<ul>';
foreach($directories as $dir) {
// devide $i by 3, is the outcome 0?
if($i % 3 == 0 && $i != 0) {
//end row
echo '</li>';
//are there more items for the next row?
if($i != count($directories)) {
echo '<li>';
}
} elseif($i == 0) {
echo '<li>';
}
// your code
echo 'Jeah!';
$i++; //add one
}
//close last element and list
echo '</li></ul>';
Output:
<ul>
<li>Jeah!Jeah!Jeah!</li>
<li>Jeah!Jeah!Jeah!</li>
<li>Jeah!Jeah!Jeah!</li>
<li>Jeah!Jeah!Jeah!</li>
</ul>

Cannot change src of image with Javascript in some versions of Chrome

Never could have imagined that chrome would have been the browser giving me grief, but the slideshow for our new website does not function properly in some versions of Chrome.
The error occurs here:
"mainPicture.src = \''.$directory.$current_pic.'\'; setMarkerPos('.$i.')"
Claiming that I can't modify mainPicture, an id that doesn't exist.
Obviously, other browsers don't have a problem seeing this object.
All help is much appreciated!
.
You can find the page here.
Source code:
<?php
/********************************************************************
* GATHER IMAGES FROM IMAGE DIRECTORY *
********************************************************************/
// Define directory where images are stored
$directory = "../../images/slideshow/";
// Create blank array to store image names
$pic_array = array("");
$num_pics;
// Create number to define position of images in array
$counter = 0;
// Gather pictures from image directory
if(is_dir($directory))
{
// Open directory to view files
if($dh = opendir($directory))
{
// Continue while still files to view
while(($file = readdir($dh)) !== false)
{
// Stop if picture limit of 6 reached
if($counter == 6)
break;
// Gather if object found is file or directory
$file_check = filetype($directory.$file);
// If object is a file, add it to the slideshow
if ($file_check == "file")
{
$extension = substr($file, strpos($file, "."));
if ($extension == ".png" || $extension == ".jpg")
{
$pic_array[$counter] = $file;
$counter++;
}
}
}
}
}
// Determine number of pics gathered
$num_pics = count($pic_array);
?>
<html>
<head>
<link href="scripts/slideshow.css" rel="stylesheet" type="text/css">
<?php
/********************************************************************
* CONTROL BEHAVIORS OF SLIDESHOW *
********************************************************************/
?>
<!-- Begin script to control slideshow -->
<script type = "text/javascript">
var thumbTop = 450; // starting value of thumb.style.top (make sure multiple of increment)
var highestTop = 342; // highest point mask can be on screen ,-, (make sure multiple of increment)
var lowestTop = 450; // lowest point mask can be on screen ,_, (make sure multiple of increment)
var delay = 2; // speed in which slide up and down methods are called
var increment = 5; // value that thumbTop increments with each method call
var intervalUp; // interval for thumb upward movements
var intervalDown; // interval for thumb downward movements
function startThumbSlideUp()
{
window.clearInterval(intervalDown);
intervalUp = window.setInterval(thumbSlideUp,delay);
}
function startThumbSlideDown()
{
window.clearInterval(intervalUp);
intervalDown = window.setInterval(thumbSlideDown,delay);
}
function thumbSlideUp()
{
thumbTop -= increment;
if (thumbTop <= highestTop)
{
thumbTop = highestTop;
window.clearInterval(intervalUp);
}
else
thumbMask.style.top = thumbTop;
}
function thumbSlideDown()
{
// Added to fix issue where images would start from too large a height
if (thumbTop <= highestTop)
thumbTop = highestTop;
thumbTop += increment;
if (thumbTop >= lowestTop)
{
thumbTop = lowestTop;
window.clearInterval(intervalDown);
}
else
thumbMask.style.top = thumbTop;
}
// Move marker above image <pos>
function setMarkerPos(pos)
{
marker.style.left = 600 - (66 * (pos)) + 19;
}
</script>
</head>
<?php
/********************************************************************
* DISPLAY SLIDESHOW *
********************************************************************/
// Start body and make images unhighlightable
echo '<body onselectstart="return false" style="margin: 0px;">';
// Create base value to increment horizontal position of thumbnails
$curr_thumb_left = 595; // (ignore this comment) 660<width of image> - 66<width of thumb> - 10<space between side> // 660
// Create and display main (large) image and image thumbnails
echo '<div id="mask" onmouseout = "startThumbSlideDown()" onmouseover = "startThumbSlideUp();">
';
echo '<img src = "'.$directory.$pic_array[0].'" id = "mainPicture" height = "420" width = "660" style = "z-index: -1; position:absolute;"/>
';
echo '<div id="thumbMask" height="66" width="660" style="z-index: 1; top: 450px; position: absolute;">
';
echo '<img id="marker" src="images/marker.png" height="10" width="10" style="z-index: 2; top: 0px; position: absolute;" onload="setMarkerPos(0);"/>';
// Search through image array, then assign names to and display thumbnails
for ($i=0; $i < $num_pics; $i++)
{
// Point to pic in array
$current_pic = $pic_array[$i];
// Create and display image thumbnails
if ($current_pic !== "")
{
echo '<img src = "'.$directory.$current_pic.'" id = "thumb'.$i.'" height = "50" width = "50" style = "left:'.$curr_thumb_left.'px; top: 10px; border: 3px white solid; position: absolute;" onclick = "mainPicture.src = \''.$directory.$current_pic.'\'; setMarkerPos('.$i.')"/>
';
// Move left value to the left [50px width + (3px padding * 2 sides) + 10px space between = 66px]
$curr_thumb_left -= 66;
}
}
echo '</div>';
echo '</div>';
?>
</body>
</html>
Chrome doesn't make image elements available by just their "id" value; you have to use document.getElementById("mainPicture") to get a reference to the DOM element. That'll work in all browsers anyway, so it's safe to just code it that way.
You are attempting to alter the src of mainPicture as though it were a global variable:
mainPicture.src = '.....';
Try referencing mainPicture by its id instead:
document.getElementById('mainPicture').src = '......';
You're not actually setting what mainPicture is, other browsers must be guessing whereas Chrome is doing what it should. Change:
echo '<img src = "'.$directory.$current_pic.'" id = "thumb'.$i.'" height = "50" width = "50" style = "left:'.$curr_thumb_left.'px; top: 10px; border: 3px white solid; position: absolute;" onclick = "mainPicture.src = \''.$directory.$current_pic.'\'; setMarkerPos('.$i.')"/>
to
echo '<img src = "'.$directory.$current_pic.'" id = "thumb'.$i.'" height = "50" width = "50" style = "left:'.$curr_thumb_left.'px; top: 10px; border: 3px white solid; position: absolute;" onclick = "document.getElementById('mainPicture').src = \''.$directory.$current_pic.'\'; setMarkerPos('.$i.')"/>

Categories