Codeigniter - Call to undefined function set_value() - codeigniter

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

Related

Laravel Import Excel into DB: ErrorException Undefined array key "name"

I am trying to use the Maatwebsite\Excel package to let users import a csv or excel file and it get imported into the DB.
I am new to laravel, so I am not quite sure how to troubleshoot this issue.
I keep getting the error:
ErrorException Undefined array key "FIRST"
http://127.0.0.1:8000/import-form
CSV Sample Data
FIRST,LAST,EMAIL,PHONE,DEPARTMENT,LOCATION
test name 1,teast last 1,test#mail.com,123-123-1231,test department,test location
Routes:
Route::post('/import-form', [ImportPatientController::class, 'importForm'])->name('import.file');
ImportPatientController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\ImportPatientModel;
use Excel;
use App\Imports\PatientImport;
use App\Http\Controllers\Controller;
class ImportPatientController extends Controller
{
public function importUploadForm()
{
return view('import-form');
}
public function importForm(Request $request)
{
Excel::import(new PatientImport,$request->file2);
return "Record are imported successfully!";
}
}
PatientImport.php (Imports Folder)
<?php
namespace App\Imports;
use App\Models\ImportPatientModel;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class PatientImport implements ToModel, WithHeadingRow
{
/**
* #param array $row
*
* #return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new ImportPatientModel([
'firstName'=>$row['FIRST'],
'lastName' => $row['LAST'],
'email' => $row['EMAIL'],
'phone' => $row['PHONE'],
'department' => $row['DEPARTMENT'],
'location' => $row['LOCATION'],
]);
}
}
ImportPatientModel.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class ImportPatientModel extends Model
{
use HasFactory;
protected $table = "imported_patients";
protected $fillable = ['firstName', 'lastName', 'email', 'phone', 'department', 'location'];
}
import-form.blade.php
<form action="" method="post" enctype="multipart/form-data" action="{{route('import.file')}}">
<img class="flowhealthlogoform" src="{{ url('images/flowhealthlogo.png')}}" />
<h1> BACKUP LIS </h1>
<!-- CROSS Site Request Forgery Protection -->
#csrf
<div class="form-group">
<label>Upload Excel Sheet</label>
<input type="file" class="form-control {{ $errors->has('file') ? 'error' : '' }}" name="file2" id="file">
<!-- Error -->
#if ($errors->has('file'))
<div class="error">
{{ $errors->first('file') }}
</div>
#endif
</div>
<input type="submit" name="send" value="Submit" class="btn btn-dark btn-block">
</form>```
Array keys in PHP are case sensitive.
I think if you change $row['FIRST'] to $row['first'] the issue will be solved!

Error 404 Page Not Found after submitting form

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(); ?>">

Undefined Variable: rows and How to display image slider in Codeigniter 3

Please help me for this...I am learning CodeIgniter 3 and i got stuck on this error.
I am getting an error:
Notice: Undefined variable: slid and Invalid argument supplied for foreach(). Can anyone give me suggest to resolve this?
Controller : home.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
function __construct()
{
parent::__construct();
}
function sliders(){
$slider = $this->slider_model->get_slider();
if ($slider){
$data['slid'] = $slider;
}
$this->template->display('home',$data);
}
}
Model : slider_model.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Slider_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function get_slider()
{
$r = $this->db->get('slider');
$data = $r->result();
return $data;
}
}
View : home.php
<div id="slider" class="fixed">
<div class="background-slider"></div>
<div class="background">
<div class="shadow"></div>
<div class="pattern">
<div class="container" id="camera_1">
<div class="camera_slider">
<div class="spinner"></div>
<div class="camera_wrap" id="camera_wrap_1" style="height: 465px">
<?php foreach($slid as $r){ ?>
<div><img src="<?php echo base_url();?><?php echo $r->image?> " alt="Slider">
</div>
<?php } ?>
</div>
</div>
</div>
</div>
</div>
</div>
This is script of Js
< script type = "text/javascript" >
var camera_slider = $("#camera_wrap_1");
camera_slider.owlCarousel({
slideSpeed: 300,
lazyLoad: true,
singleItem: true,
autoPlay: 7000,
stopOnHover: true,
navigation: true
});
$(window).load(function() {
$("#camera_1 .spinner").fadeOut(200);
$("#camera_wrap_1").css("height", "auto");
}); < /script>
Please try in this code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('slider_model');
}
function sliders(){
$slider = $this->slider_model->get_slider();
if ($slider){
$data['slid'] = $slider;
}
$this->template->display('home',$data);
}
}

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, Message: Undefined variable, Passing data

>> My controllerv - verifyregistration
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class verifyregistration extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('users_model','',TRUE);
$this->load->library('email');
}
function index()
{
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('Login', 'Login', 'trim|required|xss_clean|callback_check_database|min_length[6]|strtolower|callback_username_not_exists');
$this->form_validation->set_rules('Haslo', 'Haslo', 'trim|required|xss_clean|min_length[6]');
$this->form_validation->set_rules('Powtorz_Haslo', 'Powtorz haslo', 'trim|required|xss_clean|matches[Haslo]|min_length[6]');
$this->form_validation->set_rules('Imie', 'Imie', 'trim|required|xss_clean|min_length[2]');
$this->form_validation->set_rules('Nazwisko', 'Nazwisko', 'trim|required|xss_clean');
$this->form_validation->set_rules('Email', 'Email', 'trim|required|xss_clean|valid_email|min_length[6]');
$this->form_validation->set_rules('Data_urodzenia', 'Data urodzenia', 'trim|required|xss_clean');
$this->form_validation->set_rules('Telefon', 'Telefon', 'trim|required|xss_clean|min_length[8]');
$this->form_validation->set_rules('Miasto', 'Miasto', 'trim|required|xss_clean');
$this->form_validation->set_rules('Ulica', 'Ulica', 'trim|required|xss_clean|min_length[6]');
$this->form_validation->set_rules('Kod_pocztowy', 'Kod pocztowy', 'trim|required|xss_clean');
$this->form_validation->set_error_delimiters('<span style="color: red; font-size: 12px; ; line-height: 14px; ">', '</span>');
$this->form_validation->set_error_delimiters('<li>', '</li>');
$data['heading'] = "My Real Heading";
if($this->form_validation->run() == FALSE)
{
$this->load->view('register/register_open',$data);
}
else
{
return false; //this is not important
}
}
>> My view open - register_open
<?php
$this->load->view('mains/header');
$this->load->view('login/loggin');
$this->load->view('mains/menu');
$this->load->view('left_column/left_column_before');
$this->load->view('left_column/menu_left');
$this->load->view('left_column/left_column');
$this->load->view('center/center_column_before');
$this->load->view('register/register_view',$data);
$this->load->view('center/center_column');
$this->load->view('right_column/right_column_before');
$this->load->view('right_column/right_column');
$this->load->view('mains/footer');
?>
>>My view - register_view
<?php echo form_open('verifyregistration/index'); ?>
<form>
<label for="username">Login:</label>
<input type="text" size="25" id="Login" name="Login" set_value='Login' />
.
.
.
<legend>
<input type="checkbox" id="regulamin" name="Regulamin" onclick="this.form.elements['Wyslij'].disabled = !this.checked" />
Akceptuję regulamin serwisu. </legend>
<label> </label>
<input type="submit" name="Wyslij" value="Wyslij" disabled="disabled"/>
<label> </label>
<legend>
<<?php echo $heading;?>
</legend>
</form>
And I have errors:
Severity: Notice
Message: Undefined variable: data
Filename: register/register_open.php
Line Number: 9
this line-> $this->load->view('register/register_view',$data);
Severity: Notice
Message: Undefined variable: heading
Filename: register/register_view.php
Line Number: 48
this line-> <<?php echo $heading;?>
How I can pass the data to another view?
In your controller, change
$this->load->view('register/register_open',$data)
to
$this->load->view('register/register_open',array('data' => $data));
When you do $this->load->view('view', $variable) it takes $variable and uses the php function extract() to turn the array keys into variables. So, if you want to use the variable $data in a nested view, you have to send another array like I did above.

Resources