Mixing PHP Variables with Javascript (Zend Framework and jQuery)

Posted on: Apr 23, 2008 by wenbert

Sometimes, I “mix” PHP variables with Javascript. For example, I have something like this:

function deletenote(notes_id) 
{
    if(!confirm("Delete note? You will not be able to undo this action."))
        return false;
 
    $.post("< ?=$this->baseUrl?>/notes/deletenote",{ 
        skeedl_notes_id: notes_id
    }, function(data){
        $('#notes_id'+data).fadeOut();
    });
}

Note the $this->baseUrl, I need it like that. The thing is, I have that code in my view file (.phtml – in Zend Framework, this is parsed like a normal PHP file). If I remove all the Javascript in my view file and place it in a .js file, the $this->baseUrl will not be parsed. To go around this, I create a hidden input element in my view file with $this->baseUrl echoed as the value. Like this:

// ... in my .phtml file ...
// rest of the php and html code goes here
< input type="hidden" id="base_url" value="<?=$this->baseUrl?>" />
// rest of the php and html code goes here

Then in my .js file (assuming that you have already included this file in your header, or somewhere else), I have something like this:

//This is using jQuery, but you can use document.getElementById('base_url').value if you like
function deletenote(notes_id) 
{
    if(!confirm("Delete note? You will not be able to undo this action."))
        return false;
 
    $.post($('base_url').val()+"/notes/deletenote",{ 
        skeedl_notes_id: notes_id
    }, function(data){
        $('#notes_id'+data).fadeOut();
    });
}

What it does is that jQuery gets the value of the hidden form element. The value of this hidden form element is from a PHP Variable.

So there you go, nothing special. Just something that works and I just wanted to share.


Subscribe to comments Comment | Trackback |
Post Tags: , , ,

Browse Timeline


Comments ( 6 )

nice ething

idleboy added these pithy words on Jul 15 08 at 5:26 PM

Thanks for your tip!
I also move lots of PHP data (HTML really) to JS by using in the :
<?php
echo ‘var myhtml=’.$HTML.’;';
?>
Further in JavaScript you can use the myhtml variable.
To be successful, be careful of quotes in $HTML (str_repl them with un backslash.

Peter C added these pithy words on Aug 23 08 at 5:22 PM

In the previous post, you have to add script tags to the echo’ed variable.
This was removed by the system, sorry.

Peter C added these pithy words on Aug 23 08 at 5:23 PM

Thank you, this was a big problem I was having and you’ve just solved it for me!
All my AJAX requests within Zend work great now.
Instead of using a hidden field, I simply declared it in my page head as a Javascript variable.

Mr B added these pithy words on Jan 16 09 at 11:55 PM

@Mr B, thanks. Yes, sometimes declaring a variable in Head tag works better if the variable is required to “exist” throughout the application.

//head…
//js tag…
jsVar = “< ?=$this->baseUrl ? >”;
//close js tag…
//close head

would also work…

Wenbert added these pithy words on Jan 17 09 at 1:20 PM

it is working if we define

var baseUrl = baseUrl()?>

and this variable will be available in js file like

url: baseUrl+”/notes/deletenote”,

Roshna added these pithy words on Oct 13 09 at 5:01 PM

Add a Comment


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">


© Copyright 2007 eKini Web Developer Blog . Thanks for visiting!