codeigniter form not submitting data - php

I have tried everything I can think of but whenever I click submit the form passes on a null value, I dont know if it is the problem with the form or the controller or even the view. I changed this->input->post to posted data and i get an error of undefined variable posted data, please help.
Controller:
public function addmenu(){
$this->load->model('organizer_model');
$data = array(
'menu_name' => $this->input->post('menu name'),
'price' => $this->input->post('price'),
'email' => $this->session->userdata('email')
);
if($this->organizer_model->insertmenu($data)) {
$this->session->set_flashdata('message', 'Your menu has been added');
redirect('/menu/index', 'refresh');
} else {
$this->session->set_flashdata('message', 'Your menu was not added, please try again');
redirect('/menu/index', 'refresh');
}
View:
<form action="<?php echo site_url('Organizer/addmenu'); ?>" method="post" class="form-horizontal no-margin">
<div class="control-group">
<label class="control-label" for="menuname">
Menu Name
</label>
<div class="controls controls-row">
<input class="span3" name="data[menuname]" type="text" placeholder="Enter menu Name">
</div>
</div>
<div class="control-group">
<label class="control-label" for="price">
Price
</label>
<div class="controls controls-row">
<input class="span3" name="data[price]" type="text" placeholder="">
</div>
</div>
<div class="form-actions no-margin">
<button type="submit" name="submit" class="btn btn-info pull-right">
Add menu
</button>
<div class="clearfix">
</div>
</div>
</form>
Model:
public function insertmenu($data) {
$condition = "email = '" . $data['email'] . "'";
$this->db->select('organizer_id');
$this->db->from('organizer');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() > 0){
array_pop($data); //will remove email from data
$row = $query->row();
$data['organizer_id'] = $row->organizer_id;
$this->db->insert('menu', $data);
if ($this->db->affected_rows() > 0) {
return true;
} else {
return false;
}
} else {
return false;
}
}

I notice same question here codeigniter- insert data into db not working
Checks
Make sure you load your form helper and url helper.
Make sure you use form validation when submitting form in codeigniter on controller.
From this php user guide here http://php.net/manual/en/reserved.variables.post.php
Example on your input would be like person[0][first_name]
<form action="" method="">
<input type="text" name="data_posts[0][menu_name]" placeholder="Enter menu Name">
<input type="text" name="data_posts[0][price]" placeholder="">
</form>
Model
<?php
class Model_something extends CI_Model {
public function add_menu() {
$data_posts = $this->input->post('data_posts');
foreach ($data_posts as $data_post) {
$data = array(
'email' => $this->session->userdata('email'),
'menu_name' => $data_post['menu_name'],
'price' => $data_post['price']
);
$this->db->insert('tablename', $data);
}
}
}
Controller
<?php
class Add_menu extends CI_Controller {
public function index() {
$this->load->helper('form');
$this->load->helper('url');
$this->load->library('form_validation');
$data_posts = $this->input->post('data_posts');
foreach ($data_posts as $data_post) {
$this->form_validation->set_rules('data_posts['.$data_post.'][menu_name]', 'Menu Name', 'required');
$this->form_validation->set_rules('data_posts['.$data_post.'][price]', 'Price', 'required');
}
if ($this->form_validation->run() == FALSE) {
$this->load->view('some_view');
} else {
$this->load->model('model_something');
$this->model_something->add_menu();
redirect('to_success_page');
}
}
}
You could also check if has been inserted by using callback function
Codeigniter 3 user guide form validation http://www.codeigniter.com/user_guide/libraries/form_validation.html
Codeigniter 2 user guide form validation http://www.codeigniter.com/userguide2/libraries/form_validation.html
Also you should upgrade to the new bootstrap I see your using old version.

Related

When saving, how to fix "set" method to update an entry. after add form validation on controllers

after add form validation in my controllers
public function save($id_kecamatan='')
{
$this->form_validation->set_rules('nama_kecamatan','nama_kecamatan', 'required');
if ($this->form_validation->run() != FALSE){
$data = [
'nama_kecamatan' => $this->input->post('nama_kecamatan')
];
}
$simpan = $this->KecamatanModel->saveKecamatan($data);
redirect('admin/kecamatan/add');
}
this is my model, on the line $this->db->insert
public function saveKecamatan($data)
{
$this->db->insert('tbl_kecamatan', $data);
$id_kecamatan = $this->db->insert_id();
return true;
}
my view, when submit, appear You must use the "set" method to update an entry.
<?= validation_errors(); ?>
<form action="<?= site_url('admin/kecamatan/save');?>" method="POST">
<div class="box-body">
<div class="form-group">
<label class="control-label">Kecamatan</label>
<input type="text" class="form-control" name="nama_kecamatan" id="" placeholder="Nama Kecamatan">
</div>
<div class="form-actions">
<input type="hidden" name="id_kecamatan" value="">
<button type="submit" class="btn btn-success"><i class="fa fa-check"></i> Simpan</button>
<i class="fa fa-undo"></i> Batal
</div>
</form>
The validation is useless because it will not prevent empty value to be passed to
$this->db->insert('tbl_kecamatan', $data).
Try to prevent empty value by moving $simpan variable one line up :
public function save($id_kecamatan='')
{
$this->form_validation->set_rules('nama_kecamatan','nama_kecamatan', 'required');
if ($this->form_validation->run() != FALSE){
$data = [
'nama_kecamatan' => $this->input->post('nama_kecamatan')
];
$simpan = $this->KecamatanModel->saveKecamatan($data);
}
redirect('admin/kecamatan/add');
}

Edit page show blank in laravel

I have news details inserted and i need to show it on the edit page but when i try to edit and delete it shows blanks page insert and show is working properly. i have been stucked on this from morning. id is getting from the database but it shows a blank page,Not using any Form helper
1.what's problem,is it on route file
2.is it on Controller file
route.php
Route::get('/', function () {
return view('welcome');
});
Route::resource('books','BookController');
Route::resource('news','NewsController');
Auth::routes();
Route::get('/news','NewsController#index')->name('news');
//Route::get('/news/create','NewsController#create');
//Route::get('/news/edit','NewsController#edit');
Edit.blade.php
#extends('theme.default')
#section('content')
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
NEW BEE NEWS DETAILS
</div>
<div class="panel-body">
<div class="row">
<div class="col-sm-12">
<form method="post" action="{{route('news.update',[$news->id])}}"
enctype="multipart/form-data">
{{csrf_field()}}
<input type="hidden" name="_method" value="put">
<div class="form-group">
<label>NEWS TITLE</label>
<input type="text" name="atitle" id="atitle" class="form-control"
placeholder="PLEASE ADD TITLE OF NEWS" value="{{$news->name}}">
<p class="help-block">Example: SELFY PLAYSHARE </p>
</div>
<div class="form-group">
<label>NEWS</label>
<textarea name="news" id="news" class="form-control" {{$news->news}}></textarea>
<p class="help-block">DETAILED NEWS HERE</p>
</div>
<div class="form-group">
<label>NEWS LINK</label>
<input type="text" name="alink" id="alink" class="form-control"
placeholder="PLEASE ADD LINK OF NEWS" value="{{$news->alink}}">
<p class="help-block">Example: https://play.google.com/store/apps/selfyplusure</p>
</div>
<div class="form-group">
<label>NEWS IMAGE</label>
<input type="file" name="addimage" id="addimage" value="{{$news->imagename}}">
</div>
<button type="submit" class="btn btn-default">ADD NEWS</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
NewsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use App\News;
use Illuminate\Http\Request;
class NewsController extends Controller
{
public function index()
{
$news = News::all();
return view('news.index', ['news' => $news]);
}
public function create()
{
return view('news.create');
}
public function store(Request $request)
{
$news=new News();
if($request->hasFile('addimage')){
$request->file('addimage');
$imagename=$request->addimage->store('public\newsimage');
$news->name = $request->input('atitle');
$news->alink = $request->input('alink');
$news->news = $request->input('news');
$news->imagename = $imagename;
$news->save();
if($news) {
return $this->index();
} }
else{
return back()->withInput()->with('error', 'Error Creating News ');
}
}
public function show(News $news)
{
//
}
public function edit(News $news)
{
$news=News::findOrFail($news->id);
return view('news.edit',['News'=>$news]);
}
public function update(Request $request, $id)
{
$news = News::findOrFail($id);
// update status as 1
$news->status = '1';
$news->save();
if ($news) {
// insert datas as new records
$newss = new News();
//On left field name in DB and on right field name in Form/view
$newss->name = $request->input('atitle');
$newss->alink = $request->input('alink');
$newss->news = $request->input('news');
$newss->imagename = $request->input('addimage');
$newss->save();
if ($newss) {
return $this->index();
}
}
}
public function destroy($id)
{
$news = News::findOrFail($id);
$news->status = '-1';
$news->save();
if ($news) {
return $this->index();
}
else{
return $this->index();
}
}
}
Link to delete and Edit
<td><input type="button" name="edit" value="EDIT">
<td><input type="button" name="delete" value="DELETE"></td>
This is the link to edit
<td><input type="button" name="edit" value="EDIT">
For delete please go through
Delete
In your controller try to use this.
return view('news.edit',compact('news'));

Insert into DB with codeigniter 3

im trying to add new post into my db. I have Model, Controller and View created. Actualy im using rest api for this, but now I want to do it with pure php powerd.
But After form validation is nothing. So when I try to post, nothuing happens.
Here is my code.
Model:
// Create
function create($data) {
// Insert data into DB
$this->db->insert('blog', $data);
return $this->db->insert_id();
}
Controller:
public function add() {
if ($this->ion_auth->is_admin()) {
// Validation rules
$this->form_validation->set_rules('title', 'Titel', 'required');
$this->form_validation->set_rules('teaser', 'Teaser', 'required');
$this->form_validation->set_rules('full', 'Volltext', 'required');
if (($this->form_validation->run() == FALSE)) {
$this->load->view('templates/backend/header', $this->data);
$this->load->view('pages/backend/blog/add', $this->data);
$this->load->view('templates/backend/footer');
} else {
if($this->input->post()) {
$data = array(
'title' => $this->input->post('title'),
'teaser' => $this->input->post('teaser'),
'full' => $this->input->post('full')
);
$this->blog_model->create($data);
redirect(base_url().'blog/');
}
}
} else {
redirect('login');
}
}
And at least my view:
<div class="uk-margin-top">
<?php $attributes = array("class" => "uk-panel uk-panel-box uk-form uk-margin-lage-bottom", "id" => "add-form", "method" => "post");
echo form_open("/backend/blog/add", $attributes); ?>
<div class="uk-form-row">
<label class="uk-form-label" for="title">Title</label>
<input id="title" class="uk-width-1-1 uk-form-large title redactor-box" name="title" placeholder="Beitragstitel" type="text"
value="<?php echo set_value('title'); ?>"/>
<span class="uk-text-danger"><?php echo form_error('title'); ?></span>
</div>
<div class="uk-form-row">
<label class="uk-form-label" for="teaser">Teaser</label>
<textarea id="teaser" class="uk-width-1-1 uk-form-large teaser redactor-box" name="teaser" data-uk-htmleditor></textarea>
<span class="uk-text-danger"><?php echo form_error('teaser'); ?></span>
</div>
<div class="uk-form-row">
<label class="uk-form-label" for="body">Body</label>
<textarea id="full" name="full" rows="4" placeholder="Ihre Nachricht"
value="<?php echo set_value('full'); ?>"></textarea>
<span class="uk-text-danger"><?php echo form_error('full'); ?></span>
</div>
<div class="uk-form-row">
<a class="uk-button uk-button-success" data-action="add-post">Submit</a>
</div>
<?php echo form_close(); ?>
</div>
So my problem is, when I click on my submit button - nothing. Maybe you can show me where my problem is.
Thank you!
For your controller, I think you are missing the form helper and validation library. I have included other comments in the code, but try this:
public function add() {
// you need to load these in:
$this->load->helper('form');
$this->load->library('form_validation');
// I am assuming ion_auth is working, however, I would try this code without
// this conditional statement
if ($this->ion_auth->is_admin()) {
// Validation rules
// Make sure the second parameter is right. I think Titel should be Title.
$this->form_validation->set_rules('title', 'Titel', 'required');
$this->form_validation->set_rules('teaser', 'Teaser', 'required');
$this->form_validation->set_rules('full', 'Volltext', 'required');
// added a triple === instead of == for stricter type checking
if (($this->form_validation->run() === FALSE)) {
// I am assuming $this->data is a property of your controller class
$this->load->view('templates/backend/header', $this->data);
$this->load->view('pages/backend/blog/add', $this->data);
$this->load->view('templates/backend/footer');
} else {
// Check if the form was submitted via $_POST method
if($this->input->post()) {
// I removed your $data array and created it in the model.
// I added a condition here to check if the data was successfully inserted
if ($this->blog_model->create()) {
redirect(base_url().'blog/');
} else {
// output an error message or redirect
}
}
}
} else {
redirect('login');
}
}
For your model, I think you were not passing any data to your model. Try the following for your model:
public function create()
{
// you need to pass an array of data or an object
// the array key corresponds to your db table column
// the array value corresponds to your views input field names
$data = array(
'name' => $this->input->post('title'),
'teaser' => $this->input->post('teaser'),
'full' => $this->input->post('full')
);
// returns true or false
return $this->db->insert('blog', $data);
}

Cant validate form data or insert into database (Codeigniter)

I am working with Codeigniter framework these days. I am trying to validate my form data using codeigniter's form_validation library. The problem is I cant get it to work. I dont know where the problem is no error is being shown.
Here is the view page I am calling controller core and function postad in form action
<div class="container table-responsive" id="con1">
<div class="row" id="h4_subad">
<div class="col-md-4"><h4>Submit an Ad</h4></div>
</div>
<form class="form-horizontal" role="form" method="get" action="/core/postad">
<table id="form_table" class="table-responsive table table-striped" id="table_form">
<td>Ad Title*</td><td><input class="form-control" name="ad_title" id="ad_title" type="text" placeholder="Enter Title" id="ad_form"></td>
<tr> <td>Select Category*</td> <td><select name="ad_form" class="form-control" id="ad_form">
<option>Electronics</option>
<option>Mobiles & Accessories</option>
<option>Computers & Accessories</option>
<option>Kitchen Applicances</option>
<option>Clothing</option>
</select></td></tr>
<tr><td>Ad Description*</td><td><textarea name="txt_area" id="txt_area" class="form-control" type="text" placeholder="Enter Description" ></textarea></td></tr>
<tr><td><button type="button" class="btn btn-warning" onclick="core/postad">Submit Ad</button></td></tr>
</table>
</form>
</div>
Controller for this view
class Core extends CI_Controller {
public function index() {
$this->load->view('header1');
$this->load->view('body');
$this->load->view('footer');
}
public function product() {
$this->load->view('header1');
$this->load->view('product');
$this->load->view('footer');
}
public function contact() {
$this->load->view('header1');
$this->load->view('contact');
$this->load->view('footer');
}
public function aboutus() {
$this->load->view('header1');
$this->load->view('aboutus');
$this->load->view('footer');
}
public function reg() {
$this->load->view('header1');
$this->load->view('reg');
$this->load->view('footer');
$this->input->get('username');
}
public function postad() {
$this->load->view('header1');
$this->load->view('postad');
$this->load->view('footer');
$this->load->model('insert_model');
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
//Validating Name Field
$this->form_validation->set_rules('txt_area', 'Text Area', 'required|min_length[5]|max_length[15]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('postad');
}
else {
//Setting values for tabel columns
$data = array(
'description' => $this->input->post('txt_area'),
);
//Transfering data to Model
$this->insert_model->form_insert($data);
$data['message'] = 'Data Inserted Successfully';
//Loading View
$this->load->view('insert_view', $data);
}
}
}
Model for database connectivity
<?php
class insert_model extends CI_Model{
function __construct() {
parent::__construct();
}
function form_insert($data){
$this->db->insert('post_ad', $data);
}
}
?>
Only trying it for just one field txt_area
I dont want to use codeigniter's form helper as I am using bootstrap form.
You should echo the validation_errors(); as below in your html . For more information you can vist this
<?php echo validation_errors(); ?>
<div class="row" id="h4_subad">
<div class="col-md-4"><h4>Submit an Ad</h4></div>
</div>
<form class="form-horizontal" role="form" method="get" action="/core/postad">
<table id="form_table" class="table-responsive table table-striped" id="table_form">
<td>Ad Title*</td><td><input class="form-control" name="ad_title" id="ad_title" type="text" placeholder="Enter Title" id="ad_form"></td>
<tr> <td>Select Category*</td> <td><select name="ad_form" class="form-control" id="ad_form">
<option>Electronics</option>
<option>Mobiles & Accessories</option>
<option>Computers & Accessories</option>
<option>Kitchen Applicances</option>
<option>Clothing</option>
</select></td></tr>
<tr><td>Ad Description*</td><td><textarea name="txt_area" id="txt_area" class="form-control" type="text" placeholder="Enter Description" ></textarea></td></tr>
<tr><td><button type="button" class="btn btn-warning" onclick="core/postad">Submit Ad</button></td></tr>
</table>
</form>
</div>
You need to use validation_errors(); in posted.php file (HTML) for display error message which you validate.
<?php echo validation_errors(); ?>
Form Validation User Manual
Your button to submit the form is incorrect. When you try to submit a form make sure to set the type to submit too.
<button type="submit">Submit form</button>
or
<submit>Submit form</submit>

Unidentified Property in Control in my codeigniter framework

I am new to frameworks and trying to send values to database table, I have done it simply but now I am using templates (the famous alemsaeed free AdminLTE) now after setting BASEPATH and giving database connection in database.php,I am still getting errors.
However the code looks fine but I don't know please kindly tell me the reason it's my 2nd week in codeigniter.
MODEL
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Mdl extends CI_Model {
function __construct() {
parent::__construct();
}
function form_insert($data){
$data = array(
'name' => $this->input->post('name'),
'fname' => $this->input->post('fname'),
'dob' =>$this->input->post('dob')
);
$this->db->insert('folio', $data);
}
}
?>
Controller
public function index() {
$this->load->helper(array('form','url'));
$this->load->view('myself');
}
public function eid() {
$this->load->helper(array('form','url'));
$this->load->database();
$this->load->model('mdl');
$this->model->form_insert();
$this->load->view('edu');
}
}
?>
View
Note:i have shorted the whole view code because it's a template
For Form Loading the usual
<?php
echo form_open(site_url().'/ctrl/eid/');
$attributes = array('class' => 'sidebar-form');
?>
<label>Enter Your Name</label>
<input type="text" class="form-control" placeholder="YourName" name="name">
<label>Father Name</label>
<input type="text" class="form-control" name="fname" placeholder="father name">
<input type="date" class="form-control" name="dob" placeholder="DD/MM/YYYY" >
<button type="submit" class="btn btn-info pull-right">Submit</button>
</div>
<div class="box-footer">
Copyright © 2015-2016 PIET MULTAN.</strong> All rights reserved.
</div>
</div>
Remove argument $data from form_insert method of model. Like: public function form_insert() {/* rest of code here */} because you are not passing anything from controller. Although I would advice you that you do that passing from controller like:
public function eid()
{
//controller
$data = array();
$data['name'] => $this->input->post('name'),
$data['fname'] => $this->input->post('fname'),
$data['dob'] =>$this->input->post('dob'),
$this->load->helper(array('form','url'));
$this->load->database();
$this->load->model('mdl');
if ( $this->model->form_insert($data) )
{
//success view
}
else
{
//insert fail view
}
}
function form_insert($data)
{
//model
$this->db->insert('folio', $data);
return $this->db->affected_rows() > 0;
}
In Controller
function __construct()
{
parent::__construct();
//load default helpers in this
$this->load->helper('form');
$this->load->helper('url');
$this->load->model('mdl');
}
public function index()
{
$this->load->view('myself');
}
public function eid()
{
$name = $_POST['name'];
$fname = $_POST['fname'];
$dob = $_POST['dob'];
if(!empty($name) || !empty($fname) || !empty($dob))
{
$this->Mdl->form_insert($name, $fname, $dob);//Model name should be Mdl
$this->load->view('edu');
}
else
{
//fields are empty
}
}
In Model
function form_insert($name, $fname, $dob)
{
$data = array(
'name' => $name,
'fname' => $fname,
'dob' => $dob
);
$this->db->insert('folio', $data);
}
In View (Form should be)
<form action="<?php echo base_url();?>index.php/ctrl/eid" method="post" class="sidebar-form">
<label>Enter Your Name</label>
<input type="text" class="form-control" placeholder="YourName" name="name">
<label>Father Name</label>
<input type="text" class="form-control" name="fname" placeholder="father name">
<label>Date Of Birth</label>
<input type="date" class="form-control" name="dob" placeholder="DD/MM/YYYY" >
<button type="submit" class="btn btn-info pull-right">Submit</button>
</form>
The Problem was in the Controller
it should be the modelfileName not model in the code where we load the function
$this->mdl->form_insert();
And this did work :D
The Next problem was in the sitemap
it was this before
$config['site_url']='../../folio/';
instead of this
$config['site_url']='http://localhost/folio/index.php/';
But Once Again i appreciate all for your help it really means to me a lot .
THK_STACOVRFLW

Categories