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
Related
This is my view/login/loginform.php
<?php echo form_open('scheduler/login');
?>
<label for="username">username</label>
<input type="text" name="username"/><br/>
<label for="password">password</label>
<input type="password" name="password"></input><br/>
<input type="submit" name="submit" value="log in" />
</form>
This is my controllers/Scheduler.php
<?php
class Scheduler extends CI_Controller {
public function login()
{
$this->load->helper('form');
echo"not logged in";
$username=$this->input->post("username");
$password=$this->input->post("password");
if($username=="123" && $password="abc"){
$data['username']=$username;
$this->load->view('login/login_success_message',$data);
//$this->input->set_cookie('123','abc','15');
// redirect('home');
}
else {
$this->load->view('login/login_failure_message');
//$this->load->view('login/loginform');
}
}
}
?>
However, there is no form shown when I go the http://localhost/project/scheduler/login
//$this->load->view('login/loginform');
You are not loading the login form. So, Uncomment the above line
$this->load->view('login/loginform');
if the problem is still same then try to call $this->load->view('login/loginform');out side if else statement
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>
i am tring to set cookie in codeigniter but i cannt make it happen i dont know what is wrong.
there is no error on the error log page...
this is my view page...
<!DOCTYPE html>
<html>
<head>
<title>
EKART
</title>
</head>
<body>
<h1>SIGN IN</h1>
<form id="admin" action="/do/ekart/adminlogin/login/" method="POST">
Name :<br/>
<input type="text" name="name" id="name"/><div id="name_display"></div><br/>
Password :<br/>
<input type="password" name="password" id="pasword"/><div id="password_display"></div><br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
and here is my controller..
adminlogin.php
<?php
class adminlogin extends Controller{
public function __construct(){
parent::Controller();
$this->load->library('mylib');
}
public function index(){
$this->load->view("ekart/adminlogin");
}
public function login(){
$name=$_POST["name"];
$password=$_POST["password"];
$data=array("username"=>$name,"password"=>$password);
$result=$this->mylib->logincheck($data);
if($result){
echo "every thing is fine";
setcookie("myCookie",$name,time()+3600,"/");
$this->load->view("ekart/home",array("message"=>""));
}
}
}
the control is going inside if and it also prints "every thing is fine" the home page is also get loaded but i dont know why the cooke is still not set..
mylib is my library to check login validation....
There is a codeigniter way of doing this.
Make sure to have $this->load->helper('cookie'); in your controller, or autoload it.
$cookie = array(
"username" => $name,
"time" => time()+3600
);
$this->input->set_cookie($cookie);
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);
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());
}