Error 404 Page Not Found after submitting form - codeigniter

controller -
<?php
class News extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model('News_model');
$this->load->helper('url_helper');
}
public function create(){
echo $data['title']=$data['title1'] = 'Form';
$this->load->helper('form');
$this->load->helper('url');
$this->load->library('form_validation');
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'Text', 'required');
$this->load->view('news/create', $data);
if ($this->form_validation->run() === FALSE)
{
// $this->load->view('templates/header', $data);
//$this->load->view('news/create');
// $this->load->view('templates/footer');
}
else
{
$this->news_model->set_news();
$this->load->view('news/success');
}
}
}
?>
view-
<h2><?php echo $title1; ?></h2>
<?php echo validation_errors();
$this->load->helper('form'); ?>
<?php echo form_open(); ?>
<label for="title">Title</label>
<input type="input" name="title" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Create news item" />
</form>
model-
<?php
class News_model extends CI_Model{
public function __construct(){
$this->load->database();
}
public function getnews($slug= FALSE){
if($slug === FALSE){
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news' , array('slug' => $slug));
return $query->row_array();
}
}
?>
Getting error - 404 Page Not Found while submitting form on localhost.After Submitting form link looks like this - http://localhost/ci/index.php/news/localhost/ci/index.php/news/create
Getting error - 404 Page Not Found while submitting form on localhost.After Submitting form link looks like this - http://localhost/ci/index.php/news/localhost/ci/index.php/news/create

Please follow the below steps:
1. You need to load 'url_helper' in autoload file and its a best practice instead of writing in every page. path(application > config > autoload.php)
$autoload['helper'] = array('url');
2. You need to add below line in top of the view page.
<base href="<?php echo base_url(); ?>">

Related

How do I have a form on one page with the action on another page in CodeIgniter?

I am writing a page for the admin panel i am making that lists member results. I want to have a search form on this page to search for certain members. Since I have other forms on this page, the action of this search form has to be in a different method in my controller. The problem is that none of the post values are being transmitted to the search method. What am i doing wrong?
class Users extends MY_Controller {
function __construct(){
parent::__construct();
}
function index(){
if(!$this->form_validation->run()){
$this->load->view('users/index');
}
}
function search(){
$criteria = $this->input->post('search_criteria');
}
}
This is something like what my view would be:
<?php echo form_open('users/search'); ?>
<input type="text" name="search_criteria">
You have no form validation rules and form validation was on wrong function
class Users extends MY_Controller {
function __construct(){
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
$this->load->helper('url');
}
function index(){
// There is no need to use index on your view files.
$this->load->view('users');
}
function search(){
$criteria = $this->input->post('search_criteria');
$this->form_vadliation->set_rules('search_criteria', 'Search', 'required');
if($this->form_validation->run() == FALSE) {
// There is no need to use index on your view files.
$this->load->view('users');
} else {
// Success Stuff
}
}
}
View application > views > users.php
<?php echo validation_errors('<div class="error">', '</div>'); ?>
<?php echo form_open('users/search'); ?>
<input type="text" name="search_criteria" value="" class="" placeholder="" />
<input type="submit" value="Save" />
<?php echo form_close();?>

Codeigniter - Call to undefined function set_value()

im new on codeigniter and try to resolve following problem for hours now.
I try to set_value in a form but when i set it i get following error:
A PHP Error was encountered
Severity: Error
Message: Call to undefined function set_value()
Filename: modals/register_modal.php
Line Number: 27
Backtrace:
When i delete set_value() on line 27, everything works fine.
Line number 27 is the set_value() line:
<div class="row">
<div class="form-group">
<label class="control-label sr-only">Vorname:</label>
<div class="col-md-8 col-md-offset-2 col-xs-10 col-xs-offset-1">
<input type="text" class="form-control" name="firstname" placeholder="Vorname" value="<?php echo set_value('firstname'); ?>" size="50" />
<i class="fa form-control-feedback" aria-hidden="true"></i>
</div>
</div>
</div>
Thats my controller:
class Form extends CI_Controller {
public function index()
{
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('firstname', 'Vorname', 'required|callback_username_check');
$this->form_validation->set_rules('surename', 'Nachname', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Passwort', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('modals/register_modal');
}
else
{
$this->load->view('modals/login_modal');
}
}
public function username_check($str)
{
if ($str == 'test')
{
$this->form_validation->set_message('username_check', 'The {field} field can not be the word "test"');
return FALSE;
}
else
{
return TRUE;
}
}
}
I also set Auto-Load:
$autoload['helper'] = array('form');
You must load library('form_validation') in your Controller.
This work for me:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller{
public function __construct(){
parent::__construct();
$this -> load -> library('form_validation');
}
....
In CodeIgniter4 you have only to use the helper function to load the library
<?php namespace App\Controllers;
use App\Models\UserModel;
class Users extends BaseController {
public function index()
{
helper (['form']);
......
Add helper HTML
protected $helpers = ['form','url','html'];
It worked for me
You can add form helper in BaseController.php. you don't need to add in every cotnroller - codeigniter 4
Edit in BaseController.php
protected $helpers = ['form'];
helper(['form']);
Load the form helper in your function

Ion Auth profile page

I would like a step by step tutorial on how to create a profile page for ion auth codeigniter.
When a logged in user clicks a link User profile link, it opens a user profile page and retrieves all the details of the user in a form so the user can update. I would this to be for the admin users.
Thank you :)
I ended up doing it this way :
below controller :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User extends MY_Controller {
function __construct() {
parent::__construct();
$this->load->library('ion_auth');
}
public function index() {
}
public function login() {
if ($this->input->post()) {
$this->load->library('form_validation');
$this->form_validation->set_rules('identity', 'Identity', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('remember', 'Remember me', 'integer');
if ($this->form_validation->run() === TRUE) {
$remember = (bool) $this->input->post('remember');
if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember)) {
redirect('dashboard', 'refresh');
} else {
$this->session->set_flashdata('message', $this->ion_auth->errors());
redirect('admin/user/login', 'refresh');
}
}
}
$data['main_content'] = 'admin/login';
$this->load->view('includes/template', $data);
}
public function logout() {
$this->ion_auth->logout();
redirect('admin/user/login', 'refresh');
}
public function profile() {
$user = $this->ion_auth->user()->row();
//print_r($user);
$this->data['user'] = $user;
//var_dump($user);
$this->load->library('form_validation');
$this->form_validation->set_rules('first_name', 'First name', 'trim');
$this->form_validation->set_rules('last_name', 'Last name', 'trim');
$this->form_validation->set_rules('company', 'Company', 'trim');
$this->form_validation->set_rules('phone', 'Phone', 'trim');
if ($this->form_validation->run() === FALSE) {
$this->load->view('admin/edit_profile', $this->data);
} else {
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'company' => $this->input->post('company'),
'phone' => $this->input->post('phone')
);
if (strlen($this->input->post('password')) >= 6)
$new_data['password'] = $this->input->post('password');
$this->ion_auth->update($user->id, $data);
redirect('dashboard', 'refresh');
}
//$this->load->view('admin/edit_profile', $data);
}
}
then the view :
<h1><?php echo lang('edit_user_heading');?></h1>
<p>
<?php echo lang('edit_user_fname_label', 'first_name');?> <br />
<?php echo form_input($first_name);?>
</p>
<p>
<?php echo lang('edit_user_lname_label', 'last_name');?> <br />
<?php echo form_input($last_name);?>
</p>
<p>
<?php echo lang('edit_user_company_label', 'company');?> <br />
<?php echo form_input($company);?>
</p>
<p>
<?php echo lang('edit_user_phone_label', 'phone');?> <br />
<?php echo form_input($phone);?>
</p>
<p>
<?php echo lang('edit_user_password_label', 'password');?> <br />
<?php echo form_input($password);?>
</p>
<p>
<?php echo lang('edit_user_password_confirm_label', 'password_confirm');?><br />
<?php echo form_input($password_confirm);?>
</p>
<?php if ($this->ion_auth->is_admin()): ?>
<h3><?php echo lang('edit_user_groups_heading');?></h3>
<?php foreach ($groups as $group):?>
<label class="checkbox">
<?php
$gID=$group['id'];
$checked = null;
$item = null;
foreach($currentGroups as $grp) {
if ($gID == $grp->id) {
$checked= ' checked="checked"';
break;
}
}
?>
<input type="checkbox" name="groups[]" value="<?php echo $group['id'];?>"<?php echo $checked;?>>
<?php echo htmlspecialchars($group['name'],ENT_QUOTES,'UTF-8');?>
</label>
<?php endforeach?>
<?php endif ?>
<?php echo form_hidden('id', $user->id);?>
<?php echo form_hidden($csrf); ?>
<p><?php echo form_submit('submit', lang('edit_user_submit_btn'));?></p>

How do I get domPDF to display my codeigniter view correctly

I feel there is a small step that I am missing that apparently everyone on the other related questions understands.
I have created a simple CI 2 view, controller and model, shown below:
I have installed dompdf into the helpers folder like so:
applications/helpers/dompdf
applications/helpers/dompdf/dompdf_help.php
What I want to happen is when user clicks the submit button on the view page, send form data to the db, then get a pdf of that filled in form.
Between getting underdefined var errors or nothing at all, except for the data going to db, I can't see what I am missing.
Could some please guide me? What am I not getting here?
View
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test pdf</title>
</head>
<body>
<?php // Change the css classes to suit your needs
$attributes = array('class' => '', 'id' => '');
echo form_open('quicksubmit', $attributes); ?>
<p>
<label for="title">Title <span class="required">*</span></label>
<?php echo form_error('title'); ?>
<?php // Change the values in this array to populate your dropdown as required ?>
<?php $options = array(
'' => 'Please Select',
'Mrs' => 'Mrs',
'Miss' => 'Miss',
'Ms' => 'Ms',
'Mr' => 'Mr',
); ?>
<br /><?php echo form_dropdown('title', $options, set_value('title'))?>
</p>
<p>
<label for="first_name">First Name</label>
<?php echo form_error('first_name'); ?>
<br /><input id="first_name" type="text" name="first_name" maxlength="100" value="<?php echo set_value('first_name'); ?>" />
</p>
<p>
<label for="last_name">Last Name <span class="required">*</span></label>
<?php echo form_error('last_name'); ?>
<br /><input id="last_name" type="text" name="last_name" maxlength="100" value="<?php echo set_value('last_name'); ?>" />
</p>
<p>
<label for="branch">Branch</label>
<?php echo form_error('branch'); ?>
<?php // Change the values in this array to populate your dropdown as required ?>
<?php $options = array(
'' => 'Please Select',
'Branch 1' => 'Branch One',
'Branch 2' => 'Branch Two',
); ?>
<br /><?php echo form_dropdown('branch', $options, set_value('branch'))?>
</p>
<p>
<label for="zip">Zip</label>
<?php echo form_error('zip'); ?>
<br /><input id="zip" type="text" name="zip" maxlength="7" value="<?php echo set_value('zip'); ?>" />
</p>
<p>
<?php echo form_submit( 'submit', 'Submit'); ?>
</p>
<?php echo form_close(); ?>
</body>
</html>
Controller
<?php
class Quicksubmit extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->database();
$this->load->helper('form');
$this->load->helper('url');
$this->load->model('quicksubmit_model');
}
function index()
{
$this->form_validation->set_rules('title', 'Title', 'required|trim|xss_clean|max_length[50]');
$this->form_validation->set_rules('first_name', 'First Name', 'trim|xss_clean|max_length[100]');
$this->form_validation->set_rules('last_name', 'Last Name', 'required|trim|xss_clean|max_length[100]');
$this->form_validation->set_rules('branch', 'Branch', 'trim|xss_clean|max_length[100]');
$this->form_validation->set_rules('zip', 'Zip', 'trim|xss_clean|is_numeric|max_length[7]');
$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
if ($this->form_validation->run() == FALSE) // validation hasn't been passed
{
$this->load->view('quicksubmit_view');
}
else // passed validation proceed to post success logic
{
// build array for the model
$this->pdf($output);
$form_data = array(
'title' => set_value('title'),
'first_name' => set_value('first_name'),
'last_name' => set_value('last_name'),
'branch' => set_value('branch'),
'zip' => set_value('zip')
);
// run insert model to write data to db
if ($this->quicksubmit_model->SaveForm($form_data) == TRUE) // the information has therefore been successfully saved in the db
{
redirect('quicksubmit/success'); // or whatever logic needs to occur
}
else
{
echo 'An error occurred saving your information. Please try again later';
// Or whatever error handling is necessary
}
}
}
function success()
{
redirect(base_url(),'refresh');
/*echo 'this form has been successfully submitted with all validation being passed. All messages or logic here. Please note
sessions have not been used and would need to be added in to suit your app';*/
}
function pdf()
{
$this->load->helper(array('dompdf', 'file'));
// page info here, db calls, etc.
$html = $this->load->view('quicksubmit_view', $data, true);
pdf_create($html, 'filename');
/*or
$data = pdf_create($html, '', false);
write_file('name', $data);*/
//if you want to write it to disk and/or send it as an attachment
}
}
?>
Model
<?php
class Quicksubmit_model extends CI_Model {
function __construct()
{
parent::__construct();
}
// --------------------------------------------------------------------
/**
* function SaveForm()
*
* insert form data
* #param $form_data - array
* #return Bool - TRUE or FALSE
*/
function SaveForm($form_data)
{
$this->db->insert('quicksubmit', $form_data);
if ($this->db->affected_rows() == '1')
{
return TRUE;
}
return FALSE;
}
}
?>
dompdf_help.php file
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename='', $stream=TRUE)
{
require_once("dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
if ($stream) {
$dompdf->stream($filename.".pdf");
} else {
return $dompdf->output();
}
}
?>
you were nearly there!
It is probably better to store dompdf in the third_party folder, and it is not a code igniter helper. - see the path i store it in in the constructor. Then it is always available.
Also, it is probably better to do the 'work' of the program in the model, so this includes making PDFs etc.
don't use a ?> at the end of your code.
i modded your code to work, and verified it did work. it simply saves a file named tmp/name.pdf. I am sure you can work out the rest. i did comment out the database loader because that wasn't needed for me to test the code.
see enc.
<?php
class Quicksubmit extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
//$this->load->database();
$this->load->helper('form');
$this->load->helper('url');
$this->load->helper('file');
$this->load->model('quicksubmit_model');
global $_dompdf_show_warnings;global $_dompdf_debug;global $_DOMPDF_DEBUG_TYPES;global $_dompdf_warnings;$_dompdf_show_warnings = FALSE;
require_once(realpath(APPPATH."third_party/dompdf")."/dompdf_config.inc.php"); // remember that the constant DOMPDF_TEMP_DIR may need to be changed.
spl_autoload_register('DOMPDF_autoload');
}
function index()
{
$this->form_validation->set_rules('title', 'Title', 'required|trim|xss_clean|max_length[50]');
$this->form_validation->set_rules('first_name', 'First Name', 'trim|xss_clean|max_length[100]');
$this->form_validation->set_rules('last_name', 'Last Name', 'required|trim|xss_clean|max_length[100]');
$this->form_validation->set_rules('branch', 'Branch', 'trim|xss_clean|max_length[100]');
$this->form_validation->set_rules('zip', 'Zip', 'trim|xss_clean|is_numeric|max_length[7]');
$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
if ($this->form_validation->run() == FALSE) // validation hasn't been passed
{
$this->load->view('quicksubmit_view');
}
else // passed validation proceed to post success logic
{
// build array for the model
$form_data = array(
'title' => set_value('title'),
'first_name' => set_value('first_name'),
'last_name' => set_value('last_name'),
'branch' => set_value('branch'),
'zip' => set_value('zip')
);
$this->pdf($form_data);
// run insert model to write data to db
if ($this->quicksubmit_model->SaveForm($form_data) == TRUE) // the information has therefore been successfully saved in the db
{
redirect('quicksubmit/success'); // or whatever logic needs to occur
}
else
{
echo 'An error occurred saving your information. Please try again later';
// Or whatever error handling is necessary
}
}
}
function success()
{
redirect(base_url(),'refresh');
/*echo 'this form has been successfully submitted with all validation being passed. All messages or logic here. Please note
sessions have not been used and would need to be added in to suit your app';*/
}
function pdf($data)
{
$dompdf = new DOMPDF();
$html = $this->load->view('quicksubmit_view', $data, true);
$dompdf->set_paper('a4','portrait');
$dompdf->load_html($html);
$dompdf->render();
$pdf = $dompdf->output();
write_file('tmp/name.pdf', $pdf);
}
}

Codeigniter, Displaying results from database

I am new to CI, I've been working on this for hours with no success, please help! I am trying to generate user profiles by retrieving information from a database and displaying it in the view (using the USERID). I am getting both an Undefined Variable Error and Trying to Get Property Out of Non-Object error. Here is my code:
Model:
public function my_data()
{
$userid = $this->session->userdata('userid');
$data = array();
$this->db->select('*');
$this->db->from('user_profile');
$this->db->where('userid', $userid);
$query = $this->db->get();
return $query->result();
}
Controller:
public function profile()
{
$data['query'] = $this->user_model->my_data();
$this->load->view('header_loggedin');
$this->load->view('sidebar_left');
$this->load->view('user/user_profile', $user);
$this->load->view('footer');
}
View:
<div class="control-group">
<i class="icon-user"></i>
Name: <?php echo $row->name; ?>
</div>
<div class="control-group">
<i class="icon-home"></i>
Location: <?php echo $data->location;?>
</div>
<div class="control-group">
<i class="icon-briefcase"></i>
Occupation: <?php echo $data->occupation;?>
</div>
Thanks so much in advance, I feel like I have tried everything!
Hi as new to CI you will face some problems but later you will enjoy it here are some simple stuff you are doing wrong
Model
public function my_data()
{
$userid = $this->session->userdata('userid');
$data = array();
$this->db->select('*');
$this->db->from('user_profile');
$this->db->where('userid', $userid);
$query = $this->db->get();
//this will return multiple rows or object of arrays
//return $query->result();
// you need to send only single row
return $query->row();
}
Controller
public function profile()
{
// in data array key name should be same which you will pass to view
$data['row'] = $this->user_model->my_data();
$this->load->view('header_loggedin');
$this->load->view('sidebar_left');
$this->load->view('user/user_profile', $data);
$this->load->view('footer');
}
View
<div class="control-group">
<i class="icon-user"></i>
Name: <?php echo $row->name; ?>
</div>
<div class="control-group">
<i class="icon-home"></i>
Location: <?php echo $row->location;?>
</div>
<div class="control-group">
<i class="icon-briefcase"></i>
Occupation: <?php echo $row->occupation;?>
</div>
for more information please read CI user guide you will understand so many stuff from there CI user guide
here change $user to $data
public function profile()
{
$data['result'] = $this->user_model->my_data();
$this->load->view('header_loggedin');
$this->load->view('sidebar_left');
$this->load->view('user/user_profile', $data);
$this->load->view('footer');
}
then if your query returns only one row , you can use
return $query->row_array();
instead of
return $query->result();
in view you can now use
Name: <?php echo $result['name']; ?>
Location: <?php echo $result['location'];?>
Occupation: <?php echo $result['occupation'];?>

Resources