Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a submission form where users can submit posts on my site. I have three steps (3 form pages) and in the first one user selects the post category, clicks the submit button and moves on to the next page which loads a form assigned to that category.

Now, is there a way to have the categories as links (buttons) so that when user clicks on one, the form is submitted and he moves on to the next step without needing to click the actual submit button?

This is how the submit button looks:

<input type="submit" name="getcat" id="getcat" class="btn_catstep" value="<?php _e('Continue'); ?>" />

And this is how the form per category is handled:

<?php
            $Category = get_term_by('id',$_POST['cat'],'p_cat');
            $_POST['catname'] = $Category->name;
        ?>

            <form name="mainform" id="mainform" class="form_step" action="" method="post" enctype="multipart/form-data">

                <ol>

                    <li>
                        <label><?php _e('Category');?>:</label>
                        <strong><?php echo $_POST['catname']; ?></strong>&nbsp;&nbsp;<small><a href=""><?php _e('(change)') ?></a></small>
                    </li>

                    <?php echo show_form($_POST['cat']); ?>

                    <p class="btn">
                        <input type="submit" name="step1" id="step1" class="btn_next" value="<?php _e('Continue'); ?>" />
                    </p>

                </ol>
                    <input type="hidden" id="cat" name="cat" value="<?php echo $_POST['cat']; ?>" />
                    <input type="hidden" id="catname" name="catname" value="<?php echo $_POST['catname']; ?>" />
                    <input type="hidden" id="fid" name="fid" value="<?php if(isset($_POST['fid'])) echo $_POST['fid']; ?>" />
                    <input type="hidden" id="oid" name="oid" value="<?php echo $order_id; ?>" />
            </form>

I currently have the categories in a basic dropdown with the submit button below it. If I have to hard code the links one by one, no problem, just don't know how to go about with this.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Once you retrieved your categories, let's say in $categories, you can do something like this:

<?php
foreach ($categories as $catgory) {
    ?>
    <form id="cat-button-form-<?php echo $category->ID; ?>" action="<?php echo $url_to_step_2; ?>" method="POST">
        <input type="hidden" name="mycat" value="<?php echo $category->ID; ?>" />
        <input type="submit" name="getcat" id="getcat_<?php echo $category->ID; ?>" class="btn_catstep" value="<?php printf(__('Select %s &amp; Continue'), $category->name); ?>" />
    </form>
    <?php
}
?>

In step 2 you then read $_POST['mycat'] and go from there.

share|improve this answer
    
Great, thank you very much. One thing though: how could I hard code this? If I have different buttons for each category and need to style them very differently. I'm not too deep in these dynamic functions. –  Andrew Mar 1 '11 at 19:36
    
See this in the code above: id="getcat_<?php echo $category->ID; ?>"...i also added id="cat-button-form-<?php echo $category->ID; ?>" to the form tag. So you can now use CSS to style each of those as you like individually. If you really want to hardcode it, you just copy the whole <form></form> section for each button and replace teh $category->ID echos with the actual category id in each. I'd recomment sticking with the dynamic way and getting used to it, though. ;-) –  wyrfel Mar 1 '11 at 19:43
    
Doh! You got it covered from the beginning. Thank you once again. I will be using the dynamic approach from now on. You're a star! –  Andrew Mar 1 '11 at 22:32
    
Last thing, I promise :) I though to make the submitting a bit more straightforward and have the category selection and the category based submission form on the same page so I can take one step away from the process. I put <?php if(!isset($_POST['cat'])) { ?> on the page so I'll have the category selection first and <?php } else { $Category = get_term_by('id',$_POST['cat'],'p_cat'); $_POST['catname'] = $Category->name; ?> with the form selection process coming after it. But when I click the category button (submit) it just loads the page again, not the right form. What gives? –  Andrew Mar 2 '11 at 9:18
    
You need to check for $_POST['mycat'], not 'cat'. I called the parameter 'mycat' because that prevents it from being interpreted as a query parameter to limit posts by category. –  wyrfel Mar 3 '11 at 7:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.