Form action redirect to the same page

I’m making my own theme. In my theme I have a page that display custom post using a custom query.
I added a form to this page. The form contain 2 comboboxes that contain values of meta data of my custom post. I want that when the user select values from this comboboxes and click the submit, WordPress will show again the same page and will add the submit parameter to my custom query (something like filtering a table).

The problem is that when clicking on the submit, WordPress falling down to the index.php instead to my page.

Read More

My form declaration is:

<form action='"<?php echo get_page_link(SHIURIM_PAGE)?> "' method='get'>";

Should I put something else in the action?

Thank is advanced.

My full page code:

<?php
/*
Template Name: Shiurim page
*/

require 'search-form.php';
?>

<?php get_header(); 

global $THEME_PATH;
global $IMAGES_PATH;

?>

<link rel="stylesheet" type="text/css" href="<?php _e($THEME_PATH)?>/shiurim.css">

<div id="page-header-pic">
    <img src='<?php _e($IMAGES_PATH)?>/contact-header.jpg' alt="header picture" />
</div>

<div class="page-title">
    <?php wp_title(); ?>
</div>

<div class="areaBg">
    <div class="page-content-full">
        <div class="panel">
            <div class="panel-title"></div>
            <div class="panel-content">
                <div id="pageContentId">



<?php 
    function getParam($paramName, $defaultValue) {
        if ($_SERVER['REQUEST_METHOD'] === 'GET') {
            if (!empty($_GET[$paramName])) {
                return $_GET[$paramName];
            }
        }
        else {  // it's a post
            if (!empty($_POST[$paramName])) {
                return $_POST[$paramName];
            }
        }
        return $defaultValue;
    }

    $pageIndex = getParam('paged', 0);
    $searchRav = getParam(SEARCH_RAV, 0);
    $searchSidra = getParam(SEARCH_SIDRA, 0);
    $searchText = getParam(SEARCH_TEXT, "");

    $searchText = trim($searchText);

    $queryArgs = array (
        'post_type' => array( POST_TYPE_SHIUR ),
        'orderby' => 'title',
        'order'    => 'ASC',
        'paged' => $pageIndex
    );

    $paginateSearchArgs = array();
    $metaQuery = array();
    if ($searchRav != 0) {
        $metaQuery[] = array(
            'key' => POST_TYPE_RAV,
            'value' => $searchRav
        );

        $paginateSearchArgs[SEARCH_RAV] = $searchRav;
    }

    if ($searchSidra != 0) {
        $metaQuery[] = array(
            'key' => POST_TYPE_SIDRA,
            'value' => $searchSidra
        );

        $paginateSearchArgs[SEARCH_SIDRA] = $searchSidra;
    }

    if (count($metaQuery) > 0) {
        $queryArgs['meta_query'] = $metaQuery;
    }

    if (! empty($searchText)) {
        $queryArgs['s'] = $searchText;

        $paginateSearchArgs[SEARCH_TEXT] = $searchText;
    }

?>

<div id="shiurimSearchPanel">
    <?php renderSearchFrom($searchRav, $searchSidra, $searchText); ?>
</div>
<div>
<?php 
    //query_posts($queryArgs);
    $shiurim_query = new WP_Query( $queryArgs );
    global $wp_query;
    // Put default query object in a temp variable
    $tmp_query = $wp_query;
    // Now wipe it out completely
    $wp_query = null;
    // Re-populate the global with our custom query
    $wp_query = $shiurim_query;

    $big = 999999999; // need an unlikely integer

    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $wp_query->max_num_pages,
        'prev_text' => '&lt;&lt;',
        'next_text' => '&gt;&gt;'
    ) );
?>
</div>

<?php
    $is_first = true;
    if ( $shiurim_query->have_posts() ) : while ( $shiurim_query->have_posts() ) : $shiurim_query->the_post();

?>
            <?php if (!$is_first) { ?>
                    <div class='shiurSeperator'></div>
            <?php } ?>

            <?php
                $is_first = false;

                get_template_part( "content", "shiur-list" );
            ?> 
<?php endwhile; else: ?>
<p><?php _e('Nothing is found'); ?></p>

<?php 

endif;
// Restore original query object
$wp_query = $tmp_query;
// Be kind; rewind
wp_reset_postdata();

 ?>
                </div>
            </div>
        </div>
    </div>

    <div class="clear"></div>
</div>

<?php get_footer(); ?>

search-form.php (I’m using it in the index.php as well).

<?php

/**
 *  Search panel
 */

function renderSearchFrom($searchRav = array(), $searchSidra = array(), $searchText = "") {
    $rabanimList = getPostArrayResult(POST_TYPE_RAV);
    $sdatorList = getPostArrayResult(POST_TYPE_SIDRA);  

    $html = "<div id='search-panel'>";
    $html .= "  <form action='" . get_page_link(SHIURIM_PAGE). "' method='post'>";
    $html .= "      By rav";
    $html .= "      <select name='search_rav'>";
    $html .= "          <option value='0'>All ravs</option>";

    foreach ($rabanimList as $rav) {
        $isSelected = ' ';
        if ($rav['id'] == $searchRav) {
            $isSelected = ' selected ';
        }

        $html .= "<option value='" . $rav['id'] . "'" . $isSelected . ">" . $rav['title'] . "</option>";

    } 

    $html .= "</select>";

    $html .= "      By sidra";
    $html .= "      <select name='search_sidra'>";
    $html .= "          <option value='0'>All sidras</option>";

    foreach ($sdatorList as $sidra) {
        $isSelected = ' ';
        if ($sidra['id'] == $searchSidra) {
            $isSelected = ' selected ';
        }

        $html .= "<option value='" . $sidra['id'] . "'" . $isSelected . ">" . $sidra['title'] . "</option>";
    }

    $html .= "</select>";

    $html .= "      Free text";
    $html .= "      <input type='text' name='search_text' class='searchFreeText' value='" . $searchText . "'/>";

    $html .= "      <input type='submit' value='Search' />";
    $html .= "  </form>";
    $html .= "</div>";

    echo $html;
}

?>

Related posts

Leave a Reply

4 comments

  1. You can use this way, it´s shorter and works fine:

    <form action="<?php echo get_bloginfo('home').'/?page_id='.get_the_ID();  ? >" method="get" >
    
  2. I found the problem and the solution.

    The problem:

    Using:

    <form action="<?php echo get_page_link(66) ?>"  method="get"> 
    

    is no good. In the generated HTML I can see:

    <form action="http://mySite/?page_id=66" method="get">
    

    But when submitting the form, the ?page_id=66 is disappear. So WordPress don’t know to redirect the request to my page template.

    The solution:

    The action will point to the home page and add hidden field that will hold the page_id.

    <form action="<?php echo home_url ?>"  method="get">
        <input type='hidden' name='page_id' value='66'>
    

    Good luck.