php statements within jquery

By : DanC
Source: Stackoverflow.com
Question!

Is this bad practice to use php statements such as 'if' within a jquery function?

should I really just be writing loads more functions instead of limiting a few using php? it seems to work fine but was wondering if this is a major faux pax?

example:

this is a code snippet, basically if the php variable $url is equal to 'projects' then the code will come back with extra lines, otherwise these will be hidden from other pages. Does this make sense?

timer = setTimeout(function() {
    			$('li#contact').removeClass('cur');
    			$('li#$url').addClass('cur');
    			"; if($url=='projects') { echo"
    			$('ul.projects').fadeIn('slow');
    			$('ul.image_display').css('display', 'block');"; }
    			echo"
    				}, 8625);
By : DanC


Answers

shouldn't you put your php code between php tags then? Otherwise, it will just show up in the source code instead of being processed at the server.

timer = setTimeout(function() {
                    $('li#contact').removeClass('cur');
                    $('li#$url').addClass('cur');
                    "; <?php if($url=='projects') { echo"
                    $('ul.projects').fadeIn('slow');
                    $('ul.image_display').css('display', 'block');"; }
                    echo"
                            } ?>, 8625);

off topic: some better formatting would be nice too :)

By : Fortega


I try to keep all the code separated as much as possible (HTML/CSS/PHP/JS), because I don't like large chunks of JS code within my HTML or PHP. What I usually do with variables is just place them somewhere in the HTML, e.g. <div id="url">value</div> and then retrieve it using jQuery. Depending no the variables, I sometimes have a few of these div's somewhere hidden on the page using display:none.

This way I can still use the same JS code for multiple applications, depending on the retrieved values. I must say though that I have no idea whether this is considered ok, or that this setup/method is (also) not-done.

By : Alec


This is a bad practice in terms of code readability and maintainability, in my opinion. If only because it's two languages embedded within each other, which means you can't understand it without parsing two languages, and for many people that causes a mental 'page fault' as they context switch between parsing JS and parsing PHP.

It is easy enough to have the javascript branch based on what page elements are present, and this is a preferred solution in my opinion. It's better than conditionally providing certain javascript (although I think providing different javascript may begin to make sense when you have an overwhelmingly large amount of js) and it's also better than branching based on URL... what happens when you go and refactor that?

If you really must branch which JS you supply based on PHP, I'd want to see them at least on separate lines... something like:

<?php if($stmt): ?>
$(javascript).code.goes.here
<?php else: ?>
$(alernative.javascript).code.goes.here
<?php endif; ?>

as that would be nicer than having to read and understand two languages in a single line/statement.



This video can help you solving your question :)
By: admin