php if with url variables problem

Posted on 2005-04-08
Last Modified: 2010-04-17
Here's my code:

if (isset($HTTP_GET_VARS['SubID'])) {
  $query_rsGetWallpapers = "SELECT * FROM wallpaper_files WHERE CatID='".$HTTP_GET_VARS['CatID']."' AND SubID='".$HTTP_GET_VARS['SubID']."'";
} else if (($_GET['SubID'] = '')){
  $query_rsGetWallpapers = "SELECT * FROM wallpaper_files WHERE CatID='".$HTTP_GET_VARS['CatID']."'";
} else {
  $query_rsGetWallpapers = "SELECT * FROM wallpaper_files WHERE CatID='".$HTTP_GET_VARS['CatID']."'";

The page displays a list of available wallpapers by category and sub category.

If a main category is clicked on, the url string will be test.php?CatID=somenumber

If a sub category is clicked on, the url string will be test.php?CatID=somenumber&SubID=somenumber

This fine at first when you are clicking on the main categories; however, once you click on a sub category and then try to go back to a main category, a blank (&SubID=) is stuck in the url string in which the script then looks for a SubID match and therefore comes up with a blank set of records for the main categories.

The text links are actually form submit buttons, so, I cannot get rid of that SubID once it gets in the URL string.  Here's some more of the code so that you can see how it works:

Javascript in the head:
<script language="JavaScript" type="text/javascript">
function wallswitch ( selectedtype )
  document.wallpapers.CatID.value = selectedtype ;
  document.wallpapers.submit() ;
function subswitch ( selectedtype )
  document.wallpapers.SubID.value = selectedtype ;
  document.wallpapers.submit() ;

then the links:

<form name="wallpapers" method="GET" action="">
<input type="hidden" name="CatID" />
                      <td id="leftcolumncontent"><div id="cat"><ul>
                          <li><a href="javascript:wallswitch('1');">Abstract Textures</a></li>
                          <li><a href="javascript:wallswitch('2')">Transportation</a>
<?php if ( ($_GET["CatID"] == '2') ) { ?>
<input type="hidden" name="SubID" />
<ul id="subcat">
<li><a href="javascript:wallswitch('2');javascript:subswitch('11')">Aviation</a></li>
<li><a href="javascript:wallswitch('2');javascript:subswitch('12')">Classic Cars</a></li>
<li><a href="javascript:wallswitch('2');javascript:subswitch('13')">Hot Rods &amp; Dragsters</a></li>
<li><a href="javascript:wallswitch('2');javascript:subswitch('14')">Racing</a></li>
<?php } ?></li>

The reason why I have made the text links act as for submit is because once I have this working properly I am going to have them POST to the page rather than GET in order to hide the URL variables.

Please let me know if I was unclear or if you have any questions and of course if you have a better way to do this than the round-about I am trying to do it...  Thanks!
Question by:toddynho
    LVL 4

    Accepted Solution

    if (isset($HTTP_GET_VARS['SubID']) AND ($HTTP_GET_VARS['SubID'] != '')) {
      $query_rsGetWallpapers = "SELECT * FROM wallpaper_files WHERE CatID='".$HTTP_GET_VARS['CatID']."' AND SubID='".$HTTP_GET_VARS['SubID']."'";
    } else {
      $query_rsGetWallpapers = "SELECT * FROM wallpaper_files WHERE CatID='".$HTTP_GET_VARS['CatID']."'";

    LVL 4

    Expert Comment

    You cannot remove it from the url part because you defined it as an hidden field and this should not be worse.

    Author Comment

    thanks, that worked just fine!

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
    Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now