dongzhong7299 2012-05-08 02:03
浏览 18

如何将变量设置为$ _GET

I don't have a lot of experience with this... Help is greatly appreciated.

$id = $_GET["categoryID"]; isn't setting the variable.

$_GET["categoryID"] is returning the value, but the variable isn't being set.

I can set the variable by using $id=3; however $id = $_GET["categoryID"]; doesn't work.

<?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");
    }
}
?>

Ok, so I stripped everything down as far as I know how. it appears the problem may be related to the ajax request.

Here is the stripped down code using $_GET.... $id=$_GET["categoryID"]. It prints the $_GET["categoryID"] result and the $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> 

And here is the post page sample....

<!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>

And the Post result page...

<!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>

It still isn't setting the $id = $_GET["categoryID"]; Even though is is printing it outside of the request block Thanks again for all your help.

  • 写回答

2条回答 默认 最新

  • duanpo1821 2012-05-08 02:04
    关注

    It looks to me like the problem is that your form is POST, or that your form isn't submitting correctly. Try to echo out $_GET['categoryID'] and see what you get.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度