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 6 of 6
  1. #1
    New Coder
    Join Date
    Aug 2002
    Location
    New York
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Passing Hidden Variables

    Hello, I need help in passing hidden variables. I'd like to know how to correctly incorporate

    PHP Code:
    <input type="hidden" name="my_var" value="<?php echo $my_var ?>">
    (my_var being any variable that it should be called to make this thing work) into my php script...

    PHP Code:
    <!--begin order--> 

    <form name="order"> 

    <font face="verdana" size="2">Sort By </font> 

       <select size="1" name="category" style="font-family: Verdana; font-size: 8pt; border-style: solid; border-width: 1; padding-left: 4; padding-right: 4; padding-top: 1; padding-bottom: 1" onChange="location=document.order.category.options[document.order.category.selectedIndex].value;" value="GO"> 

       <option>---------------</option> 
            <option value="?id=results2&search=<?=$_POST['search']?>&order=date&how=desc">Date (DESC)</option> 
       <option value="?id=results2&search=<?=$_POST['search']?>&order=date&how=asc">Date (ASC)</option> 
            <option value="?id=results2&search=<?=$_POST['search']?>&order=name&how=asc">Title (ASC)</option> 
       <option value="?id=results2&search=<?=$_POST['search']?>&order=name&how=desc">Title (DESC)</option> 
            <option value="?id=results2&search=<?=$_POST['search']?>&order=category&how=asc">Category (ASC)</option> 
       <option value="?id=results2&search=<?=$_POST['search']?>&order=category&how=desc">Category (DESC)</option> 

            </select> 

    </form> 

    <!--end order--> 

       </td> 
       <td align="left" width="408"> 

    <!--begin search--> 

    <FORM METHOD="post" ACTION="http://localhost/george/articles/index.php"> 

    <font size="2" face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    <input type="submit" value="Title Search" name="Title Search" style="font-size: 7pt; font-family: Verdana; border-style: solid; border-width: 1">&nbsp; </font> 

    <input type="text" name="search" size="30" style="font-family: Verdana; font-size: 8pt; border-style: solid; border-width: 1"> 

    </form> 

    <!--end search-->
    So that my $search variable will continue to be passed on from page to page. This way my users can easily sort their search results without losing the $search variable. I'm really having trouble trying to pass my users search query from page to page... please help!

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Hidden input form elements work the same as any other form element. It will get passed to php file that get's called in the action of the form. In your case my_var will get passed as $my_var.

    Or did I misunderstand your question?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    New Coder
    Join Date
    Aug 2002
    Location
    New York
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I believe you misunderstood my question. The HTML I wrote above enables the user browsing the site to search and sort the search listings once (it works just one time). But if the user tried sorting the listings again the browser kinds of "forgets" the previous query and instead of sorting out the search results, sorts out everything in the database. I'm trying to find a way to keep the original query throughout the users sorts so that they can sort their search results more than once.

    If it confuses you just minus what I was asking about the hidden input variable. Basically I'm just trying to rewrite or add something to the second script so that the browser doesn't keep "forgetting" the original search query, in this case it would be the $search variable.

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The browser does not forget anything, it's only there to display the result that your PHP script running on the server generated.

    I'm afraid your question confuses me a little, but I might help you anyway. From your HTML it looks like you are passing a query string to the same file itself. That's okay so far, only that your query string is constructed with a variable deriving from the $_POST array initially.

    PHP Code:
    <option value="?id=results2&search=<?=$_POST['search']?>&order=date&how=desc">Date (DESC)</option>
    When you submit this query string to the file, the "search" variable is no longer in $_POST, but rather in $_GET, since you only called the page by it's URL, not through a form submission.

    So you need to check whether "search" is available in the $_POST or $_GET array and apply the correct value in the line above

    PHP Code:
    $searchParameters = ( isset($_POST['search']) ) ? $_POST['search'] : $_GET['search']; 
    and then you have in your option tags

    PHP Code:
    <option value="?id=results2&search=<?=$searchParameters?>&order=date&how=desc">Date (DESC)</option>
    Try that, if it doesn't work, you'll have to post more info about your search routine so we can check where the code chokes.

  • #5
    New Coder
    Join Date
    Aug 2002
    Location
    New York
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It worked perfectly just like you said! Thanks a lot mordred!
    Last edited by gmitra; 08-04-2002 at 06:17 PM.

  • #6
    New Coder
    Join Date
    Jul 2002
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    I came to this post to see the solutions here might fit my problem, but, I do have the GET and when the user tries to order the results, the varibles aro no longer there !?

    May anyone give alook at my session code to help on this bug please?

    Thanks in advance.

    The post link:
    http://codingforums.com/showthread.php?threadid=3121


  •  

    Posting Permissions

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