Can't post simple form date in codeigniter - php

This i'm sure is a simple overlook, but its given me quite a headache in a short time period.
Im trying to post data from this view:
<?php
if (!$this->tank_auth->is_logged_in())
{
echo "<a href='index.php/auth/login'>Login</a>";
} else { ?>
<form method="POST" action="create_community">
<label>Community Name: </label><input type="text" name="communityname" value="231"/><br>
<label>Description: </label><br><textarea name="communitydesc"></textarea><br>
<label>Image Location(URL): </label><input type="text" name="imageloc"/><br>
<input type="radio" name="privacy" value="public" /> public<br />
<input type="radio" name="privacy" value="private" /> private<br />
<input type="radio" name="privacy" value="business" /> business<br />
<input type='submit'/>
</form>
<?php }
?>
create controller is the following:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Create extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library('tank_auth');
$this->load->library('encrypt');
$this->load->library('session');
$this->load->model('topbar_model');
$this->load->model('left_model');
$this->load->model('right_model');
/*
$this->load->model('right_model');
$this->load->model('left_model');
$this->load->model('topic_model');
*/
$this->load->helper('url');
$this->load->helper('form_helper');
$this->load->helper('form');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->library('security');
$this->load->library('tank_auth');
$this->lang->load('tank_auth');
}
public function index()
{
$this->load->view('create_community');
}
public function create_community()
{
$this->load->view('templates/head');
echo "testing";
print_r($this->input->post());
}
}
create_community shows "testing" but does not show the post. I've tried posting individual input contents as well, and just get nothing.
Any ideas?

The believe $this->input->get_post() is a method call, not a class variable, so you can't handle like you would $_POST[].
If you are wanting to follow traditional form processing using POST, use the standard $_POST[] assoc. array, but be aware that there might be a CSRF field that will be included, and that you will need to handle.
Also, I don't know how you are routing, but shouldn't your form action be 'create/create_community', unless you are routing past 'create' for all calls at this point.

public function create_community()
{
var_dump($this->input->post());
}

Related

An Error Was Encountered in CI forms

Iam a newbie in Codeigniter , Iam learning it from watching videos , the instructor did the same what I did , But it gives me an error like this "The action you have requested is not allowed." ,and it worked with him, I don't know why, any help ! .
this is my Controller code
public function index(){
if($this->input->post('submit')){
echo $this->input->post('first_name');
}
$this->load->view('forms');
}
this is my View code
<form method="POST">
<input type="text" name="first_name" />
<input type="submit" name=submit" />
</form>
Use form_open() helper which automatically adds hidden input with CSRF value.
So your view should be:
<?php echo form_open('action_url'); ?>
<input type="text" name="first_name" />
<input type="submit" name=submit" />
<?php echo form_close(); ?>
Disabling CSRF protection also works but it's a bad idea.
you almost right, you need to add some parts like action in your form, and isset or empty in your controller like
class Test_form extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
$this->load->view('form_test');
}
//using your example. good
public function check_form(){
if( isset($this->input->post('first_name', TRUE))){
echo "success <br>$$this->input->post('fisrt_name', TRUE)";
}
else{
echo "error";
}
}
//using form_validation. best
public function check_form_validation(){
$this->load->library('form_validation');
$this->form_validation->set_rules('first_name', 'first Name', 'trim|required|xss_clean');
if( ! $this->form_validation->run()){
echo "error <br>" . validation_errors();
}
else{
echo "success <br>$$this->input->post('fisrt_name', TRUE)";
}
}
}
form_test.php
first method
<form method="post" action="<?= base_url()?>index.php/test_form/check_form">
<input type="text" name="first_name">
<input type="submit" value="test">
</form>
<hr>
second method
<form method="post" action="<?= base_url()?>index.php/test_form/check_form_validation">
<input type="text" name="first_name">
<input type="submit" value="test">
</form>

Behaviour of redirect in codeigniter

Code for myform.php
<html>
<head>
<title>My form</title>
</head>
<body>
<form action="success" method="post">
<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<!--
<h5>Password</h5>
<input type="password" name="password" value="" size="50" />
<h5>Confirm Password </h5>
<input type="password" name="passconf" value="" size="50" />-->
<h5>Email Address </h5>
<input type="email" name="email" value="" size="50" />
<div><input type="submit" value="submit" /></div>
</form>
</body>
</html>
controller script 1
<?php
class Form extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->library('session');
$this->load->helper('url');
}
public function index(){
$this->load->view('myform');
}
public function success(){
$_SESSION['username']=$_POST['username'];
$_SESSION['email']=$_POST['email'];
redirect('form/home');
}
public function home(){
$this->load->view('test_home');
}
}
?>
controller script 2
<?php
class Form extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->library('session');
$this->load->helper('url');
}
public function index(){
$this->load->view('myform');
}
public function success(){
$_SESSION['username']=$_POST['username'];
$_SESSION['email']=$_POST['email'];
echo $_SESSION['username'];
echo $_SESSION['email'];
redirect('form/home');
}
public function home(){
$this->load->view('test_home');
}
}
?>
The question is when I use controller 1, the script work as intended and redirects me to form/home. However, when I use controller 2 I get this error
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /usr/local/apache2/htdocs/parth/application/controllers/Form.php:15)
Filename: helpers/url_helper.php
Line Number: 564
Backtrace:
File: /usr/local/apache2/htdocs/parth/application/controllers/Form.php
Line: 18
Function: redirect
File: /usr/local/apache2/htdocs/parth/index.php
Line: 292
Function: require_once
why is the code behaving this way? Thank you for your time.
Its because, you have echoed two strings in second case.
Comment out this:
//echo $_SESSION['username'];
//echo $_SESSION['email'];
Redirection is not happening due to this.
In CodeIgniter, redirection uses PHP's header("Location...") construct.
This requires that your current script is not outputting anything on screen.
Not even a space (that is why CodeIgniter recommends you should not end up your PHP files with ?> as spaces can remain there.
redirect() uses PHPs
header() function. If you have output before any header you get this error.
This is your Output:
echo $_SESSION['username'];
echo $_SESSION['email'];
redirect('form/home');
Commented out, or delete it, cause while redirecting you dont need it.
Kind regards

Form method Post not working

My problem seams to be simple, but I am struggling to get this to work.
Here's my html:
<form class="form" role="form" id="form" method="post" action="<?php echo site_url("register"); ?>" onsubmit="" >
<div class="col-md-6 col-sm-6 col-xs-12 fm">
<input type="text" class="form-control" id="email" name="email"
value="<?php if(isset($validation_errors['post_data']['email'])) echo $validation_errors['post_data']['email']; ?>"
placeholder="Email" >
</div>
<div class="text-center">
<input type="submit" id="btn" name="btn" class="btn" value="Quero ganhar um Cartão">
</div>
</form>
I made the code a bit simple, so you could read it fast.
When I submit the form to the register page, I try to see if it is a post or not like this :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register extends CI_Controller {
function Register() {
parent::__construct();
$this->load->model('register_model');
}
public function index() {
if($_POST) { echo 'post'; } else { echo 'not a post'; }
}
}
And the controller keeps printing 'not a post'.
It seams like the form only redirect to the URL and do nothing more.
Edit :
I did echo $_SERVER['REQUEST_METHOD']; in my controller and it prints " GET ", so, I guess something is wrong with the method ? I Already tried method="POST", method="post" and it keeps printing " GET ". Don't know what else to do.
add url helper in controller, then only you can access site_url() in your view page
$this->load->helper('url');
remove unnecessary onsubmit="" in your form tag
i did manage to get this going.
My redirect link didn't have "www" on it, and despite the redirect, it needed the "www" to do the POST.
Thanks to everyone that tried to help !

can seem to submit form in codeigniter and go to next view?

below is my 'mainview.php' view. from here iam attempting to submit and just open the next view which is called 'carerview.php'.
<form action="<?php echo base_url()?>login" method="post">
<div class="input-prepend">
<span class="add-on"><i class="icon-envelope"></i></span>
<input type="text" id="" name="" placeholder="your#email.com"></br></br>
<div class="input-prepend">
<span class="add-on"><i class="icon-lock"></i></span>
<input type="password" id="" name="" placeholder="Password"></br></br>
<button type="submit" class="btn btn-primary"><i class="icon-user icon-white"></i>Sign in</button>
</div>
</div>
</form>
Iam trying to submit this is giving me issues.The Index page loads which contains the above view. but when i submit . i get requested URL not found on this server
. then if i use the full url action="application/controllers/user/login" i get a forbidden, dont have permission to access it.
my method in my controller class is just to load the next view on submit so i dont think there is an issue there . below is the controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
public function index()
{
if(!$this->isLoggedIn())
{
$this->load->view('mainview');
}
else
{
//do something
}
}
public function login()
{
$this->load->view('carerview');
}
public function isLoggedIn()
{
return false;
}
}
any help would be appreciated thanks.
if you didn't remove index.php from your URL and didn't set anything to base_url in configuration,try this
<?php echo base_url();?>index.php/user/login
localhost/your_app_folder/index.php/controller/action
Your form action is base_url(), which means is the application index route.
Try using form_open() (in the form_helper), which takes care of building the correct url:
<?php echo form_open('user/login');?>
... your form here
<?php echo form_close();?> // since I didn't see a close form tag in your form
Be careful of any routes that might intercept the request.
Alternatively, you could use site_url():
<form method="POST" action="<?php echo site_url('user/login');?>">

Codeigniter POST Form Submit Problems

I just started learning CI and PHP and wanted to make a simple CRUD application.
I created a function to add a record, but when i submit the data in the form, Chrome downloads a file with no extension.
Controller
<?php
class Options extends CI_Controller{
function index()
{
$this->load->view('options_view');
}
function create()
{
$data = array(
'name' => $this->input->post('name'),
'price' => $this->input->post('price'));
$this->data_model->addItem($data);
$this->index();
}
}
Model
<?php
class Data_model extends CI_Model {
function getAll() {
$q = $this->db->query("SELECT * FROM items");
if($q->num_rows() >0){
foreach($q->result() as $row){
$data[]=$row;
}
}
return $data;
}
function addItem($data){
$this->db->insert('items', $data);
return;
}
}
?>
View
<html><head></head><body>
<style type="text/css">
label {display: block;}
</style>
<h2>Create</h2>
<?php echo form_open('options/create'); ?>
<p>
<label for="name">Name</label><input type="text" name="name" id="name" />
</p>
<p>
<label for="name">Price</label><input type="text" name="price" id="price" />
</p>
<p><input type="submit" value="Submit" /></p>
<?php echo form_close(); ?>
</body>
</html>
Is there something that i did wrong?
No errors pop out. The form is created, but when i add data in the textboxes and click submit, the browser dowloands a "Create" file.
Could it be that you don't need the last slash in that line?
<?php echo form_open('options/create/'); ?>
i think you need to load form helper like
$this->load->helper(array('form', 'url'));
please let me know if you face any problem.
Add $this->load->model('data_model'); to method before $this->data_model->addItem($data);

Categories