grab value from form drop down menu and insert into variable
Results 1 to 8 of 8

Thread: grab value from form drop down menu and insert into variable

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    38

    grab value from form drop down menu and insert into variable

    hello, using pagination, which has a variable you change to set the results per page.
    I want the user to be able to change this using a simple drop down menu with some numeric options.

    so far ive got a simple form set up with the various options i want to give. what is the syntax for grabbing the value chosen and storing it as the variable?

    so far i have:

    PHP Code:
     <form action="<? echo($_SERVER['PHP_SELF']);?>" method="post" enctype="multipart/form-data" nme="resultsperpage" id="resultsperpage">
               
     
        Display
        <select name="numresults">
        <option value="1" id="1">1</option>
        <option value="2" id="2">1</option>
        <option value="4" id="4">1</option>
        <option value="8" id="8">1</option>
        </select> results per page
           
    </form>
        

    <?php

    $rpp 
    $_GET['option value']; // results per page
    thanks

  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    38
    ..just realised my mistake in the form, u get the idea ha

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    38
    me again!

    i've managed to get the desired result by including a submit button, and then:
    PHP Code:


    <form action="<? echo($_SERVER['PHP_SELF']);?>" method="post" enctype="multipart/form-data" nme="resultsperpage" id="resultsperpage">
               
     
        Display
        <select name="numresults">
        <option value="1" id="1">1</option>
        <option value="2" id="2">2</option>
        <option value="4" id="4">4</option>
        <option value="8" id="8">8</option>
        </select> results per page
           <input type="submit" name="go" value="go" />
        
    </form>
       
      


    <?php
    if($_POST['go']){
        
    $value $_POST['numresults'];
    }

    else{
        
    $value 1;
    }

    $rpp $value;
    where ($rpp) = results per page

    however, is there a way of achieving the same result without the user needing to select the option and then clicking go? i.e the number of results per page changes as soon as they select the option

    edit: just realised ill need to store their choice within a session variable as well otherwise it reverts to default if you click 'next' or reload the page

    working demo at: http://www.creative-spotlight.com/dy...e/display4.php
    Last edited by rpsep2; 04-11-2012 at 02:08 PM.

  4. #4
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,399
    Quote Originally Posted by rpsep2 View Post
    however, is there a way of achieving the same result without the user needing to select the option and then clicking go? i.e the number of results per page changes as soon as they select the option
    You'd need to use Javascript to either a) automatically submit the form when the value in the <select> element changes, or b) take a more Web 2.0-ish approach using AJAX to update the content of the page without refreshing it.

    Quote Originally Posted by rpsep2 View Post
    edit: just realised ill need to store their choice within a session variable as well otherwise it reverts to default if you click 'next' or reload the page
    Why does it need to be a session variable? Why not just use a regular cookie instead?

  5. #5
    Member
    Join Date
    Apr 2012
    Posts
    38
    Quote Originally Posted by bradgrafelman View Post
    You'd need to use Javascript to either a) automatically submit the form when the value in the <select> element changes, or b) take a more Web 2.0-ish approach using AJAX to update the content of the page without refreshing it.

    Why does it need to be a session variable? Why not just use a regular cookie instead?

    thanks for the info, ill research these things and try to figure it out. if not... ill be back

  6. #6
    Junior Member
    Join Date
    Jul 2009
    Posts
    22
    dont bother researching my friend, here you go

    <select onchange='this.form.submit()' ...>

  7. #7
    Member
    Join Date
    Apr 2012
    Posts
    38
    the form now submits without havign to click'go', thanks!

    but now I cant figure out setting and using the cookie...

    so first i set a cookie above the head tags
    PHP Code:
    <?
    setcookie
    ("results"$value);
    ?>
    and then try to use it later on...
    PHP Code:
    <?
        
    $value 
    $_POST['numresults'];
    if(
    $value>0){
        
    $rpp $_COOKIE['results'];
    }

    else{ 
    $rpp1;
    }

    ?>
    my thinking is I always want atleast 1 result showing, so im trying to set up an if argument for if a cookie has been set or not, if it has then use it, if not then default to 1 result per page.

    but of course it doesn't work! any hints?

  8. #8
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,399
    Quote Originally Posted by rpsep2 View Post
    so first i set a cookie above the head tags
    PHP Code:
    <?
    setcookie
    ("results"$value);
    ?>
    How did you define $value? Also, the "above the head tags" part doesn't really tell us much - what matters is that you placed the call to setcookie() before any output is sent. Did you?

    Also, do you have display_errors set to On and error_reporting set to E_ALL (or better)?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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