ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Category Dropdown

February 25, 2011 4:25pm

Subscribe [2]
  • #1 / Feb 25, 2011 4:25pm

    Gareth Redfern

    86 posts

    In the EE docs there is a method of displaying a dropdown list of categories but this method uses Javascript.

    <form name="catmenu" action="">
    <select name="selcat">
    <option value="">--Select Category--</option>
    {exp:channel:categories channel="yourchannel" style="linear"}
    <option value="{path='channel'}">{category_name}</option>
    {/exp:channel:categories}
    </select>
    </form>

    Can this be done without using Javascript? If so how, I have tried different paths but i can’t get it to display the single category page.

  • #2 / Feb 25, 2011 4:54pm

    Mark Bowen

    12637 posts

    I could well be wrong on this and hopefully there is an easier way than this but I think if you are going to go without using javascript then you will need to utilise the form action=”“ parameter to send to an intermediary script called something like redirect.php which will take the value submitted and then supply a header redirect to take the person where they need to go.

    Any reason why you don’t want to use Javascript though?

    Also hopefully someone else will pop in here in case there is an easier way of doing this.

    Best wishes,

    Mark

  • #3 / Feb 25, 2011 4:57pm

    Mark Bowen

    12637 posts

    Posted too soon again!

    Try this article here.

    Kind of like what I was saying in a round about sort of way.

    Best wishes,

    Mark

  • #4 / Feb 25, 2011 5:04pm

    Gareth Redfern

    86 posts

    Hi Mark, thanks for the help, doesn’t sound too simple. The main reason for not wanting to use Javascript is from a usability standpoint. If Javascript is disabled the form will not work. I suppose I could add an if statement that just displays a list of the categories if Javascript is disabled, that may be easier.

    Regards,

    Gareth

  • #5 / Feb 25, 2011 5:17pm

    Mark Bowen

    12637 posts

    Hi Mark, thanks for the help, doesn’t sound too simple.

    By the looks of it, it’s not really all that difficult. Perhaps give it a go and see how you get on. You get the best of both worlds using that method too.

    Best wishes,

    Mark

  • #6 / Feb 25, 2011 5:23pm

    Gareth Redfern

    86 posts

    Thanks for the help Mark, I’ll give it a go. Looks like I will need to enable php in my templates which isn’t something I have had to do before, this shouldn’t cause any issues though?

  • #7 / Feb 25, 2011 5:23pm

    Mark Bowen

    12637 posts

    As a matter of fact I just tried this out really quickly and had it working in under 5 minutes. One thing you’ll need to add in though is an exit; after the header redirect in the php script and enable php on the template you’re doing this on but other than that it’s pretty easy to do.

    Let me know if you get stuck and I can write down the code here if you like.

    Best wishes,

    Mark

  • #8 / Feb 25, 2011 5:24pm

    Mark Bowen

    12637 posts

    this shouldn’t cause any issues though?

    Nope shouldn’t be as long as only you have access to the template. Also before this goes live you’d probably want to add in some sanitisation of the POST value just in case someone messes with the form values.

    I’m not totally certain if ExpressionEngine does this anyway so that might be a moot case. Hopefully someone can come in and clear that one up for us though.

    Best wishes,

    Mark

  • #9 / Feb 25, 2011 5:28pm

    Gareth Redfern

    86 posts

    I am not too familiar with php what do you mean by an exit; if you could post an example of the code used that would be really helpful and appreciated.

  • #10 / Feb 25, 2011 6:08pm

    Mark Bowen

    12637 posts

    I am not too familiar with php what do you mean by an exit; if you could post an example of the code used that would be really helpful and appreciated.

    No problems. Here you go :

    <?php
            if (isset($_POST['nav'])) {
                     header("Location: $_POST[nav]");
                     exit;
            }
    ?>
    
    <form id="page-changer" action="" method="POST">
        <select name="nav">
    <option value="">--Select Category--</option>
    {exp:channel:categories weblog="products" style="linear"}
    <option value="{path='products/listings'}">{category_name}</option>
    {/exp:channel:categories}
        </select>
        <input type="submit" value="Go" id="submit" />
    </form>

    You’ll need PHP turned on for the template but other than that it should hopefully work. Let me know how you get on.

    Best wishes,

    Mark

  • #11 / Feb 26, 2011 2:26am

    Gareth Redfern

    86 posts

    Thanks Mark worked a treat, really appreciate all your help.

  • #12 / Feb 26, 2011 5:39am

    Mark Bowen

    12637 posts

    Thanks Mark worked a treat, really appreciate all your help.

    No problem. Glad that helped a bit.

    Best wishes,

    Mark

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases