加载 PHP 页面的 Javascript Light Box

标签 javascript php jquery

背景:我试图了解以下场景实际上是如何发生的。

我正在使用colorbox.js灯箱脚本以及当用户单击 <a class='ajax' href='edit_labor.php?edit_id=$id' title=''><span class='oi oi-pencil'>Edit</span></a> 时任何表格行中的链接都会弹出一个灯箱,并且一个 php 页面(位于服务器上)应该加载到灯箱内。

这种情况发生,但是子 PHP 页面 edit_labor.php无法访问父页面planned_labot.php上设置的任何变量。只是为了测试子页面的范围,我实际上可以添加 include('functions.php');在子页面和父页面的标题中。我是否应该因尝试加载相同的函数两次而出现错误?

问题:我不明白jquery.colorbox.js加载时加载的php页面和父php页面之间的父子关系。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
   
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.colorbox/1.6.4/jquery.colorbox-min.js"></script>

<script>
			$(document).ready(function(){
				$(".ajax").colorbox();				
				//Example of preserving a JavaScript event for inline calls.
				});
		</script>
/*
    Colorbox Core Style:
    The following CSS is consistent between example themes and should not be altered.
*/
#colorbox, #cboxOverlay, #cboxWrapper{position:absolute; top:0; left:0; z-index:9999; overflow:hidden; -webkit-transform: translate3d(0,0,0);}
#cboxWrapper {max-width:none;}
#cboxOverlay{position:fixed; width:100%; height:100%;}
#cboxMiddleLeft, #cboxBottomLeft{clear:left;}
#cboxContent{position:relative;}
#cboxLoadedContent{overflow:auto; -webkit-overflow-scrolling: touch;}
#cboxTitle{margin:0;}
#cboxLoadingOverlay, #cboxLoadingGraphic{position:absolute; top:0; left:0; width:100%; height:100%;}
#cboxPrevious, #cboxNext, #cboxClose, #cboxSlideshow{cursor:pointer;}
.cboxPhoto{float:left; margin:auto; border:0; display:block; max-width:none; -ms-interpolation-mode:bicubic;}
.cboxIframe{width:100%; height:100%; display:block; border:0; padding:0; margin:0;}
#colorbox, #cboxContent, #cboxLoadedContent{box-sizing:content-box; -moz-box-sizing:content-box; -webkit-box-sizing:content-box;}

/* 
    User Style:
    Change the following styles to modify the appearance of Colorbox.  They are
    ordered & tabbed in a way that represents the nesting of the generated HTML.
*/
#cboxOverlay{background:url(images/overlay.png) repeat 0 0; opacity: 0.9; filter: alpha(opacity = 90);}
#colorbox{outline:0;}
    #cboxTopLeft{width:21px; height:21px; background:url(images/controls.png) no-repeat -101px 0;}
    #cboxTopRight{width:21px; height:21px; background:url(images/controls.png) no-repeat -130px 0;}
    #cboxBottomLeft{width:21px; height:21px; background:url(images/controls.png) no-repeat -101px -29px;}
    #cboxBottomRight{width:21px; height:21px; background:url(images/controls.png) no-repeat -130px -29px;}
    #cboxMiddleLeft{width:21px; background:url(images/controls.png) left top repeat-y;}
    #cboxMiddleRight{width:21px; background:url(images/controls.png) right top repeat-y;}
    #cboxTopCenter{height:21px; background:url(images/border.png) 0 0 repeat-x;}
    #cboxBottomCenter{height:21px; background:url(images/border.png) 0 -29px repeat-x;}
    #cboxContent{background:#fff; overflow:hidden;}
        .cboxIframe{background:#fff;}
        #cboxError{padding:50px; border:1px solid #ccc;}
        #cboxLoadedContent{margin-bottom:28px;}
        #cboxTitle{position:absolute; bottom:4px; left:0; text-align:center; width:100%; color:#949494;}
        #cboxCurrent{position:absolute; bottom:4px; left:58px; color:#949494;}
        #cboxLoadingOverlay{background:url(images/loading_background.png) no-repeat center center;}
        #cboxLoadingGraphic{background:url(images/loading.gif) no-repeat center center;}

        /* these elements are buttons, and may need to have additional styles reset to avoid unwanted base styles */
        #cboxPrevious, #cboxNext, #cboxSlideshow, #cboxClose {border:0; padding:0; margin:0; overflow:visible; width:auto; background:none; }
        
        /* avoid outlines on :active (mouseclick), but preserve outlines on :focus (tabbed navigating) */
        #cboxPrevious:active, #cboxNext:active, #cboxSlideshow:active, #cboxClose:active {outline:0;}

        #cboxSlideshow{position:absolute; bottom:4px; right:30px; color:#0092ef;}
        #cboxPrevious{position:absolute; bottom:0; left:0; background:url(images/controls.png) no-repeat -75px 0; width:25px; height:25px; text-indent:-9999px;}
        #cboxPrevious:hover{background-position:-75px -25px;}
        #cboxNext{position:absolute; bottom:0; left:27px; background:url(images/controls.png) no-repeat -50px 0; width:25px; height:25px; text-indent:-9999px;}
        #cboxNext:hover{background-position:-50px -25px;}
        #cboxClose{position:absolute; bottom:0; right:0; background:url(images/controls.png) no-repeat -25px 0; width:25px; height:25px; text-indent:-9999px;}
        #cboxClose:hover{background-position:-25px -25px;}

/*
  The following fixes a problem where IE7 and IE8 replace a PNG's alpha transparency with a black fill
  when an alpha filter (opacity change) is set on the element or ancestor element.  This style is not applied to or needed in IE9.
  See: http://jacklmoore.com/notes/ie-transparency-problems/
*/
.cboxIE #cboxTopLeft,
.cboxIE #cboxTopCenter,
.cboxIE #cboxTopRight,
.cboxIE #cboxBottomLeft,
.cboxIE #cboxBottomCenter,
.cboxIE #cboxBottomRight,
.cboxIE #cboxMiddleLeft,
.cboxIE #cboxMiddleRight {
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#00FFFFFF,endColorstr=#00FFFFFF);
}
<?PHP
//I am also including this same file in edit_labot.php
include('functions.php');

//
$test_var_parent = "this is a test string that is undefined in edit_labor.php";

?>

<a class='ajax' href='edit_labor.php?edit_id=1' title=''><span class='oi oi-pencil'>Edit</span></a>

上面是代码,edit_labor.php在加载时尝试引用$test_var_parent但我收到了未定义的通知

最佳答案

PHP 变量仅在服务器被处理以生成您想要的 HTML 时才有意义。

很明显,父页面在第一个 PHP 进程后加载到浏览器,子页面在第二个 PHP 进程发生后加载到浏览器。变量之间不能发生任何交叉访问,因为该过程是单独发生的并且在两个不同的时间点

如果你的子页面想要访问父页面生成的信息,让父页面生成信息并将其存储在全局js变量中。当子页面加载时,其js现在可以访问父页面生成的全局变量。

下面的问题展示了另一种方法。这取决于具体情况,您应该使用哪种方法在页面之间共享信息。

How to pass variables between php scripts?

关于加载 PHP 页面的 Javascript Light Box,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56232030/

相关文章:

php - 显示 WordPress 中某个类别中当前日期的生日

php - 如何在redbean php中使用id更新特定行

php - Shell:SVN 状态管道到 php 以检查语法

javascript - 显示 block /无,函数 javascript

javascript - 如何使用 Prototype JS 在前一个函数完成后执行一个函数

javascript - Bootstrap 在不同的屏幕上无法正常工作

javascript - 试图让击球检测适用于乒乓球。左桨不起作用

javascript - 只允许字母和空格并将每个单词大写

jquery - 如何从 jQuery 调用 WCF Web 服务?

javascript - 将表单发送到电子邮件(AJAX、Bootstrap、Jquery)