如何将变量设置为$ _GET

时间:2012-05-08 02:03:14

标签: php

我对此没有太多经验......非常感谢帮助。

$ 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”];即使是在请求块之外打印它再次感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

在我看来,问题是您的表单是POST,或者您的表单没有正确提交。尝试回应$_GET['categoryID'],看看你得到了什么。

答案 1 :(得分:0)

检查以确保在尝试使用它初始化$ id之前设置了$_GET["categoryID"]

  isset($_GET["categoryID")
如果$_GET["categoryID"]包含值,

将返回true。