Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top