how to use $_POST in wordpress - php

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);
?>

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

Related

Wordpress custom form post to send datas to another page

Hello I'm creating two pages in Wordpress: dragdrop-translation.php and translation-detail.php using custom template. In the first one the user select a couple of languages, a dead line and the file (.txt or .pdf) that must be traslate. This datas should be send to traslation-detail but I've have a problem with the POST action. The template are placed in wp-content/themes/hello-theme-child-master/dragdrop-translation.php and wp-content/themes/hello-theme-child-master/translation-detail.php
The code of the page that contain the form POST is:
<?php
/**
* Template Name: Drag&Drop - Traslation
*
*/
get_header();?>
<div style="margin-top:120px;">
<!--FIRST PART OF FORM-->
<form method="POST" enctype="multipart/form-data" action="/translation-detail/">
<?php function show_languages_list(){
global $wpdb;
$table_name = "wp_languages_price";
$prepared_query = $wpdb->prepare("SELECT * FROM ".$table_name."");
$results = $wpdb->get_results($prepared_query );
echo '<label for="'.'languages'.'">Select language:</label><select name="'.'languages'.'" id="'.'languages'.'">';
echo '<option value="" disabled selected>Select languages</option>';
foreach($results as $languageCouple){
echo '<option>'.$languageCouple->languages_1.' - '.$languageCouple->languages_2.'</option>';
}
echo '</select>';
}
show_languages_list();?>
<label for="start">Deadline:</label>
<input type="date" id="date" name="trip-start"
min="" max="" placeholder="yyyy-mm-dd">
<br>
<label>Insert text file</label>
<input type="file" name="datafile" id="datafile" size="40" required>
<br>
<input type="submit" value="Calcola preventivo" onclick="checkFields()">
</form>
<br>
<div id="errors" style="color: red;">
</div>
</div>
<script>
//Script for check the datas removed
</script>
<?php get_footer();?>
The code of translation-detail.php is empty so I don't post it.
When I submit the datas, the server replies with a 404 on the action folder of the post.. What path i need to place on the action field of the form?

Submit form does not work correctly

<div class="container">
<form class="form-signin">
<h2 class="form-signin-heading">Please sign in</h2>
<?php echo form_open('login/validate_credentials');?>
<?php $u = 'placeholder="Username"';
$p = 'placeholder="Password"';?>
<?php echo form_input('username','',$u,'class="input-block-level"');?>
<?php echo form_password('password','',$p,'class="input-block-level"');?>
<label class="checkbox">
<input type="checkbox" value="remember-me"> Remember me
</label>
<?php echo form_submit('submit','Sign in','class= "btn btn-primary"');?>
<?php echo anchor('login/signup','Sign up!', 'class= "btn btn-primary"');?>.<br/><br />
<?php echo anchor('login/admin_log','Go to admin login page');?>
<?php echo form_close();?>
</form>
</div>
I have a login form. When I click sign , it's not redirecting me to the form_open page.
you need to create an action for the form, usually a php script on a different page to handle the data:
as an example:
<form action= "../create_comment.php" method="post" name="comments_form" id="comment" enctype="multipart/form-data">
<div>
<label>Name<span>*</span></label>
<input name="name" type="text" value=" ">
</div>
</form>
You have two forms Parent and child(form inside form).
Submitting the form will process parent form. Simply remove the first (parent) <form> tag.
<form class="form-signin">
^^^^^^^^^^^^^^^^^^^^^^^^^^ ------ remove this
...
...
</form>
^^^^^^^ ------ and this
It looks like you might be using CodeIgniter?
You have 2 form tags in your code.
here: <form class="form-signin">...</form>
and here:
<?php echo form_open('login/validate_credentials');?>...<?php echo form_close();?>
Get rid of this one: <form class="form-signin">...</form>
Your second form tag will handle everything for you. The output will look something like this:
<form method="post" accept-charset="utf-8" action="http:/example.com/index.php/login/validate_credentials" />
If you need to add a class or any other property to the form tag, do this:
$attributes = array('class' => 'email', 'id' => 'myform');
echo form_open('email/send', $attributes);
Form helper on CI Docs

How to call a value from function set_value in Code Igniter?

I have a simple task , and i'm using a MVC methods and CI framework for this task.
I have made a view to input data to database, and it's work, and i make a 2 anchors, those are an [Update] and [Delete], the function delete is working, but the update isn't working.
After user click anchor [Update], it will linked to another view(update_view), and i want to show the content which i've clicked in the (update_view). and i think it use a set_value to set a second parameter, to show a value in my update_view.
This is my code in a view(update_view)
<!-- update_view.php -->
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>Update</h2>
<?php echo form_open('site/update'); ?>
<p>
<label>Judul : </label>
<input type="text" name="judul" id="judul" value="<?php echo set_value('judul','??')?>"/>
</p>
<p>
<label>Konten : </label>
<input type="text" name="konten" id="konten" size="100px" value="<?php echo set_value('konten','??')?>"/>
</p>
<p><input type="submit" value="Ubah" /></p>
<?php echo form_close(); ?>
</body>
</html>
What should i put in input tag in value attributes, i want to show a value in input fields (judul, konten) from page before where i clicked the anchors.
I still can't show a image for a view before click, because i'm still don't have 10 rep to share images. so i'will show the coding where the view(options_view) i've clicked the anchors.
This below is the code in a view(options_view) :
<!-- options_view.php -->
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>Create</h2>
<?php echo form_open('site/create'); ?>
<p>
<label>Judul : </label>
<input type="text" name="judul" id="judul" />
</p>
<p>
<label>Konten : </label>
<input type="text" name="konten" id="konten" size="100px"/>
</p>
<p><input type="submit" value="Simpan" /></p>
<?php echo form_close(); ?>
<hr/>
<h2>Read</h2>
<?php if(isset($records)): foreach($records as $baris) : ?>
<h3><?php echo $baris->judul ?></h3>
<div><?php echo $baris->konten ?></div>
<?php echo anchor("site/view_update/$baris->id","[Update]"); ?>
<?php echo anchor("site/delete/$baris->id","[Delete]"); ?>
<?php endforeach; ?>
<?php else : ?>
<h3>Tidak ada data.</h3>
<?php endif; ?>
</body>
</html>
i'm still doubt, whether i should add code in my controller or my view(set_value).
So anyone can help me to solve this problem.
Thank's for help
set_value is codeigniter form_helper function. That helps u to print previous input value when form_validation fails.
In your case if u want to show your data u need to pass data to the view in controller.
E.G:
Controller:
$data["me"] = $this->model->getData($id);
$this->load->view("update_view",$data);
View:
<input type="text" name="judul" id="judul" value="<?php echo $me->judul;?>"/>

Form is not getting displayed in codeigniter.

I am new to code igniter. I've created a form but it is not display properly.
When I put
<?php echo form_open('sms'); ?> instead of <form action="">tag
here in my form and controller, I can't understand why it is not displayed.
<?php echo form_open('sms'); ?>
<p>
<label><strong>Username</strong>
<input type="text" name="textfield" class="inputText" id="textfield" />
</label>
</p>
<p>
<label><strong>Password</strong>
<input type="password" name="textfield2" class="inputText" id="textfield2" />
</label>
</p>
<input type="submit" value="Authentification" name="auth" />
<label>
<input type="checkbox" name="checkbox" id="checkbox" />
Remember me</label>
</form>
and my controller is
<?php
class sms extends CI_Controller{
function school(){
$this->load->view('school/index.php');
if($this->input->post('auth',TRUE)){
$this->load->view('school/dashboard.php');
}
else{
$this->load->view('school/index.php');
}
}
}
?>
If this is your entire script for the most part, it looks like you need to load the helper first from the CodeIgniter Form Helper Page.
If you don't have this line, try adding it before the form_open() function:
<?php $this->load->helper('form'); ?>
While I have used CodeIgniter, it's been a while. Let me know if that changes the result.
Edit: Since you've chosen my answer I'll include this one, credits go out to devo:
You could change </form> to: <?php echo form_close(); ?>. There are pros and cons for this method though, and without using arguments you might be better off sticking with </form>.
I'll explain further:
<div class="registration">
<div class="form-box">
<?php $this->load->helper( 'form' ); ?>
<?php $end = '</div></div>'; ?>
<?php echo form_open( 'register' ); ?>
<!-- Form Inputs Here -->
<?php echo form_close( $end ); ?>
<!-- Echos '</form></div></div>' -->
So for closing the form without arguments, the </form> tag works best, both by performance and simplicity. The example used above is a rather simplistic view of what you can do with it, since what I wrote is not very efficient either.
However, this is still php we're talking about, so perhaps the craftier among us could put it to better use.
End Edit
Have you loaded the form helper? You can use $this->load->helper('form'); in your controller action, her inside function school(). You can then use form helper in the view pages.
Load form helper,
$this->load->helper('form');
And use,
echo form_close()
Instead of,
</form>
First in your Controller put in:
$this->load->helper('form');
And Change </form> to :
<?php echo form_close(); ?>

PHP sessions problems

I'm using sessions to save what ever the user types in the form and what ever they do type will be displayed on the other pages.
It was working perfectly fine but after all the server uploads and such my code has completely done one on me and i'm lost.
Can somebody see if they can spot a mistake? I need fresh eyes.
HTML.
<div id="form"><!--Form Start-->
<form action="home.php" method="post">
<p>Enter Name <input type="text" id="full_name" name="fullName" class="name_input"/>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
</div><!--Form end-->
PHP.
<?php
session_start(); // declaring the variable
if(isset($_POST['fullName'])){ //setting the variable
if(empty($_POST['fullName'])){ //if empty dont to nothing but my wep page will reload
}else{ //if they have do this
$_SESSION['fullName'] = $_POST['fullName']; //get the session for the name (From the from)
header("Location: home.php"); //then will direct the user to the home page (will also display name on each page)
}}
?>
Session on other pages
<div id="echo"> <!-- div ECHO start -->
<?php
echo $_SESSION['fullName']
?>
</div> <!--div ECHO end -->
$_SESSION['fullName'] = $_POST['fullName'];
session_register(fullName);
replace with this code try it
You'll need to add session_start() on whatever page you are redirecting to that is supposed to display the data.
Also, (I'm assuming you realize) what you posted doesn't have anything that would output the data, like:
<input type="text" name="fullName" value="<?php echo $_SESSION['fullName']; ?>"/>
You need to start session on other page as well and stop the script from setting that session. After header location you need to use exit here.
<?php session_start();?>
<div id="echo"> <!-- div ECHO start -->
<?php
echo $_SESSION['fullName'];
?>
you need use exit after header location :-
header('location: home.php');
exit;
Just change the div id form to other because it has a default and remove the empty function because you add isset functon.
Use this.
<div id="myform"><!--Form Start-->
<form action="home.php" method="post">
<p>Enter Name <input type="text" id="full_name" name="fullName" class="name_input"/>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
</div><!--Form end-->
PHP.
<?php
session_start();
if(isset($_POST['fullName']))
{
$_SESSION['fullName'] = $_POST['fullName']; //get the session for the name (From the from)
header("Location: home.php");
exit();
}
?>
Session on other pages.
<div id="echo"> <!-- div ECHO start -->
<?php
session_start();
print_r($_SESSION);
echo $_SESSION['fullName'];
?>
</div> <!--div ECHO end -->
May be it helpful to you.If any problem then let me know.
You are "posting" the values to home.php, doing that you can't set $_SESSION['fullName'] = $_POST['fullName'] in the origin.
Change
<form action="home.php" method="post">
to
<form action="name_of_the_first_script.php" method="post">
$_POST['fullName'] does not exist before the redirect.
Here is how everything should look like (lest call the page index.php):
<div id="form"><!--Form Start-->
<form action="index.php" method="post">
<p>Enter Name <input type="text" id="full_name" name="fullName" class="name_input"/>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
</div><!--Form end-->
now after you hit submit the index.php will be reactioned and at this time with the $_POST request meaning that that the condition
if(isset($_POST['fullName'])){
will be true and the PHP code can be executed, setting the $_SESSION variable and redirecting you to home.php where you ca now read the $_SESSION previously set in index.php
Hope this can me more clear now! :)

Categories