Support » Fixing WordPress » Get variable from dropdown list selection

  • Resolved harman79

    (@harman79)


    Hi all,

    My PHP knowledge is very limited and I have been trying to do this for a couple of days now so I decided that I surely need some help..

    So I have a basic form listing all the categories in a dropdown list (code below). What I would like to do is this: When the user selects a category from the list and clicks submit, I want to store a variable for the selected category (ID would be fine) and then pass it to the database. I know how to insert into the db, what I dont know is how to store the category variable…

    If($_POST['Submit']) {
    // This is where my db insert code goes...
    ?>
    <?php
    }
    else // else we didn't submit the form, so display the form
    {
    ?>
    <form action="" method="post">
    <select>
    <option value=""><?php echo esc_attr(__('Select a Category')); ?></option>
    <?php
        $categories = get_categories('orderby=name&name=categorySearch');
        foreach ($categories as $category) {
        $option = '<option value="/category/'.$category->category_nicename.'">';
        $option .= $category->cat_name;
        $option .= '</option>';
        echo $option;
        }
    ?>
    </select>
    <input type="submit" name="Submit" id="addcoursesubmit" value="Submit" />
    </form>
    <?php
    } // end else no post['submit']
    ?>

    Any help would be greatly appreciated!
    Harry

Viewing 1 replies (of 1 total)
  • Thread Starter harman79

    (@harman79)

    OK,

    funny how you really feel the pressure of being called a lazy b once you posted something in the forum.. After 24 hours of searching I finally found it!! And its so easy I am about to cry!!

    Anyways, for all the co-newbies out there having similar struggles. There goes:

    If($_POST['Submit']) {
    $drop = $_POST['dropdown'];
    echo $drop;
    ?>
    <?php
    }
    else // else we didn't submit the form, so display the form
    {
    ?>
    <form action="" method="post">
    <select name="dropdown" >
    <option value=""><?php echo esc_attr(__('Select a Category')); ?></option>
    <?php
        $categories = get_categories('orderby=name&name=categorySearch');
        foreach ($categories as $category) {
        $option = '<option value="'.$category->term_id.'">';
        $option .= $category->cat_name;
        $option .= '</option>';
        echo $option;
        }
    ?>
    </select>
    <input type="submit" name="Submit" id="addcoursesubmit" value="Submit" />
    </form>
    <?php
    } // end else no post['submit']
    ?>
Viewing 1 replies (of 1 total)
  • The topic ‘Get variable from dropdown list selection’ is closed to new replies.