How to combine PHP and Javascript together?

I have a function in my Javascript script that needs to talk to PHP to get data in my database. But I have a little problem, because I run PHP in a for loop in Javascript, and then inside this loop I get data in my database. But the pointer in the for loop inside the PHP code does not work. Think the problem is escape? Or perhaps this is not possible at all.

Here is 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: ....
    };
  };
});
+3
source share
3 answers

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

You cannot do this:

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

Expands to:

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

i, i - , ?, , i , php- .

+6

1. javascript URL- php.

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

2. , $_GET php script.

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

3. Php .

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

4. , , , PHP.

Php Javascript -. , php script js. .

, !

0
source

All Articles