Codeigniter POST Form Submit Problems - php

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

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>

codeigniter data is not getting inserted in database

Im a newbie in CodeIgniter and I just followed a tutorial for CRUD operations within framework but my data is not getting inserted in my database. Here is my code:
Controller: site.php
<?php
class Site extends CI_Controller
{
function index()
{
$this->load->view("home");
}
function create()
{
$data=array('title'=>$this->input->post("title"),
'song'=>$this->input->post("song")
);
$this->site_model->add_record($data);
$this->index();
}
}
View: home.php
<html>
<head>
<title>my page</title>
<style type="text/css">
input,label
{
display:block;
}
</style>
</head>
<body>
<?php echo form_open('site/create'); ?>
<p>
<label for="title">Title:</label>
<input type="text" name="title" id="title" />
</p>
<p>
<label for="content">Song:</label>
<input type="text" name="song" id="song" />
</p>
<p>
<input type="submit" value="submit"/>
</p>
<?php echo form_close(); ?>
</body>
</html>
Model: site_model.php
<?php
class site_model extends CI_Model
{
function getAll()
{
$q=$this->db->get('name');
return $q->result();
}
function add_record($data)
{
$this->db->insert("name",$data);
return;
}
function update_record($data)
{
$this->db->where("id",14);
$this->db->update('name',$data);
}
function delete_row($data)
{
$this->db->where("id",$this->uri->segment(3));
$this->db->delete('name',$data);
}
}
My table name is name and I have 3 fields: id(auto_increment), title, and song. Help would be much appreciated.
Add this line before you calling model function.
$this->load->model('site_model');
after that call your model function
$this->site_model->add_record($data);
I think this will solve your problem
Regards
iijb

Can't post simple form date in codeigniter

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());
}

Switching controllers and sending data in codeigniter

I have to devellop an internal web based application with codeigniter and I need to chain different forms (generate upon data choosen with previous form).
Right now, I tried to use form validation in the same method of the controller but the chaining only validate the first form, I tried also with $_SESSION variables but I have to send a large amount of data between each form. I tried with class variable (in controllers and models) but every time the form is send the variable are initialise...
So i wonder if there is a way to switch from a method to another one in my controller giving the data to the new controller.
my first form:
<p>Filtres: </p>
<br/><br/>
<form action="" method="post" id="form_ajout_manip" >
<label for="thematique[]">Thématique</label><br/>
<select name="thematique[]" size="20" multiple>
<?php
foreach($list_thema->result() as $thema)
{
echo "<option value='".$thema->THEMATIQUE_ID."'>".$thema->PARENT_THEMATIQUE_ID." - ".
$thema->NOM."</option>";
}
?>
</select>
<input type="hidden" value="true"/>
<br/>
<br/>
<br/>
<input type="submit" value="Rechercher" />
</form>
my second form:
<form action="" method="post" id="form_ajout_manip_cdt">
<label for="nom_manip" >Nom manipulation: </label>
<br/>
<input type="text" name="nom_manip"/>
<TABLE border="1">
<CAPTION><?php echo $data->num_rows.' '; ?>resuuultat</CAPTION>
<TR>
<?php
foreach($data->list_fields() as $titre)
{
echo '<TH>'.$titre.'</TH>';
}
?>
</TR>
<?php
foreach($data->result() as $ligne)
{
echo '<TR>';
foreach($ligne as $case)
{
echo '<TD>'.$case.'</TD>';
}
echo '<TD><input type="checkbox" name="cdt[]" value="'.$ligne->ID_CANDIDAT.'"
checked="true"</TD>';
echo '</TR>';
}
?>
</TABLE>
<br/><br/>
<input type="submit" value="créer"/>
</form>
Those are the two method of my controller
public function choix()
{
//controller for the second form
$this->info_page['title']='Ajout manipulation';
$this->load->view('ui_items/header',$this->info_page);
$this->load->view('ui_items/top_menu');
$this->load->view("manipulation/choix",$data);
}
public function filtre()
{
//controller for the first form
$this->form_validation->set_rules('thematique[]','Thematique','');
if($this->form_validation->run())
{
$data['data']=$this->manipulation_mod->select_par_filtre($this->input->post('thematique'));
//need to send $data to the second method "choix()"
}
else
{
$this->info_page['title']='Filtre ajout manipulation';
$this->load->view('ui_items/header',$this->info_page);
$this->load->view('ui_items/top_menu');
$data= array();
$data['list_op']= $this->candidat_mod->list_operateur();
$data['list_thema']= $this->thematique_mod->list_all_thematique();
$data['list_gene']= $this->candidat_mod->list_gene();
$this->load->view('manipulation/filtre', $data);
}
}
Have you any idea? I totally stuck...
Based on your clarification, let me give you an outline on what will work
View
Have both the forms in the same page
<? if(!$filtered): ?>
<input type="hidden" name="filtered" value="true"/>
/* Form 1 content here */
<? else: ?>
<input type="hidden" name="filtered" value="true"/>
/* Form 2 content here */
<? endif; ?>
Controller
You just need to use one controller
public function filter() {
$filtered = $this->input->post('filtered');
$data['filtered'] = $filtered;
if(empty($filtered)) {
/* Form validation rules for Form 1 */
/* Run form validation etc. */
/* Set title etc. for Form 1 */
} else {
/* Form validation rules for Form 2 */
/* Run form validation etc. */
/* Set title etc. for Form 2 */
}
/* Load view */
}
There might just be a better way to do this, but I am sure this will work. Good luck!

View rendering as blank in Zend Framework action

<?php
class SignupController extends Zend_Controller_Action
{
function indexAction()
{
if ($this->_request->isPost())
{
echo "Your email address is: " . $this->_request->getPost('email');
}
$this->render("signup");
}
}
This is my controller
this is my view
<html>
<body>
<form action="/signup" method="post">
Email: <input name="email" type="text" />
<input name="btnSubmit" value="Click me" type="submit" />
</form>
</body>
</html>
when I tried to run the view
I am getting result
blank/signup
why this happens
please rectify that error, and tell me the reason for that error
If you are using signup view then replace your render code from this :-
$this->_helper->viewRenderer('signup');
Try it with else:
if ($this->_request->isPost()) {
echo "Your email address is: " . $this->_request->getPost('email');
} else {
$this->render("signup");
}

Categories