0

I am adding a form to my wordpress with a custom php page template

Below is my wordpress page template php code:

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

<?php get_header(); ?>

<div id="content">

    <h1 class="page-title"><?php the_title(); ?></h1>

    <div class="entry">

        Welcome <?php echo $_POST["fname"]; ?>!<br>
You are <?php echo $_POST["age"]; ?> years old.

    </div> <!--end .entry-->

</div> <!--end #content-->

<?php get_sidebar(); ?>

<?php get_footer(); ?>

And I am including the following code from my page which shows the form

<form action="http://127.0.0.1:4001/wordpress/script" method="post">
Name: <input type="text" name="fname">
Age: <input type="text" name="age">
<input type="submit">
</form>

This will show the form data in a new page instead of showing the result on the same page. How can I show the result on same page?

I would like to have something like this:

enter image description here

3
  • just leave blank the form action Commented Jul 24, 2013 at 17:15
  • <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">. Right way to do it. Commented Jul 24, 2013 at 17:20
  • @ Murtaza Hussain: no go Commented Jul 24, 2013 at 17:37

4 Answers 4

1

Remove the action attribute from your form:

<form method="post">

UPDATE

Now, to keep data on your fields after form submit, just print them on its respective fields:

Name: <input type="text" name="fname" value="<?php if (isset($_POST["fname"])) echo $_POST["fname"]; ?>" />
Age: <input type="text" name="age" value="<?php if (isset($_POST["age"])) echo $_POST["age"]; ?>" />
Sign up to request clarification or add additional context in comments.

7 Comments

Tried this, but no luck
@user1128648 try action="<?php echo $_SERVER["REQUEST_URI"]; ?>"
@user1128648 what happens? did your form is submited to another page or what? Tell us the behaviour of your tries.
@ DontVoteMeDown after submitting the form, it just clearing the filled form data and showing blank form again
@user1128648 why don't you said before? That is the espected behaviour, to show the values on the fields you need to do more things, look my edit.
|
1
<form method="post" action="">

this is how it should be

1 Comment

it just clearing the form data, not retrieving the data from form
0

If you want to have the result of the form on the same page you have to leave your action empty. So it is considered as the same page. Your php-file should then contain the validation.

Comments

0

use bloginfo('url') to retrieve your site url.

<form method='post' action="<?php bloginfo('url') ?>/wordpress/script">

1 Comment

If you are fetching the form from wordpress admin the bloginfo('url') will not work. I don't see the code to fetch data from the admin in your template file. <?php while(have_posts()):the_post();?> <?php the_content();?> <?php endwhile;?>.I tried the script on my machine (including the line i just added) and it is working great! You can use empty action in form tag!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.