Send variable from one page to another in wordpress template

混江龙づ霸主 提交于 2019-12-04 11:01:40

The issue is solved. It was that, the variable "name" that I was using for this test is a WP keyword and stands for template name. In the absence of template name it looks for other pages where the beginning of url slugs matches the variable name.

I changed the variable name and it worked as it should.

<?php
/*
Template Name: Form
*/
?>

<?php get_header(); ?>    

<form action="/form-result/" method="post">
  <input type="text" name="name" size="20" />
  <input type="submit" value="Go" />
</form>

<a href="/form-result/">Result page</a>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Modify your result page.

<?php
/*
Template Name: Form Result
*/
?>

<?php get_header(); ?>    

<?php if(isset($_POST['name'])) {
  echo $_POST['name'];
} ?>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Try adding the link to the results page inside the form like so:

<button type="submit" name="submitted" id="submitted" class="submit">Submit</button>

and on the results page:

if(isset($_POST['submitted'])) {
$name = $_POST['name'];

}

To pass form data you need to post the form via submit. You can do this outside using jquery like this:

$el.click($('#form_name').submit());

-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!