0

i have a problem with javascript. i´d like to use colorbox to display some responsemessages. is there anyone who knows how to refer to a php script variable within a javascript?

to bring an example

<?php if(isset($msg)):?>
<script type="text/javascript">
$(document).ready(function(){
$.colorbox({iframe:true, href:"<?php print variable $message from this site", scrolling:false, innerWidth:"408px", innerHeight:"292px", opacity:0.75, overlayClose:false, escKey:false, });
});
$("#colorboxCloseBtn").click(function() {
$.colorbox.close();
});
</script>
<?php endif; ?>

i´m not sure if i do understand the usage of iframe, inline, html. if i use iframe this will open a complete idependent site without any relation to the mainpage, isn´t it? inline i use, when i would like to relate on content which is on the mainpage, like i like to, right? and html... don´t know.

thanks for help.

2 Answers 2

2

no, this is barking up the wrong tree the way you're doing this, you need to put message into a session then have href get the full url of the new file then have that file echo nothing but $message:

//EG:your.php
<?PHP       
   session_start();
   $_SESSION['message'] = $message;
   if(isset($msg)) {
     ?>        
     <script type="text/javascript"> 
       $(document).ready(function(){
            $.colorbox({iframe:true, href:"message_get.php", scrolling:false, innerWidth:"408px", innerHeight:"292px", opacity:0.75, overlayClose:false, escKey:false });
            $("#colorboxCloseBtn").click(function() {
                $.colorbox.close();
            });
        });
     </script>
   <?php 
   } 
 ?> 

then a whole new document

//EG: message_get.php
<?PHP       
   session_start();
   echo $_SESSION['message'];
?>

to be honest i'd get rid of the iframe you probably don't need it because this is a very ineligant way of doing this. You look to me like you've learnt ASP.NET first

Sign up to request clarification or add additional context in comments.

7 Comments

hello and thanks for posting. this is working like i dreamed of :) the only thing i do not understand is that errormessage that i will get when i will call the page: Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent
the call to session_start(); on your.php has to be right at the top
problem is that the box need its own installation. means that before i can call the function it needs to be defined with a link to css and the js file. when i set it to top another errmessage will be shown: Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively in Unknown on line 0
ok. The Error message is because sessions=cookies and cookies cannot be sent after you've started piping html out.. cookies must get nominated in the document header (you don't see/smell/touch that header 99 times out of a 100 it's not the <head> tag)..
so to fix it you need to make sure you call session_start() somewhere up the "include tree" (you can use DIE("dead !".__FILE__.__LINE__); to degbug view the source in your browser until the "dead!" text appears above the very first bit of HTML pipe (EG: echo and print and response.write all output to the html pipe) read more here [link]php.net/manual/en/function.headers-sent.php[/link]
|
1
<?php if(isset($msg)):?>
    <script type="text/javascript">
       $(document).ready(function(){
           $.colorbox({iframe:true, href:"<?php echo $message ?>", scrolling:false, innerWidth:"408px", innerHeight:"292px", opacity:0.75, overlayClose:false, escKey:false });
           $("#colorboxCloseBtn").click(function() {
               $.colorbox.close();
           });
       });
    </script>
<?php endif; ?>

using <?php echo $message ?> will be what you are after

I have moved the click method inside the ready() function too - this ensures the DOM element is ready before attaching the click handler.

I have also removed the last comma from escKey:false in the colorbox function call

5 Comments

@John well ... are you sure $message is defined ... as your if statement checks for $msg not $message
yes it is. i´m using the variable from php which defines the text. if i use this the colorbox even won´t start! if i replace that with an external link like href:"example.php" that will pop up the box and show me the content of "example.php" like it should to.
@John please check that $message is actually set ... do alert('message = <?php echo $message ?>'); and see what happens
the variable is set correct. i know that because the same variable will be called on another place where i created a div where i it will show up. and i even know that it is "$msg". i just posted another, sorry. <div><!--<?php if(isset($msg)):?> <div id="msg"> <?php echo $msg;?> </div> <?php endif;?> </div>-->
to avoid misunderstandings: <?php if(isset($msg)):?> <script type="text/javascript"> $(document).ready(function(){ $.colorbox({iframe:true, href:"<?php echo $msg ;?>", scrolling:false, innerWidth:"408px", innerHeight:"292px", opacity:0.75, overlayClose:false, escKey:false, }); }); $("#colorboxCloseBtn").click(function() { $.colorbox.close(); }); $(document).ready(function() { /* Automatically resize to content var y = $(document.body).height(); var x = $(document).width(); ... <?php endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.