3

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: ....
    };
  };
});
3
  • 3
    the variable i is not in php scope so it will not be parsed by the php parser the browser runs the javascript on client side Commented Jun 13, 2011 at 17:57
  • @g molvi, yeah. I know.. is there a way to make the variable i in php scope? Commented Jun 13, 2011 at 17:59
  • in your php code, where you need the javascript you can call the javascript file and use it at as you please. Here is an example of php with javascript itamer.com/using-php-and-javascript-together/278 Commented Jun 13, 2011 at 18:01

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

so it is not possible to mix these two scripts together?
At least not in this way, you will have to use ajax, and make a complete request to an specific script on the server side, remember everything in the <? ?> NEVER reach to a client (only the specifically printed with echo for example), you can make some script that take as an argument (using $_GET to fetch the argument) the index and respond with the content of file.
0

You may want to consider using PHP to output the JavaScript file, that way the PHP variables will be available wherever you want them.

The following links better explain this.

http://www.givegoodweb.com/post/71/javascript-php

http://www.dynamicdrive.com/forums/showthread.php?t=21617

Comments

0

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!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.