Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Page and Form Not Playing Well Together

    This one's got me scratching my head. I've got a page which displays inventory which has worked fine for months. I'm in the process of adding a drop-down to allow visitors to view a subset of the inventory. The problem is once I've added the form, the original query doesn't execute upon display. I've tinkered with a couple of things, but am just baffled. Any insights will be appreciated.

    Form Section:

    Code:
    <FORM NAME="sort" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
    <SELECT NAME="earcode" 
    <OPTION VALUE=""></option>
    <OPTION VALUE="ALL" selected="selected">Display all Earrings </option>
    <OPTION VALUE="E">Display only Earwire Earrings</option>
    <OPTION VALUE="P">Display only Post Earrings</option>
    <OPTION VALUE="L">Display only Lever back Earrings</option>
    </SELECT>
    <input type="submit" value="Display Selected Earrings">
    </FORM>
    A block that doesn't seem to make a difference:

    Code:
    if (isset($_REQUEST['submitted']) && $_REQUEST['submitted'] == '1') {
        echo "Form submitted!";
      }
    Here's the standard query:

    Code:
    $query = "SELECT code, id,name,price,description
    
    	   	 FROM dbtable WHERE status = 'A'
    		 AND code = 'E'"; 
    
    $result = mysql_query($query) or die('Query not successfully processed: ' . mysql_error());
    Here's the logic block from the form. If I comment this out, the page query executes just fine.

    Code:
    // select dbquery
     if ($earcode =="ALL")
      $queryb = "SELECT code, id,name,price,description
    	   	 FROM dbtable WHERE status = 'A'
    		 AND code = 'E'"; 
    else
      $queryb = "SELECT code,id,name,price,description
    	   	 FROM dbtable WHERE status = 'A'
    		 AND cat_code = 'E'
    		 AND ear_code = '$earcode'" ; 
    		 
    $result = mysql_query($queryb) or die('Query not successfully processed: ' . mysql_error());

    Here's another block of code, pretty standard stuff:


    Code:
    $cols = mysql_num_fields( $result );
    // get each row
    while($row = mysql_fetch_row($result))  {
    What am I missing here? Do I need completely separate paths? Are they tripping over shared variables? Do I need another branch on my IF?

    Thanks in advance.

  • #2
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    Code:
    if (isset($_REQUEST['submitted']) && $_REQUEST['submitted'] == '1') {
        echo "Form submitted!";
      }
    This is never going to be true. You have no element on your form named "submitted", therefore $_REQUEST['submitted'] will never be set.

    Add the part in red:
    Code:
    <FORM NAME="sort" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
    <SELECT NAME="earcode" 
    <OPTION VALUE=""></option>
    <OPTION VALUE="ALL" selected="selected">Display all Earrings </option>
    <OPTION VALUE="E">Display only Earwire Earrings</option>
    <OPTION VALUE="P">Display only Post Earrings</option>
    <OPTION VALUE="L">Display only Lever back Earrings</option>
    </SELECT>
    <input type="submit" value="Display Selected Earrings" name="submitted">
    </FORM>
    It's a little distracting trying to read your code broken up this way, besides it's difficult to know if some other part of the code is causing a problem. However, I suspect that you are relying on register_globals being on, but your server has them set to off (as they should be).

    Assuming that is the case you'll something like this:
    PHP Code:
    $earcode $_POST['earcode']; 
    Could be other problems, but without seeing all the code ... ??

    Edit: Just noticed ...

    Code:
    if (isset($_REQUEST['submitted']) && $_REQUEST['submitted'] == '1') {
        echo "Form submitted!";
      }
    should be
    Code:
    if (isset($_REQUEST['submitted'])) {
        echo "Form submitted!";
      }
    Where was $_REQUEST['submitted'] == '1' being set?
    Last edited by PappaJohn; 08-26-2007 at 10:42 AM. Reason: more info

  • #3
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    your missing > from your first select tag too.

  • #4
    New to the CF scene
    Join Date
    May 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've taken a different approach with the switch function, but can't get the drop down selections to trigger the alternate queries. It always does the default query regardless of which option is selected. What am I missing here?

    Here's what I've done:

    I had to do some reading on the switch function. I've incorporated it and it seems to ignore all but the default query. In other words, both the link to the page and clicking the submit button after the making a selection in the drop down menu generate the same results. Thanks in advance for any code suggestions.



    Code:
    <?php
    
    <FORM NAME="sort" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
    <SELECT NAME="earcode" 
    <OPTION VALUE=""></option>
    <OPTION VALUE="ALL" selected="selected">Display all Earrings </option>
    <OPTION VALUE="E">Display only Earwire Earrings</option>
    <OPTION VALUE="P">Display only Post Earrings</option>
    <OPTION VALUE="L">Display only Lever back Earrings</option>
    </SELECT>
    <input type="submit" value="Display Selected Earrings" name="ear">
    </FORM>
    
    
    if (isset($_POST['ear'])) {
        switch ($_POST['earcode']) {
          case 'All':
            if ($earcode == "ALL")
    { 
      $query = "SELECT cat_code,id,name,price,description
    	   	 FROM dbtable WHERE status = 'A'
    		 AND cat_code = 'E' 
    		 order by pid DESC ";
    }		  
    	  		
            break;
          case 'Type':
             if ($earcode == 'E' || $earcode == 'P' || $earcode == 'L')
    		 
    { 	  
     $query = "SELECT cat_code,id,name,price,description
    	   	 FROM dbtable WHERE status = 'A'
    		 AND cat_code = 'E'
    		 AND ear_code = '$earcode'
    		 order by pid DESC " ; 
    }		        
            break;   
          default:
            $query = "SELECT cat_code,id,name,price,description
    	   	 FROM dbtable WHERE status = 'A'
    		 AND cat_code = 'E' 
    		 order by pid DESC ";
        }
      } else {
        $query = "SELECT cat_code,id,name,price,description
    	   	 FROM dbtable WHERE status = 'A'
    		 AND cat_code = 'E' 
    		 order by pid DESC " ;
      }

  • #5
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    You have the form code before the logic to determine the proper query. So what's happening is, php is processing the form which resets the value of "earcode" to "All" and then proceeds to process the logic. So, yes, you will always get the same results.

    You'll have to move the processing code above the form code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •