PDA

View Full Version : Use this javascript string with PHP



treeleaf20
Nov 14th, 2011, 11:17 PM
Hello,
I have the following Javascript function:


function loadReviews(){
var url='http://search.twitter.com/search.json?callback=?&q=test';
jQuery.getJSON(url,function(json){
jQuery.each(json.results,function(i,review){
jQuery("#divroller_container").append('<div class="reviews" id=item'+i+'>'+review.text+'<br>Reviewed By: '+review.from_user+'</div>');
});
start(30000, 1);
});

}


I'm trying to use this in a PHP page. I tried to make do an echo ' and then display the Javascript but I'm struggling on how to convert the following line to PHP to that it still works like it should in Javascript:


jQuery("#divroller_container").append('<div class="reviews" id=item'+i+'>'+review.text+'<br>Reviewed By: '+review.from_user+'</div>');


I still need the Javascript variables to function the same etc. Any help would be greatly appreciated. Thanks in advance!

Old Pedant
Nov 14th, 2011, 11:23 PM
Why do you need to use echo?

What part of that javascript is going to change, to come from PHP variables???

Why not simply do

<?php
... bunch of php code ...
?>
jQuery("#divroller_container").... the JS code ...
<?php
... more php code ...
?>

If there is one or two things from PHP that need to end up in the JS code, then just put them in, by themselves.

Example:



<?php
... bunch of php code ...
?>
jQuery("#<?php echo $container;?>").append('<div class="<?php echo $classname;?>" id=i.... the JS code ...
<?php
... more php code ...
?>


The mutliple quotes needed to write JS *from* PHP can quickly lead to a nightmare. So don't do it.

treeleaf20
Nov 14th, 2011, 11:26 PM
A couple parts, the url part so this line:


var url='http://search.twitter.com/search.json?callback=?&q=test';


The url would be replaced with a PHP variable and then the other line that would change would be:


start(30000, 1);


The 30000 would be replaced by a PHP variable and also possibly the 1 could be replaced as well.

treeleaf20
Nov 14th, 2011, 11:33 PM
I have my jquery wrapped in <script> tags so when I try and insert my PHP to get those variables, the PHP isn't being reconginized.

Old Pedant
Nov 14th, 2011, 11:36 PM
var url='http://<?php echo $url; ?>?callback=?&q=test';
or even
var url = "<?php echo $url; ?>"; // depending on how much of the url is in php variable




start(<?php echo $startvalue; ?>, 1);
or maybe
start(<?php echo $startvalue . "," . $count; ?>);
or even
start(<?php echo $startvalue;?>, <?php echo $count; ?>);


You know, if your PHP server supports short tags, you can use


start(<?=$startvalue?>, 1);

gets rid of the "echo" and the semicolon.

ASP and JSP programmers use this all that time, and PHP has it as an option. I'm not quite sure why PHP programmers avoid it. (Only excuse I have heard is that they worry that their code might get put on some shared hosting service that doesn't have short tags enabled. Me, I'd immediately change hosting services if that happend.)

Old Pedant
Nov 14th, 2011, 11:38 PM
I have my jquery wrapped in <script> tags so when I try and insert my PHP to get those variables, the PHP isn't being reconginized.

??? I've seen tons of other PHP code where this has worked.

Try a small experiment to be sure that's really the problem.

In your PHP page, do something as simple as


<script>
var foo = "<? echo 'this is from php'; ?>";
alert(foo);
</script>

treeleaf20
Nov 14th, 2011, 11:43 PM
You are right, it did work! I really appreciate the help!

Thanks!