2012-03-28 66 views
1

我正在使用PHP爲小型項目生成條形碼,但似乎無法使其工作。我是PHP的初學者,似乎無法找到我做錯了什麼。PhP獲取數據未分配給變量

這裏是我的代碼:

<?php 

// Including all required classes 
require_once('class/BCGFontFile.php'); 
require_once('class/BCGColor.php'); 
require_once('class/BCGDrawing.php'); 

// Including the barcode technology 
require_once('class/BCGcode39.barcode.php'); 

// Loading Font 
$font = new BCGFontFile('./class/font/Arial.ttf', 18); 

// The arguments are R, G, B for color. 
$color_black = new BCGColor(0, 0, 0); 
$color_white = new BCGColor(255, 255, 255); 

//$invoice="36"; 
$drawException = null; 
$invoice = $_GET['invoice']; 
try { 
$code = new BCGcode39(); 
$code->setScale(2); // Resolution 
$code->setThickness(30); // Thickness 
$code->setForegroundColor($color_black); // Color of bars 
$code->setBackgroundColor($color_white); // Color of spaces 
$code->setFont($font); // Font (or 0) 
$code->parse($invoice); // Text 
} catch(Exception $exception) { 
$drawException = $exception; 
} 

的問題是該行22 "$invoice = $_GET['invoice'];"實際上不拉得的數據!如果我取消註釋"//$invoice="36";並註釋掉get部分,代碼將正常工作。

任何想法?

+0

嘗試使用print_r($ _GET)或print_r($ _REQUEST)來查看實際獲得的值。我猜猜這裏有一個錯字。另外,只需查看URL即可輕鬆檢查GET變量。 – 2012-03-28 21:46:23

+0

變量$ _GET在url中? 'test.php的?發票= 45' – tttony 2012-03-28 21:47:24

+0

我可以去Site.com/tmp/barcodegen.1d-php5.v4.1.0/test.html?invoice=33 – NRGdallas 2012-03-28 21:53:15

回答

0

你實際上傳遞發票數據的URL查詢字符串?這就是$ _GET的用途。如果您從表單提交數據,請改用$ _POST。

如果不幫助,嘗試print_r($_GET),並確保數據實際被傳遞你期望的方式,並且在正確的變量名。

+0

不作爲獲得或從表單工作後,這兩種方法導致一個「錯誤:沒有數據輸入」 – NRGdallas 2012-03-28 22:03:52

0

你確定你用GET提交,而不是帖子?嘗試做print_r($_GET);print_r($_POST);並查看「發票」的位置。

+0

我可以去Site.com/tmp/barcodegen.1d-php5.v4.1.0正好驗證網址/test.html?invoice=33 – NRGdallas 2012-03-28 21:49:02

+0

您是否在本地服務器上運行此操作? – 2012-03-28 22:05:14

+0

不,目前託管在一個全功能的網站上,長期以來一直運行完整的php解決方案(zen cart,interspire),沒有任何問題。 – NRGdallas 2012-03-28 22:06:22

0

試圖加入這個到您的代碼

if(isset($_GET['invoice'])){$invoice = $_GET['invoice']; }else{ echo 'No invoice'; exit;} 
+0

沒了,整個代碼停止工作,簡單地顯示alt標籤,它是硬編碼的「條碼」而不是動態圖像 – NRGdallas 2012-03-28 22:04:38

0

的頂部就意味着你不發送invoice爲GET方法,您需要檢查它是否存在。

$invoice = isset($_GET['invoice']) ? $_GET['invoice'] : 'default invoice'; 
/*I dont know what is invoice var and its type , but its always good to validate 
    user input*/ 
if(validInvoice($invoie)) { 
    //doStuff 
}