Send variable from one page to another in wordpress template

I am trying to pass date from one wordpress template form to another. To make it simple, I have created two templates (and associated them with WP pages as a standard page) as follows:

<?php
/*
Template Name: Form test
*/
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(); ?>

and

Read More
<?php
/*
Template Name: Form result
*/
get_header(); 
$name = $_REQUEST['name'];
?>

And the name is <?=$name?>

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

When I click on the text link Result page I get to the /form-result/ page, however, if I submit the form, I get a page not found sort of error. After verifying, I found that the problem is coming from trying to pass the variable from one page to another.

Pusing further, I found that <a href="/form-result/?name=Elodie">Result page</a> get an error page whereas <a href="/form-result/">Result page</a> takes me properly to the next page.

Probably I am missing something small.

Any help here ?

Related posts

Leave a Reply

3 comments

  1. 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.

  2. <?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(); ?>
    
  3. 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