简体   繁体   中英

Set a JavaScript variable equal to a PHP variable

I downloaded someone's project on GitHub that enabled Facebook users to send Bitcoin to each other. He abandoned development a year ago, and I want to build my own application but I am currently looking at his code and trying to get my development environment set up.

I understand PHP kinda, that its meant for server side scripting and that JavaScript is executed on a client machine (all in the browser, and Node.js is a server side framework)

Anyways, he's doing something peculiar, and its producing an error.

The directory structure is as follows:

inc/
  init.php
  app.php
  btc.php
index.php
settings.php

With many more files but the above will suffice I think.
So I run the command "php -S localhost:8000" and it starts a server

I go to localhost and once the page renders I get these errors in the terminal:

[Sat Apr 12 02:23:11 2014] PHP Notice:  Undefined variable: app_url in /home/arthur/projects/bfb/index.php on line 21

[Sat Apr 12 02:23:11 2014] PHP Notice:  Undefined variable: callback_url in /home/arthur/projects/bfb/index.php on line 22

[Sat Apr 12 02:23:11 2014] PHP Fatal error:  Call to undefined function get_x_rates() in /home/arthur/projects/bfb/index.php on line 23

[Sat Apr 12 02:23:11 2014] 127.0.0.1:59511 [200]: / - Call to undefined function get_x_rates() in /home/arthur/projects/bfb/index.php on line 23

[Sat Apr 12 02:23:11 2014] 127.0.0.1:59512 [200]: /stylesheets/reset.css

[Sat Apr 12 02:23:11 2014] 127.0.0.1:59513 [200]: /stylesheets/btc.css

[Sat Apr 12 02:23:11 2014] 127.0.0.1:59514 [200]: /javascript/chosen.css

[Sat Apr 12 02:23:21 2014] 127.0.0.1:59515 Invalid request (Unexpected EOF)

[Sat Apr 12 02:23:21 2014] 127.0.0.1:59516 Invalid request (Unexpected EOF)

The index.php file has the following general structure:

<?php
//error_reporting(E_ALL);
//ini_set("display_errors", 1);

require 'settings.php';

require 'inc/btc.php';
require 'inc/app.php';

require 'inc/init.php';

?>
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
    <head>
        <meta charset="utf-8" />
        <title>Bitcoin Transactions</title>
        <link rel="stylesheet" href="stylesheets/reset.css" type="text/css" />
        <link rel="stylesheet" href="stylesheets/btc.css" type="text/css" />
        <link rel="stylesheet" href="javascript/chosen.css" type="text/css" />
        <script>var appurl='<?=$app_url?>',
                    hosturl='<?=$callback_url?>',
                    rates=<?=get_x_rates()?>;</script>
        <script type="text/javascript" src="javascript/jquery-1.7.1.min.js">    </script>
    <script type="text/javascript" src="javascript/chosen.jquery.min.js"></script>
    <script type="text/javascript" src="javascript/btc.js"></script>
</head>

<body>
 ....
</body>
</html>

Since he's loading the PHP files in the beginning of the html file, why can't it see the variables I've defined in this settings.php file:

<?
$bucket_addr='-taken-out-for-Stack-Overflow';
$callback_url='https://super-lame-name-1231232.herokuapp.com/';
$app_url='https://apps.facebook.com/c------------------n';
$blockchain_guid="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
$blockchain_pw="xxxxxxxxxxxxxxxx";
$fb_settings=array(
  'appId'  => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
);
$fb_tx_fee=10000; //in satoshi (0.0001 BTC)
$btc_tx_fee=50000; //in satoshi (0.0005 BTC)
$tx_per_page=10;
$revert_duration="60 days";

Thanks for any help!!

May not be the whole picture, but I have encountered the problem with server settings before. Perhaps your server does not support the php echo shortcode of <?=. So I would suggest trying this, replace:

<script>
    var appurl='<?=$app_url?>',
        hosturl='<?=$callback_url?>',
        rates=<?=get_x_rates()?>;
</script>

with:

<script>
    var appurl='<?php echo $app_url; ?>',
        hosturl='<?php echo $callback_url; ?>',
        rates=<?php get_x_rates(); ?>;
</script>

Made a difference on my local machine. Might help. Good luck.

Maybe your php.ini is setting with no short tags active, check your php.ini settings (short_open_tag).

Or you can try to substitute the <? with <?php

Try to replace <? on the beginning of settings.php by <?php

My advice is to reduce mysterious guessing by using a decent PHP IDE , set breakpoints at lines shown in the error log and use the break-on-exception feature so that the IDE stops when it detects the problem. Decent IDEs also usually have a code validators built-in so the IDE may show you what is wrong even without running the code (quite likely when you are working on unstable abandoned code).

  • PHP IDE with very mature debugger and break-on-exception feature is NuSphere's PhpED - #1 PHP debugger as far as I know

  • PHP IDE with very mature code analyzers (they call it "inspections") showing many potential problems without the need to run the code is JetBrains's PhpStorm - #1 PHP code analyzers as far as I know

Here is what I think is happening.

The short_open_tag in the php.ini is indeed off. The file index.php uses the tags <?php and <?= in the code both of which are valid. Hence php is able to read this content.
However, the file settings.php starts with a short tag <? and hence this piece of code is imported without being treated as php code.

PHP is also able to import the files (since the requires are in index.php and is able to search for the variables. But since the variables are defined in files which cannot be read as PHP, it throws an undefined error.

Therefore, you should use a <?php tag instead of <? tag in all the files that you are importing.
There is already an answer on this post that suggests this solution sans explanation.

You will probably get more insight into the problem if you see the source code of the HTML page that is being rendered.

I don't think these files are being included properly. Please try to do this:

require dirname(__FILE__) . '/settings.php';
require dirname(__FILE__) . '/inc/btc.php'; 
require dirname(__FILE__) . '/inc/app.php';
require dirname(__FILE__) . '/inc/init.php';

尝试将变量声明为全局变量,方法是在其声明之前在变量之前添加global关键字。

Setting a JavaScript variables with PHP is helpful, it cannot always be done. For instance, PHP cannot modify external .js documents temporarily. It changes them for good.

You can use the below code.

<script type="text/javascript"> 
var php = <?php echo $phpVar ?>; 
</script>

我看到未定义您在index.php上使用的函数(get_x_rates),将此函数添加到注释中或检查该函数在任何文件中的定义位置?

This was annoying me so ........................

there are a couple of issues:

  1. the files don't have closing php tags + its better to spell out

  2. Did you set up your database? test the function in btc.php by adding in $rates="hello"; return $rates; at the top of the function just under the opening function() {

i have done both and its working, just i didnt set up the database to check the function.

Two things.

First, replace all of the <? with <?php (including in the script).

Then, you should replace (in the script) <?=$some_var?> with <?php echo $some_var; ?> <?php echo $some_var; ?> .

Those are the two things I see. Hope it helps anybody who comes on this question.

It is very simple to do so.

Assume you have a JS variable url . So you have something like

var url = //your url

Because of the echo function of PHP, all you have to do is to run your PHP inside your script tags, and add in something like this

echo "url = //your new url"

That will cause your url variable to be edited.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM