dongyue7796 2013-05-28 15:27
浏览 42
已采纳

确定传递哪个隐藏的输入值

I built a custom form for search filtering on my site. I'm using hidden input fields to decide what type of search filter was selected. The value gets passed and I check on my search page what the value was, and then direct to the correct search results page like so:

<?php

    $search_refer = $_GET['search_type'];

    if($search_refer == 'members') {
        load_template(TEMPLATEPATH . '/search-members.php');
    } else if($search_refer == 'event') {
        load_template(TEMPLATEPATH . '/search-member-event.php');
    } else {
        load_template(TEMPLATEPATH . '/search-site.php');
    }

?>

Here are the two fields in HTML/PHP

<div class="state"> // Sorting by user State
    <input type="hidden" name="search_type" value="members">
    <select id="stateDrop" name="state">
        <option value="">State</option>
        <?php
            foreach($states as $state) {
                echo '<option value="'.$state.'">'.$state.'</option>';
            }
        ?>
    </select>
</div>
<div class="sort-event"> // Sorting by an Event
    <input type="hidden" name="search_type" value="event">
    <select id="eventDrop" name="event">
        <option value="">Event</option>
        <?php
            $args = array(
                'post_type' => 'events',
                'order' => 'ASC'
            );

            $eQuery = new WP_Query( $args );

            while( $eQuery->have_posts() ) : $eQuery->the_post();

                echo '<option value="'.$eQuery->post->post_title.'">'.$eQuery->post->post_title.'</option>';

            endwhile;

            wp_reset_query();
        ?>  
    </select>
</div>

Both have the name search_type as a hidden name, but the values are different. members vs event. My problem is when you click on the submit button, the value being received is always the last hidden input value. e.g. event.

Is there a way to decide which field was chosen so that I can then direct the search results to the correct page. (the search pages pull in different information based on the search).

Thanks,

  • 写回答

1条回答 默认 最新

  • douhao2856 2013-05-28 15:56
    关注

    As you have two inputs with the same name, only one of them will ever be set to the server.

    If you want them to use the same name you're going to need to use POST and two forms, which would require two separate submit buttons but go to the same page. This way you can submit the desired form based on the filter.

    <form action="yourpage.php" method="post">
          <input type="hidden" name="search_value" value="members"/>
           ... member stuff
          <input type="submit" value="Filter Members"/>
    </form>
    <form action="yourpage.php" method="post">
          <input type="hidden" name="search_value" value="event"/>
           ... event stuff
          <input type="submit" value="Filter Events"/>
    </form>
    

    Or you could use an input type of a radio button, as that supports having the same name as only one value can be selected at once.

    Event: <input type="radio" name="search_type" value="event"/>
    Members: <input type="radio" name="search_type" value="members"/> 
    
    
    
    // Then in PHP $_POST["search_type"] will hold the selected value.
    

    If you wish to stick with GET, you could use JavaScript to set the value of a single hidden element before the user clicks the link.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100