2016-11-23 164 views
0

我一直在尝试访问全局范围内的局部变量。我试过global $var方法,但似乎并不奏效。 我试图访问的变量是$word1Txt变量。 这里是我的代码:如何在全局范围PHP中访问局部变量?

HTML:

<form class="form-inline" method="post"> 
         <input class="form-control" type="text" placeholder="First Word" name="word1" autofocus> 
         <input class="form-control" type="text" placeholder="Second Word" name="word2" autofocus> 
         <input class="btn btn-primary form-submit" type="submit" value="Compare"> 
        </form> 

PHP:

<?php 
      require('./wordnik/Swagger.php'); 
      $APIKey = '342eac9900e703079b0050d5f7008eab962195189e75bfbcb'; 
      $client = new APIClient($APIKey, 'http://api.wordnik.com/v4'); 

      if (!empty($_POST['word1'])) { 
       $word1 = $_POST['word1']; 
       $wordApi = new WordApi($client); 
       $word1 = $wordApi->getDefinitions($word1, null, null, 1); 
       global $word1Txt; 
       global $word10; 
       $word1Txt = $_POST['word1']; 
       $word10 = $word1[0]->text; 
      } 
      if (!empty($_POST['word2'])) { 
       $word2 = $_POST['word2']; 
       $wordApi = new WordApi($client); 
       $word2 = $wordApi->getDefinitions($word2, null, null, 1); 
       global $word2Txt; 
       global $word20; 
       $word2Txt = $_POST['word2']; 
       $word20 = $word2[0]->text; 
      } 
      print $word1Txt; 
     ?> 

JS:

$(document).ready(function() { 
       var word1Txt = <?php echo $word1Txt; ?>; 
       var word2Txt = <?php echo $word2Txt; ?>; 

       $('div.word1').prepend("<h3 class='header'>hi" + word1Txt + "</h3>"); 
       $('div.word2').prepend("<h3 class='header'>hi" + word2Txt + "</h3>"); 
      }); 

编辑: 我尝试添加的var_dump($ _ POST);在if语句前面。我得到以下输出。

array(2) { ["word1"]=> string(2) "hi" ["word2"]=> string(2) "no" } 

Fatal error: Uncaught Exception: Unauthorized API request to 
http://api.wordnik.com/v4/word.json/hi/definitions?limit=1: 
unauthorized in 
C:\xampp\htdocs\DictionaryCompare\wordnik\Swagger.php:111 Stack trace: 
#0 C:\xampp\htdocs\DictionaryCompare\wordnik\WordApi.php(176): APIClient->callAPI('/word.json/hi/d...', 'GET', Array, NULL, Array) #1 
C:\xampp\htdocs\DictionaryCompare\index.php(40): 
WordApi->getDefinitions('hi', NULL, NULL, 1) #2 {main} thrown in 
C:\xampp\htdocs\DictionaryCompare\wordnik\Swagger.php on line 111 
+0

不确定问题是什么 - 您尝试访问什么样的变量以及在哪里? – WEBjuju

+0

@WEBjuju对不起。我编辑了我的问题。让我知道如果你需要更多的信息:) – Njinx

+0

它看起来不像你应该需要与此代码全球。这里包含的内容只有一个范围。 –

回答

1

我不知道你是否有正确的价值观在$_POST或没有,但这个被注意到自当你看到这一点也适用范围做!

$_POST['word1'] = 'word1'; 
$_POST['word2'] = 'word2'; 
var_dump($_POST); // here you make sure you have posted right values 

// array(2) { ["word1"]=> string(5) "word1" ["word2"]=> string(5) "word2" } <---- this should have values 

      if (!empty($_POST['word1'])) { 
       $word1 = $_POST['word1']; 

       global $word1Txt; 
       global $word10; 
       $word1Txt = $_POST['word1']; 
       $word10 = $word1[0]->text; 
      } 
      if (!empty($_POST['word2'])) { 
       $word2 = $_POST['word2']; 
       global $word2Txt; 
       global $word20; 
       $word2Txt = $_POST['word2']; 
       $word20 = $word2[0]->text; 
      } 
      print $word1Txt; 
     ?> 

所以一定要通过之前IF语句中使用var_dump($_POST)$_POST正确的价值观。

更新:在这里,我错过了action属性:)!

<form class="form-inline" method="post" action="result.php"> 
          <input class="form-control" type="text" placeholder="First Word" name="word1" autofocus> 
          <input class="form-control" type="text" placeholder="Second Word" name="word2" autofocus> 
          <input class="btn btn-primary form-submit" type="submit" value="Compare"> 
         </form> 
+0

我很抱歉,但我对PHP很陌生,不太明白你希望我做什么? – Njinx

+0

我想$ _POST ['word1']和$ _POST ['word2']都是空的,所以这就是为什么你没有价值$ word1Txt –

+0

对不起,我将上传完整的代码。我有一个向该帖子提交数据的HTML表单。我敢肯定的作品。 – Njinx

0

存在一个名为$GLOBALS的全局变量(借口双关语)。见官方PHP manual

// in the middle of some context 
$GLOBALS['foo'] = 'some text'; 
// in another context and scope, in the same run 
echo $GLOBALS['foo']; 
// echoes `some text`