Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use $_POST in wordpress

Tags:

php

wordpress

Hey i am trying to retrive data from a basic form .. but when i am using $_POST['field name'] then it gives me nothing .

here is my basic code

form page is:

<?php
/**

Template Name: galaxy
 */

get_header(); ?>

<div id="main-content" class="main-content">

<form action="<?php echo site_url();?>?page_id=8" method="post">
<input type="text" name="name"  /> <input type="submit" value="Send"  />

</form>

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

<?php
get_footer();

when i click submit it redirects to next page but display nothing with this code

<?php
/**

Template Name: get_value_galaxy
 */


$name=$_POST['name'];
echo $name;

print_r($_POST);

?>
like image 405
Brett Avatar asked Aug 07 '14 15:08

Brett


People also ask

How does $_ POST work?

The $_POST variable collects the data from the HTML form via the POST method. When a user fills the data in a PHP form and submits the data that is sent can be collected with the _POST Method in PHP. The Post Method transfers the information via the Headers.

What does $_ POST do in PHP?

PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables.

How do I fetch POST data in WordPress?

Getting Posts WP get_posts function retrieves an array of posts and accepts a number of parameters such as numberposts, category, include, exclude, etc. The best way of using WordPress get_posts function is to create an array and pass the appropriate parameters to fetch the desired results.


1 Answers

Try using a different name for the variable. I know that Wordpress uses "name" as a public query var, and perhaps that's why it's not working. So rather than using name="name", try this:

Form:

<input type="text" name="unique_name" />

Post Page:

$name=$_POST['unique_name'];
echo $name;

See this list for all query vars: http://codex.wordpress.org/WordPress_Query_Vars#Query_variables

like image 91
Mastrianni Avatar answered Sep 30 '22 20:09

Mastrianni