0

i have a form, like this

        <form id="property_type_form" method="post" action="">
            <div data-toggle="buttons" class="category-filters btn-group pull-left">
                <label id="property_type_all" class="btn active hvr-overline-from-center"
                       onclick="location.replace('<?php echo home_url( $wp->request ) ?>')">
                    <?php _e( 'Todas', 'irealtor' ) ?>
                </label>
                <?php foreach ( $property_type as $type ) : ?>
                    <label for="house" id="label_<?php echo $type->slug ?>"
                           class="btn hvr-overline-from-center">
                        <input type="checkbox" id="<?php echo $type->slug ?>" value="<?php echo $type->slug ?>"
                               name="property_type" onchange="this.form.submit()"/>
                        <?php echo $type->name ?>
                    </label>
                <?php endforeach; ?>
            </div>
        </form>

in the same file, i process the request with this code:

if ( isset( $_REQUEST['property_type'] ) && $_REQUEST['property_type'] ) {
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'property_type',
            'field'    => 'slug',
            'terms'    => $_REQUEST['property_type'],
        )
    );
}

I also try any of:

action="<?php echo esc_url( $_SERVER['REQUEST_URI'] ) ?>"
action="<?php echo home_url( $wp->request ) ?>"
action="<?php get_permalink();?>"

when I use this form on any other page it works as I want, but if I use it in the frontpage the form action go to the blog page, and I don't know why. any idea?

6
  • What fields does the form contain? Do the names clash with any WordPress query vars?
    – Milo
    Apr 15, 2018 at 2:03
  • @Milo, I put all the code of my form, but the form works ok, i´m using this part of code into a require_one to include it in other page, and all works fine. the probles is just in the frontpage
    – efirvida
    Apr 15, 2018 at 4:20
  • is property_type a taxonomy? your problem has the symptoms of the main query being altered in some way by the form inputs.
    – Milo
    Apr 15, 2018 at 4:41
  • yes property_type is a taxonomy, i´m going to update my post
    – efirvida
    Apr 15, 2018 at 4:44
  • if the form isn't meant to alter the main query, then rename that field to something unique.
    – Milo
    Apr 15, 2018 at 7:01

1 Answer 1

0

Because, by default, if you don't specify an actual page in the target, it assumes you meant www.example.com/ , which goes to the www.example.com/index.php page, which loads the home page of your site, which is the front page of your site, which is normally the blog page (unless you specify a static page as the 'home' page of your blog). (Simple explanation...)

So, you need to go to the page that processes the form. If it is itself, then you need to use this in the 'action' parameter:

action="<?php get_permalink();?>"

Which will return the link of the current page, which I assume has the processing code for your form.

Correction

You need to use the_permalink(), not get_permalink(). For the reason why, see here.

4
  • thankf for your hint, but didt solve mi issue, I update my question
    – efirvida
    Apr 15, 2018 at 1:50
  • If you change the code to use get_permalink, what do you you see in the generated code in the 'action' parameter of the form tag? All of this assumes that your code is inside the 'loop' of a template, so the get_permalink function uses the post_id value. ... and see the correction to my answer.... Apr 15, 2018 at 2:03
  • non of theese works because the permalink of the frontpage is the home url so its like the index.php
    – efirvida
    Apr 15, 2018 at 2:20
  • A form needs an 'action'. By using '#" (in HTML/PHP-only sites), a 'submit' will load the same page as the form. That page has code that looks for the 'submit' & processes the form. On a WP site, using '#' will not necessarily get you to the same page as the form (unless it is the main/home page). So you need to use the 'action' parameter to specify the page that contains the code that processes the form. If that is the main page, fine; you just need to put code on that main page (in the template that 'buids' the main page) to process the form. But I'd use another page to process the form. Apr 15, 2018 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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