将表单字段值传递给新的(动态)页面

pass form field value to new (dynamic) page

我正在尝试将值从输入字段传递到其他页面。 不幸的是,这个值没有 retrieving/not 起作用,所以希望有人能帮我解决这个问题。

page.php

<form action="" id="testform" method="post">          
    <select size="1" class="dropdown">
        <?php // dropdown options (pages)
        $Args = array ('cat' => 1);
        $loop = new WP_Query( $Args );
        while($loop->have_posts()) : $loop->the_post(); ?>          
           <option value="<?php echo get_permalink(); ?>">
               <?php the_title(); ?>
           </option>     
        <?php endwhile; ?>
    </select>    
    <input id="year" name="year" type="text" value=""/><!--The value that will be passed to another page -->
    <input class="button" name="submit" type="submit" value="ENTER"/>   
</form>

提交时 - 转到位置(表单操作)

    $(".button").click(function(){   
        var linkadress = $('.dropdown option:selected').val();
        window.location = linkadress;
        return false;
    });

Footer.php

问题: 传递值(字段 id = year)

var yearfield = "<?php echo htmlspecialchars($_POST['year']); ?>";
alert(yearfield); // Currently no value is passed

建立在 wordpress platform 的基础上。该表单在所有页面上都可见。当提交表单时,应该在新页面(选择的下拉位置)上检索 'year' 值。其余代码工作正常。

您必须获取 select 而非 选项 的值。 (此外,最好为 select 设置 ID 并使用 jquery ID selector 而不是 class selector。)

 $(".button").click(function(){   
        var linkadress = $('.dropdown').val();
        window.location = linkadress;
        return false;
    });

是否要根据下拉列表的值更改表单操作, 如果是的话;

$("#testform").submit(function(){   
    var linkadress = $('.dropdown option:selected').val();
    $(this).attr('action', linkadress);
});

您不是在提交(发布)表单,而是在单击按钮时进行重定向。所以 PHP 永远不会收到 $_POST。

要么必须在函数中使用 submit(),要么必须将年份变量添加到 url。

$(".button").click(function(){   
        var linkadress = $('.dropdown').val();
        $("#testform").setAttribute('action', 'linkadress');
        $("#testform").submit();
        //Now you can use $_POST['year'] in linkadress
        return false;
    });

$(".button").click(function(){   
        var linkadress = $('.dropdown option:selected').val();
        var year = $().val('#year'); 
        window.location = linkadress + '?year=' + year;
        //Now you can use $_GET['year'] in linkadress
        return false;
    });