简体   繁体   中英

How do I set a variable to $_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.

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.

Check to make sure that $_GET["categoryID"] is set before you try to initialize $id with it:

  isset($_GET["categoryID")

will return true if $_GET["categoryID"] contains a value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM