我正在尝试将值从输入字段传递到其他页面。 不幸的是,该值无法检索/不起作用,因此希望有人可以帮助我。
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;
});
页脚.php
问题:传递值(字段 id = 年)
var yearfield = "<?php echo htmlspecialchars($_POST['year']); ?>";
alert(yearfield); // Currently no value is passed
在 wordpress 平台上构建
。该表格在所有页面上都可见。提交表单后,应在新页面(所选的下拉位置)上检索 'year'
值。其余代码工作正常。
最佳答案
您不是在提交(发布)表单,而是在单击按钮时进行重定向。所以 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;
});
关于php - 将表单字段值传递到新(动态)页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29431331/