2016-11-23 139 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`