2012-05-08 76 views
1

我沒有太多的經驗......幫助是非常感謝。

$ id = $ _GET [「categoryID」];不設置變量。

$ _GET [「categoryID」]正在返回值,但該變量未被設置。

我可以通過使用$ id = 3來設置變量;然而$ id = $ _GET [「categoryID」];不起作用。

<?php 
if (@$_REQUEST['ajax']) { 
     $link = $nm3; 
    if ($link == false) 
     trigger_error('Connect failed - ' . mysql_error(), E_USER_ERROR); 
    $connected = mysql_select_db('dgp', $link); 

    if ($connected) { 


     $id = $_GET["categoryID"]; 


     $results = mysql_query('select * from selectMenu where categoryID= \'' . $id . '\' AND category="' . strtolower(mysql_real_escape_string(strip_tags($_REQUEST['category']))) . '"'); 

     $json = array(); 
     while (is_resource($results) && $row = mysql_fetch_object($results)) { 
      //$json[] = '{"id" : "' . $row->id . '", "label" : "' . $row->label . '"}'; 
      $json[] = '"' . $row->label . '"'; 
     } 
     echo '[' . implode(',', $json) . ']'; 
     die(); // filthy exit, but does fine for our example. 
    } else { 
     user_error("Failed to select the database"); 
    } 
} 
?> 

好了,我剝都記錄下來,據我知道怎麼辦。看來問題可能與ajax請求有關。

這是使用$ _GET .... $ id = $ _ GET [「categoryID」]的精簡代碼。它打印$ _GET [「categoryID」]結果和$ id。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Test $_GET</title> 
</head> 
<body> 
<?php 
     if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { 
     $id = $_GET["categoryID"]; 
     } 
?> 
    print_r($_GET) = <?php print_r($_GET); ?> 
    <br /> 
    print_r($id) = <?php print_r($id); ?> 
</body> 
</html> 

這裏是帖子頁樣本....

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>testPOST</title> 
</head> 
<body> 
<form action="testPOST.php" method="post"> 
    categoryID: <input type="text" name="categoryID" /><br /> 
    <input type="submit" value="Submit" /> 
</form> 
</body> 
</html> 

和後結果頁面...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Test $_POST</title> 
    </head> 
    <body> 
    <?php 
      if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { 
      $id = $_POST["categoryID"]; 
      } 
    ?> 
     print_r($_POST) = <?php print_r($_POST); ?> 
     <br /> 
     print_r($id) = <?php print_r($id); ?> 
    </body> 
    </html> 

它仍然是不設置$ id = $ _GET [「categoryID」];即使是在請求塊之外打印它再次感謝您的幫助。

+1

你是什麼意思*「$ _ GET [」categoryID「]正在返回值,但該變量沒有被設置。」*?你如何驗證這個?實際上需要多少代碼來重現問題? –

+1

jQuery在哪裏發揮作用? – Imp

+0

如果我使用$ id = 2,則查詢成功。如果我使用$ id = $ _ GET [「categoryID」],即使print_r($ _ GET);查詢失敗。返回數組([categoryID] => 2) – John

回答

1

它在我看來像問題是你的表單是POST,或者你的表單沒有正確提交。試着迴應出$_GET['categoryID'],看看你得到了什麼。

+0

我肯定會支持$ _POST請求,因爲它是通過AJAX提交的。 $ _GET是可能的,但$ _POST是更常見的方法 – Bryan

+0

如果我使用$ id = 2,則查詢成功。如果我使用$ id = $ _ GET [「categoryID」],即使print_r($ _ GET);查詢失敗。返回Array([categoryID] => 2) – John

+0

在上面的代碼中顯示他正在使用'POST'方法? – itachi

0

檢查並嘗試之前初始化的$ id與它確保$_GET["categoryID"]設置:如果$_GET["categoryID"]包含值

isset($_GET["categoryID") 

將返回true。

+0

如果我使用$ id = 2,則查詢成功。 如果我使用$ id = $ _ GET [「categoryID」],即使print_r($ _ GET);返回Array([categoryID] => 2) – John

+0

我改變了$ id = $ _GET [「categoryID」];到$ id = $ _POST [「categoryID」]; 在這個頁面的底部,我放置了,打印$ _POST [「categoryID」] = <?php print $ _POST [「categoryID」]; ?> <?php print $ _POST [「categoryID」]; ?>返回我在引薦頁面上輸入的任何數字。 – John