Can you combine js and php?

I have a function in my Javascript script that needs to talk with the PHP to get the data in my database. But I'm having a small problem because I start the PHP in the for loop in Javascript, and then inside that for loop I get the data in my database. But the pointer in the for loop inside the PHP code is not working.. I guess the problem is in escaping? Or maybe it's not possible at all.

Here's my code:

(function() {
  var data = [];
  for(var i = 0; i < 25; i++) {
    data[i] = {
       data1: "<a href='<?= $latest[?>i<?=]->file; ?>'><?= $latest[?>i<?=]->title; ?></a>", // The problems
       data2: ....
    };
  };
});

asked Jun 13, 2011 at 17:51

3

I think you are confused, you are trying to use a variable of javascript in php.

You cannot do this:

<?= $latest[?>i<?=]->file; ?>'

Which expands to:

<?php
   $latest[
?>
 i
<?php
]->file;
?>

how can you possibly know the value of i, if i is a variable generated in the client side?, do not mix the ideas, i is defined in the browser and the php code is on the server.

answered Jun 13, 2011 at 18:01

cyraxjoecyraxjoe

5,5523 gold badges27 silver badges42 bronze badges

2

1.Pass the javascript variable to the URL to another php page.

window.open("<yourPhpScript>.php?jsvariable="+yourJSVariable);

2.Then you can use this variable using $_GET in the php script.

$jsVaribleInPhp=$GET['jsvariable'];
//do some operation
$yourPhpResult;

3.Perform any server side operation in the Php and pass this result.

header("Location:<your starting page>?result=".$yourPhpResult);

4.Redirect back to the page you started from thus passing the result of PHP.

Php and Javascript have different roles in web development. This task can also be performed if there's a php script and js in the same page. But that I leave for the readers to work it out.

Hope this helps!!

answered Dec 26, 2013 at 11:23

AmarAmar

2,1011 gold badge12 silver badges16 bronze badges

Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Posted by Muhamad Sufian Saharuddin

Hi,

My javascript is,

var fvisit  = setTimeout(function(){
        $.niftyNoty({
            type: 'dark',
            title: 'Hello',
            message: 'Lorem ipsum dolor sit amet consectetuer <br> adipiscing elit sed diam nonummy nibh.',
            container: 'floating',
            timer: 55000
        });
        clearTimeout(fvisit);
     }, 3000);

PHP script,

<?php echo validation::safeOutputToScreen($Auth->getAccountScreenName()); ?>

I try combine script like below,

var fvisit  = setTimeout(function(){
        $.niftyNoty({
            type: 'dark',
            title: 'Hello <?php echo validation::safeOutputToScreen($Auth->getAccountScreenName()); ?>',
            message: 'Lorem ipsum dolor sit amet consectetuer <br> adipiscing elit sed diam nonummy nibh.',
            container: 'floating',
            timer: 55000
        });
        clearTimeout(fvisit);
     }, 3000);

But get output like,

Hello getAccountScreenName()); ?>

Thanks

1 Answer

Can you combine js and php?

STAFF

You cannot run PHP inside javascript. You can get away with certain things such as having html that invokes javascript functions in PHP variables etc..

Can I combine PHP and JavaScript?

Besides, PHP and JavaScript similarities, these two languages are a powerful combination when used together. Large numbers of websites combine PHP and JavaScript – JavaScript for front-end and PHP for back-end as they offer much community support, various libraries, as well as a vast codebase of frameworks.

Can I use PHP variable in JavaScript?

PHP provides a method to set the cookie using the setCookie() method. Here, we can set the data or variable in PHP cookie and retrieve it from JavaScript using document. cookie.

Should I use JavaScript or PHP?

The comparison between PHP vs JavaScript ends with the score 3 to 5 – JavaScript beats PHP. Both languages are fairly good in terms of community support, extensibility, and apps they are suited to. JavaScript is certainly more efficient in terms of speed and universality.